import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { RouterExtensions } from '@nativescript/angular';
import { Select, Store } from '@ngxs/store';
import { FeedbackType } from 'nativescript-feedback';
import { Observable } from 'rxjs';
import { withLatestFrom } from 'rxjs/operators';
import { transitionOptions } from '~/constants/shared.constants';
import { PhoneContactService } from '~/core/services/phone-contact.service';
import { WarehouseItemReservationAllOutput } from '../../entities/output/warehouseItemReservationAllOutput';
import { LoadWarehouseItemReservationAll } from '../../state-mgmt/reservation-lent-detail.action';
import { ReservationLentDetailState } from '../../state-mgmt/reservation-lent-detail.state';
import { feedbackMessage } from '../../utils/feedback-messages';

@Component({
    selector: 'ReservationLentDetail',
    moduleId: module.id,
    templateUrl: './reservation-lent-detail.component.html',
    styleUrls: ['./reservation-lent-detail.component.scss']
})

export class ReservationLentDetailComponent implements OnInit {
    @Select(ReservationLentDetailState.getwarehouseItemReservationAll) reservation$: Observable<WarehouseItemReservationAllOutput>;
    @Select(ReservationLentDetailState.isLoading) isLoading$: Observable<boolean>;
    reservation: WarehouseItemReservationAllOutput;
    othersIsLoading: boolean;

    constructor(
        private router: Router,
        private route: ActivatedRoute,
        private routerExtensions: RouterExtensions,
        private store: Store,
        private phoneContactSrv: PhoneContactService
    ) {
        this.router.events.subscribe((val) => {
            if (val instanceof NavigationEnd) {
                if (RegExp('\/material\/reservationLentDetail\/[0-9]+\/[0-9]+').test(this.router.url)) {
                    this.othersIsLoading = false;
                }
            }
        });
     }

    ngOnInit(): void {
        this.othersIsLoading = false;
        this.store.dispatch(new LoadWarehouseItemReservationAll(this.route.snapshot.params['idItem'], this.route.snapshot.params['idReservation']))
            .pipe(withLatestFrom(this.reservation$))
            .subscribe(([_, reservation]) => {
                this.reservation = reservation;
            }, err => {
                feedbackMessage(FeedbackType.Error, 'Načtení rezervace', 'Nepodařilo se načíst rezervaci.');
                console.log(err);
            });
    }

    onItemTap() {
        this.othersIsLoading = true;
        setTimeout(() => {
            this.routerExtensions.navigate(['material/materialDetail', this.reservation.ID_WarehouseItem], {transition: transitionOptions});
        }, 10);
    }

    // onPersonTap() {
    //     setTimeout(() => {
    //         this.routerExtensions.navigate(['search/list/', this.reservation.ID_Submitter, false], {transition: transitionOptions});
    //     }, 10);
    // }

    // onWarehouseOriginTap() {
    //     if (this.warehousesIds.indexOf(this.reservation.ID_WarehouseOrigin.toString()) === -1) {
    //         feedbackMessage(FeedbackType.Error, "Cizí sklad", "Nemáte přístup do tohoto skladu.");
    //         return;
    //     }
    //     setTimeout(() => {
    //         this.routerExtensions.navigate(['material/warehouseDetail', this.reservation.ID_WarehouseOrigin], {transition: transitionOptions});
    //     }, 10);
    // }

    onWarehouseTap() {
        this.othersIsLoading = true;
        setTimeout(() => {
            this.routerExtensions.navigate(['material/warehouseDetail', this.reservation.ID_Warehouse], {transition: transitionOptions});
        }, 10);
    }

    onCallTap() {
        this.phoneContactSrv.createCall(this.reservation.Phone);
    }

    onEmailTap() {
        this.phoneContactSrv.createEmail(this.reservation.Email);
    }

    onBackButton(): void {
        this.routerExtensions.back();
    }
}
