import {Component, ViewChild} from '@angular/core';
import {ListComponent} from "@app/_components/list/list.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";
import {VesselJsonld, VesselService, ZoneJsonld} from "@app/core/api/v1";
import {Router} from "@angular/router";
import {AppHelperService} from "@app/_helpers/app-helper.service";
import {FilterBarComponent} from "@app/_components/filter-bar/filter-bar.component";
import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
import {Sort} from "@angular/material/sort";
import {ROUTE_VESSELS, ROUTE_ZONES} from "@app/app-routing.module";
import {VesselFormComponent} from "@app/_views/vessel/vessel-form/vessel-form.component";

@Component({
  selector: 'app-vessel-list',
  templateUrl: './vessel-list.component.html',
  styleUrl: './vessel-list.component.scss'
})
export class VesselListComponent {
    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;

    protected readonly VesselFormComponent = VesselFormComponent;
    protected listColDefinitions!: ListColDefinition[];

    constructor(
        private vesselService: VesselService,
        private router: Router,
        protected appHelperService: AppHelperService,
    ) {

        this.listColDefinitions = [
            {
                name: 'name',
                text: 'common.name',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'code',
                text: 'common.code',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'code',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'shippingCompanyName',
                text: 'model.shipping_company',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'company',
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'createdAt',
                text: 'common.created_at',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'createdAt',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_DATE,
            } as ListColDefinition,
        ];
    }

    ngOnInit() {

    }

    ngAfterViewInit(): void {
        this.listComponent.getData();
    }

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

    getCustomDetailLink(element: ZoneJsonld) {
        return ROUTE_VESSELS + '/' + this.appHelperService.extractId(element?.id);
    }
}
