import { Component } from '@angular/core';
import { VesselJsonld, VesselService, ShippingCompanyService } from "@app/core/api/v1";
import { vesselForm } from "@app/_forms/apiForms";
import { SearchSelectComponent } from "@app/_components/search-select/search-select.component";
import { ListGetDataFunctionType } from "@app/_components/list/list-get-data-function-type";
import { ListColDefinition } from "@app/_components/list/list-col-definition";
import { AbstractDataFormComponent } from "@app/_components/_abstract/abstract-data-form-component";
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";

@Component({
    selector: 'app-vessel-form',
    templateUrl: './vessel-form.component.html'
})
export class VesselFormComponent extends AbstractDataFormComponent<VesselJsonld> {
    protected readonly SearchSelectComponent = SearchSelectComponent;
    protected shippingCompanyColDefinitions: ListColDefinition[];

    constructor(
        private vesselService: VesselService,
        private shippingCompanyService: ShippingCompanyService,
        private appHelperService: AppHelperService,
        translateService: TranslateService,
        router: Router
    ) {
        super(
            vesselForm,
            (data: VesselJsonld) => this.vesselService.vesselsPost(data),
            (id: string | number, data: VesselJsonld) =>
                this.vesselService.vesselsIdPatch(
                    id.toString(),
                    this.appHelperService.convertJsonldToJson(data)
                ),
            (id: string | number) => this.vesselService.vesselsIdDelete(id.toString()),
            translateService,
            router
        );

        this.redirectAfterDelete = '/' + ROUTE_BASE_DATA;
        this.shippingCompanyColDefinitions = SearchSelectComponent.getDefaultColDefShippingCompanies();
    }

    protected override getNewDataSet(): VesselJsonld {
        return {} as VesselJsonld;
    }

    getShippingCompanies: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
        return this.shippingCompanyService.shippingCompaniesGetCollection(
            index,
            pageSize,
            term
        );
    }
}