import { Component } from '@angular/core';
import {
    EventService, LocationService,
    UserTripEventJsonld,
    UserTripEventService, UserTripJsonld,
} from "@app/core/api/v1";
import {tripForm, userTripEventForm} from "@app/_forms/apiForms";
import {AbstractDataFormComponent} from "@app/_components/_abstract/abstract-data-form-component";
import {AppHelperService} from "@app/_helpers/app-helper.service";
import {TranslateService} from "@ngx-translate/core";
import {Router} from "@angular/router";
import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
import {SearchSelectComponent} from "@app/_components/search-select/search-select.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";

@Component({
  selector: 'app-user-trip-event-form',
  templateUrl: './user-trip-event-form.component.html',
  styleUrl: './user-trip-event-form.component.scss'
})
export class UserTripEventFormComponent extends AbstractDataFormComponent<UserTripEventJsonld> {

    protected readonly SearchSelectComponent = SearchSelectComponent;
    protected readonly userTripEventForm = userTripEventForm;
    protected userTrip?: UserTripJsonld;
    protected eventColDefinitions: ListColDefinition[] = SearchSelectComponent.getDefaultColDefEvents();
    protected locationColDefinitions: ListColDefinition[] = SearchSelectComponent.getDefaultColDefLocations();

    constructor(
        protected userTripEventService: UserTripEventService,
        protected eventService: EventService,
        protected locationService: LocationService,
        appHelperService: AppHelperService,
        translateService: TranslateService,
        router: Router
    ) {
        super(
            userTripEventForm,
            appHelperService,
            (data: UserTripEventJsonld) => {
                return this.userTripEventService.userTripEventsPost(data);
            },
            (id: string | number, data: UserTripEventJsonld) =>
                this.userTripEventService.userTripEventsIdPatch(
                    id.toString(),
                    this.appHelperService.convertJsonldToJson(data)
                ),
            (id: string | number) => this.userTripEventService.userTripEventsIdDelete(id.toString()),
            translateService,
            router
        );

        //this.redirectAfterDelete = '/' + ROUTE_USER_TRIPS;
    }

    override ngOnInit() {
        super.ngOnInit();
        this.form.get('userTripIri')?.setValue(this.userTrip?.id!);
    }

    getEvents: ListGetDataFunctionType = (
        index: number,
        pageSize: number,
        term?: string,
    ) => {
        return this.eventService.eventsGetCollection(
            index,
            pageSize,
            term
        );
    }

    getLocations: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
        return this.locationService.locationsGetCollection(
            index,
            pageSize,
            term,
        );
    }
}
