import { Component, OnInit } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { RouterExtensions } from '@nativescript/angular';
import { AndroidActivityBackPressedEventData, AndroidApplication, Application, EventData } from '@nativescript/core';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { transitionOptions } from '~/constants/shared.constants';
import { PickerProvider } from '~/core/entities/picker-provider';
import { dismissSoftKeybaord } from '~/core/utils/dismiss-keyboard';
import { ApplicationNewData } from '~/pages/application/entities/applicationNewData';
import { PersonSearch } from '~/pages/search-person/entities/person-search';
import { SetSearchPersonParams } from '~/pages/search-person/state-mgmt/search-person-list.actions';
import { LoadOfferTypes } from '~/pages/search-person/state-mgmt/search-person.actions';
import { SearchPersonState } from '~/pages/search-person/state-mgmt/search-person.state';

@Component({
    selector: 'SearchPerson',
    moduleId: module.id,
    templateUrl: './search-person.component.html',
    styleUrls: ['./_search-person.component.scss']
})
export class SearchPersonComponent implements OnInit {
    personSearch: PersonSearch;
    @Select(SearchPersonState.isLoading) isLoading$: Observable<boolean>;
    @Select(SearchPersonState.getOfferTypesForProvider) offerTypes$: Observable<PickerProvider[]>;
    othersIsLoading: boolean;

    applicationData: ApplicationNewData;

    constructor(
        private router: Router,
        private routerExtensions: RouterExtensions,
        private store: Store
    ) {
        this.router.events.subscribe((val) => {
            if (val instanceof NavigationEnd) {
                if (this.router.url === '/search') {
                    this.othersIsLoading = false;
                }
            }
        });
    }

    ngOnInit(): void {
        this.applicationData = new ApplicationNewData();
        this.othersIsLoading = false;
        this.personSearch = new PersonSearch('', 0, false, true);
        this.store.dispatch(new LoadOfferTypes());
        Application.android.on(AndroidApplication.activityBackPressedEvent, (args: AndroidActivityBackPressedEventData) => {
            dismissSoftKeybaord();
        });
    }

    onSearchPersons(args: EventData) {
        if (!this.personSearch.searchInUnits && !this.personSearch.searchInAddressBook) {
            alert({
                title: 'Pozor',
                message: 'Není povoleno hledání ani mezi členy jednotky, ani ve Skautském adresáři. Povol alespoň jednu možnost, pak bude možné hledat.',
                okButtonText: 'OK'
            });

            return;
        }

        if ((this.personSearch.searchInUnits || this.personSearch.searchInAddressBook) && this.personSearch.name === '' && this.personSearch.offer === 0) {
            alert({
                title: 'Pozor',
                message: 'Nebylo zadáno jméno. Hledání bez uvedení jména je možné pouze ve Skautském adresáři. Musíš však zvolit nějakou činnost.',
                okButtonText: 'OK'
            });

            return;
        }

        this.othersIsLoading = true;
        this.store.dispatch(new SetSearchPersonParams(this.personSearch));
        dismissSoftKeybaord();
        setTimeout(() => {
            this.routerExtensions.navigate(['/search/list'], { transition: transitionOptions });
        }, 10);
    }

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