import { Component } from '@angular/core';
import { AbstractDataFormComponent } from "@app/_components/_abstract/abstract-data-form-component";
import { LocationJsonld, LocationService, ZoneService } from "@app/core/api/v1";
import { SearchSelectComponent } from "@app/_components/search-select/search-select.component";
import { ListColDefinition } from "@app/_components/list/list-col-definition";
import { locationForm } from "@app/_forms/apiForms";
import { ListGetDataFunctionType } from "@app/_components/list/list-get-data-function-type";
import { TranslateService } from "@ngx-translate/core";
import { Router } from "@angular/router";
import { ROUTE_BASE_DATA } from "@app/app-routing.module";
import {AppHelperService} from "@app/_helpers/app-helper.service";
import {Validators} from "@angular/forms";

@Component({
    selector: 'app-location-form',
    templateUrl: './location-form.component.html'
})
export class LocationFormComponent extends AbstractDataFormComponent<LocationJsonld> {
    protected readonly SearchSelectComponent = SearchSelectComponent;
    protected zoneColDefinitions: ListColDefinition[];
    protected readonly locationForm = locationForm;

    constructor(
        private locationService: LocationService,
        private zoneService: ZoneService,
        private appHelperService: AppHelperService,
        translateService: TranslateService,
        router: Router
    ) {
        super(
            locationForm,
            (data: LocationJsonld) => this.locationService.locationsPost(data),
            (id: string | number, data: LocationJsonld) => this.locationService.locationsIdPatch(
                id.toString(),
                this.appHelperService.convertJsonldToJson(data)
            ),
            (id: string | number) => this.locationService.locationsIdDelete(id.toString()),
            translateService,
            router
        );

        this.redirectAfterDelete = '/' + ROUTE_BASE_DATA;
        this.zoneColDefinitions = SearchSelectComponent.getDefaultColDefZones();
    }

    override ngOnInit(): void {
        super.ngOnInit();

        // Wenn wir einen neuen Benutzer erstellen, initialisiere active mit false
        if (!this.isEditMode()) {
            this.form.get('isZone')?.setValue(false);
            this.form.get('isPlace')?.setValue(false);
            this.form.get('isPort')?.setValue(false);
        }
    }

    getZones: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
        return this.zoneService.zonesGetCollection(index, pageSize, term);
    }

}