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

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

    protected readonly SearchSelectComponent = SearchSelectComponent;
    protected vesselColDefinitions: ListColDefinition[];
    protected locationColDefinitions: ListColDefinition[];
    protected readonly tripForm = tripForm;

    constructor(
        private tripService: TripService,
        private vesselService: VesselService,
        private locationService: LocationService,
        private appHelperService: AppHelperService,
        translateService: TranslateService,
        router: Router
    ) {
        super(
            tripForm,
            (data: TripJsonld) => {
                data.completed = false;
                return this.tripService.tripsPost(data);
            },
            (id: string | number, data: TripJsonld) =>
                this.tripService.tripsIdPatch(
                    id.toString(),
                    this.appHelperService.convertJsonldToJson(data)
                ),
            (id: string | number) => this.tripService.tripsIdDelete(id.toString()),
            translateService,
            router
        );

        this.redirectAfterDelete = '/' + ROUTE_BASE_DATA;
        this.vesselColDefinitions = SearchSelectComponent.getDefaultColDefVessels();
        this.locationColDefinitions = SearchSelectComponent.getDefaultColDefLocations();
    }

    getVessels: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
        return this.vesselService.vesselsGetCollection(index, pageSize, term);
    }

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