import {
    Component,
    EventEmitter,
    Input,
    OnInit,
    Output,
    ViewChild,
    ViewEncapsulation,
} from '@angular/core';
import {
    AirportModel,
    CityModel,
    RegionFlatModel,
} from '@modules/components/page-filters/models';
import { DictionariesService } from '@modules/components/page-filters/services/dictionaries-service';
import { FeatureFlagsService } from '@shared/services/feature-flags.service';
import { ScrollPanel } from 'primeng/scrollpanel';

@Component({
    selector: 'city-select',
    templateUrl: './city-select.component.html',
    styleUrls: ['./city-select.component.scss'],
    encapsulation: ViewEncapsulation.None,
})
export class CitySelectComponent implements OnInit {
    currentRegion: RegionFlatModel;
    @Input() city: CityModel | AirportModel;
    @Output() selectLocation: EventEmitter<CityModel | AirportModel> =
        new EventEmitter<CityModel | AirportModel>();
    @Output() locate = new EventEmitter();

    @ViewChild('sp', { static: false }) scrollPanel: ScrollPanel;

    constructor(
        public dictService: DictionariesService,
        public featureFlags: FeatureFlagsService,
    ) {}

    ngOnInit() {
        this.currentRegion = this.dictService.regions.find(
            (x) => x.id == 500374,
        );
    }

    loadRegion(region) {
        this.currentRegion = region;

        this.scrollPanel.scrollTop(0);
    }

    getRegionData() {
        if (this.currentRegion) {
            return this.currentRegion;
        }
        return [];
    }

    onLocationSelect(model: CityModel | AirportModel) {
        this.selectLocation.emit(model);
    }

    onLocate() {
        this.locate.emit();
    }
}
