import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RouterExtensions } from '@nativescript/angular';
import { confirm } from '@nativescript/core/ui/dialogs';
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 { feedbackMessage } from '~/pages/material/utils/feedback-messages';
import { MeetingDateDetailResponse } from '../../entities/output/meetingDateDetailResponse';
import { DeleteMeetingDate, LoadMeetingDateDetail } from '../../state-mgmt/meeting-date.actions';
import { MeetingDateState } from '../../state-mgmt/meeting-date.state';

@Component({
    selector: 'MeetingDateDetail',
    moduleId: module.id,
    templateUrl: './meeting-date-detail.component.html',
    styleUrls: ['./_meeting-date-detail.component.scss']
})

export class MeetingDateDetailComponent implements OnInit {
    @Select(MeetingDateState.getMeetingDateDetail) meetingDateDetail$: Observable<MeetingDateDetailResponse>;
    @Select(MeetingDateState.isLoading) isLoading$: Observable<boolean>;

    id: number;
    meetingDateDetail: MeetingDateDetailResponse;

    canEdit: boolean = false;

    constructor(
        private store: Store,
        private routerExtensions: RouterExtensions,
        private route: ActivatedRoute
     ) {}

    ngOnInit(): void {
        this.id = +this.route.snapshot.params['id'];
        this.canEdit = this.route.snapshot.params['permission'];
    }

    onLoad() {
        this.store.dispatch(new LoadMeetingDateDetail(this.id))
            .pipe(withLatestFrom(this.meetingDateDetail$))
            .subscribe(([_, output]) => {
                this.meetingDateDetail = output;
            }, err => {
                console.log(err);
                feedbackMessage(FeedbackType.Error, 'Načítání dat', `Při načítání dat došlo k chybě. Zkontrolujte prosím vaše internetové připojení.`);
                this.routerExtensions.back();
            });
    }

    onUpdateTap() {
        this.routerExtensions.navigate(['advertising/meetingUpdate', this.id], {transition: transitionOptions});
    }

    onDeleteTap() {
        const options = {
            title: 'Smazání schůzky',
            message: 'Opravdu chcete tuto schůzku smazat? Schůzka bude smazána permanentně.',
            okButtonText: 'Ano',
            cancelButtonText: 'Ne'
        };
        confirm(options).then((result: boolean) => {
            if (result) {
                this.store.dispatch(new DeleteMeetingDate(this.id))
                    .subscribe(() => {
                        feedbackMessage(FeedbackType.Success, 'Smazání schůzky', `Schůzka byla úspěšně smazána.`);
                        this.routerExtensions.back();
                }, err => {
                    console.log(err);
                    feedbackMessage(FeedbackType.Error, 'Smazání schůzky', `Při mazání schůzky došlo k chybě. Zkontrolujte prosím vaše internetové připojení.`);
                });
            }
        });
    }

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

    get updateIcon() {
        return String.fromCharCode(0xf044);
    }

    get trashIcon() {
        return String.fromCharCode(0xf2ed);
    }
}
