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 {TripJsonld, UserTripEventService, UserTripJsonld} 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 {
    UserTripEventFormComponent
} from "@app/_views/user-trip-event/user-trip-event-form/user-trip-event-form.component";
import {Observable} from "rxjs";

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

    protected readonly userTripEventFormComponent = UserTripEventFormComponent;
    protected listColDefinitions!: ListColDefinition[];
    protected dataFormComponentData: any;

    constructor(
        private userTripEventService: UserTripEventService,
        protected appHelperService: AppHelperService,
    ) {
        this.listColDefinitions = [
            {
                name: 'eventName',
                text: 'model.event',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                subResource: 'event',
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'user',
                text: 'model.user',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'user',
                field: 'fullName',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'location',
                text: 'model.location',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'name',
                subResource: 'location',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'zone',
                text: 'model.zone',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'zoneName',
                subResource: 'location',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'date',
                text: 'basic.date',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'date',
                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,
        ];
        this.showCreateButton = true;
    }

    ngOnInit() {
        this.dataFormComponentData = {
            userTrip: this.userTrip,
        };
    }

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

    getData: ListGetDataFunctionType = (
        index: number,
        pageSize: number,
        term?: string,
    ) => {
        return this.userTripEventService.userTripEventsGetCollection(
            index,
            pageSize,
            this.userTrip !== undefined ? this.userTrip.id! : undefined,
            undefined,
            this.trip !== undefined ? this.trip.dbId! : undefined,
            this.listComponent.getFilterJsonString(),
            this.listComponent.getSortingJsonString()
        );
    }

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