import {Component, Input, Type, ViewChild} from '@angular/core';
import {ListComponent} from "@app/_components/list/list.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";
import {TripJsonld, TripLocationService} 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 {TripLocationFormComponent} from "@app/_views/trip/trip-location-form/trip-location-form.component";
import {map, tap} from "rxjs/operators";

@Component({
  selector: 'app-trip-location-list',
  templateUrl: './trip-location-list.component.html',
  styleUrl: './trip-location-list.component.scss'
})
export class TripLocationListComponent {

    @Input() public trip!: TripJsonld;
    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;

    protected tripLocationFormComponent = TripLocationFormComponent;
    protected listColDefinitions!: ListColDefinition[];
    protected dataFormComponentData: any;

    constructor(
        private tripLocationService: TripLocationService,
        protected appHelperService: AppHelperService,
    ) {
        this.listColDefinitions = [
            {
                name: 'location',
                text: 'model.location',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'location',
                field: 'name',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'zone',
                text: 'model.zone',
                type: ListComponent.COLUMN_TYPE_TEXT,
                subResource: 'location',
                field: 'zoneName',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
            {
                name: 'arrivalDateTime',
                text: 'trip_location.arrival_date_time',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'arrivalDateTime',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_DATE,
            } as ListColDefinition,
            {
                name: 'departureDateTime',
                text: 'trip_location.departure_date_time',
                type: ListComponent.COLUMN_TYPE_DATE,
                field: 'departureDateTime',
                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 = {
            trip: this.trip,
            arrivalDateTime: new Date().toISOString(),
            departureDateTime: new Date(new Date().setHours(new Date().getHours() + 1))
        };
    }

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

    getData: ListGetDataFunctionType = (
        index: number,
        pageSize: number,
        term?: string,
    ) => {
        return this.tripLocationService.tripLocationsGetCollection(
            index,
            pageSize,
            this.trip.id,
            undefined,
            this.listComponent.getFilterJsonString(),
            this.listComponent.getSortingJsonString()
        ).pipe(
            map(response => {
                // Set default arrival and departure time before return
                let arrivalDateTime = new Date().toISOString();
                let departureDateTime = new Date(new Date().setHours(new Date().getHours() + 1)).toISOString();
                if (response.member && response.member.length > 0) {
                    const lastEntry = response.member[response.member.length - 1];
                    if (lastEntry.departureDateTime) {
                        const departureDate = new Date(lastEntry.departureDateTime);
                        departureDate.setDate(departureDate.getDate() + 1);
                        arrivalDateTime = departureDate.toISOString();

                        const arrivalDate = new Date(arrivalDateTime);
                        arrivalDate.setHours(arrivalDate.getHours() + 1);
                        departureDateTime = arrivalDate.toISOString();
                    }
                }
                this.dataFormComponentData = {
                    trip: this.trip,
                    arrivalDateTime: arrivalDateTime,
                    departureDateTime: departureDateTime

                };

                return response;
            })
        );
    }
}
