import {Component, Input, ViewChild} from '@angular/core';
import {ListComponent} from "@app/_components/list/list.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";
import {LocationJsonld, TripJsonld, UserJsonld, UserTripService} from "@app/core/api/v1";
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 {Observable} from "rxjs";
import {UserTripFormComponent} from "@app/_views/user-trip/user-trip-form/user-trip-form.component";
import {ROUTE_LOCATIONS, ROUTE_USER_TRIPS} from "@app/app-routing.module";

@Component({
  selector: 'app-user-trip-list',
  templateUrl: './user-trip-list.component.html',
  styleUrl: './user-trip-list.component.scss'
})
export class UserTripListComponent {
    @Input() public user?: UserJsonld;
    @Input() public trip?: TripJsonld;
    @Input() public showCreateButton: boolean;
    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;

    protected readonly userTripLocationFormComponent = UserTripFormComponent;
    protected listColDefinitions!: ListColDefinition[];
    protected dataFormComponentData: any;

    constructor(
        private userTripService: UserTripService,
        protected appHelperService: AppHelperService,
    ) {
        this.showCreateButton = false;
        this.listColDefinitions = [
            {
                name: 'pilotageReference',
                text: 'trip.pilot_reference',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'trip',
                field: 'pilotageReference',
                sortable: true,
                sortingSubResource: 'trip',
                sortingFieldName: 'id',
            } as ListColDefinition,
            {
                name: 'userFirstname',
                text: 'users.firstname',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'firstName',
                subResource: 'user',
                sortable: true,
                sortingSubResource: 'user',
                sortingFieldName: 'firstName',
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'userLastname',
                text: 'users.lastname',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'lastName',
                subResource: 'user',
                sortable: true,
                sortingSubResource: 'user',
                sortingFieldName: 'lastName',
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'captainName',
                text: 'trip.captain_name',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'captainName',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'completed',
                text: 'user_trip.completed',
                type: ListComponent.COLUMN_TYPE_BOOLEAN,
                field: 'completed',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_BOOLEAN,
            } as ListColDefinition,
            {
                name: 'approved',
                text: 'user_trip.approved',
                type: ListComponent.COLUMN_TYPE_BOOLEAN,
                field: 'approved',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_BOOLEAN,
            } as ListColDefinition,
            {
                name: 'completedDate',
                text: 'user_trip.completed_at',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'completedDate',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_DATE,
            } 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() {
        this.dataFormComponentData = {};
        if (this.trip) {
            this.dataFormComponentData['trip'] = this.trip;
        }
        if (this.user) {
            this.dataFormComponentData['user'] = this.user;
        }
    }

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

    getData: ListGetDataFunctionType = (
        index: number,
        pageSize: number,
        term?: string,
    ) => {
        return this.userTripService.userTripsGetCollection(
            index,
            pageSize,
            this.trip ? this.trip.id : undefined,
         undefined,
            this.user ? this.user.id : undefined,
         undefined,
            this.listComponent.getFilterJsonString(),
            this.listComponent.getSortingJsonString()
        );
    }

    get deleteItemFunction(): (id: string) => Observable<any> {
        return (id: string) => this.userTripService.userTripsIdDelete(id);
    }

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