import {Component, Input, ViewChild} from '@angular/core';
import {UserJsonld, UserZoneService, ZoneJsonld} from "@app/core/api/v1";
import {ListComponent} from "@app/_components/list/list.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";
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 {UserZoneFormComponent} from "@app/_views/user-zone/user-zone-form/user-zone-form.component";

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

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

    constructor(
        private userZoneService: UserZoneService,
        protected appHelperService: AppHelperService,
    ) {
        this.showCreateButton = true;
        this.listColDefinitions = [
            {
                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: 'zoneName',
                text: 'model.zone',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'name',
                subResource: 'zone',
                sortable: true,
                sortingSubResource: 'zone',
                sortingFieldName: 'name',
                filterType: FilterBarComponent.FILTER_TYPE_TEXT,
            } as ListColDefinition,
        ];
    }

    ngOnInit() {
        this.dataFormComponentData = {};
        if (this.zone) {
            this.dataFormComponentData['zone'] = this.zone;
        }
        if (this.user) {
            this.dataFormComponentData['user'] = this.user;
        }
    }

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

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

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