import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {Sort} from "@angular/material/sort";
import {UserJsonld, UserService} from "@app/core/api/v1";
import {OrderFilter} from "@app/_models/orderFilter";
import {Router} from "@angular/router";
import {AppHelperService} from "@app/_helpers/app-helper.service";
import {ListComponent} from "@app/_components/list/list.component";
import {ListColDefinition} from "@app/_components/list/list-col-definition";
import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
import {FilterBarComponent} from "@app/_components/filter-bar/filter-bar.component";
import {UserFormComponent} from "@app/_views/user/user-form/user-form.component";

@Component({
    selector: 'app-user-list',
    templateUrl: './user-list.component.html',
    styleUrl: './user-list.component.scss'
})
export class UserListComponent implements OnInit, AfterViewInit {

    @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;

    protected readonly userFormComponent = UserFormComponent;
    protected firstNameOrderFilter: OrderFilter;
    protected lastNameOrderFilter: OrderFilter;
    protected listColDefinitions!: ListColDefinition[];

    constructor(
        private userService: UserService,
        private router: Router,
        protected appHelperService: AppHelperService,
    ) {
        this.firstNameOrderFilter = OrderFilter.Undefined;
        this.lastNameOrderFilter = OrderFilter.Asc;

        this.listColDefinitions = [
            {
                name: 'image',
                text: 'basic.image',
                type: ListComponent.COLUMN_TYPE_IMAGE,
                field: 'imageUrl',
            } as ListColDefinition,
            {
                name: 'firstName',
                text: 'users.firstname',
                type: ListComponent.COLUMN_TYPE_TEXT,
                field: 'firstName',
                sortable: true,
            } as ListColDefinition,
            {
                name: 'lastName',
                text: 'users.lastname',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'lastName',
                sortable: true,
            } as ListColDefinition,
            {
                name: 'email',
                text: 'users.email',
                type: ListComponent.COLUMN_TYPE_EMAIL,
                field: 'email',
            } as ListColDefinition,
            {
                name: 'referenceId',
                text: 'users.pilotIdNo',
                type: ListComponent.COLUMN_TYPE_TEXT_BOLD,
                field: 'referenceId',
            } as ListColDefinition,
            {
                name: 'isAdmin',
                text: 'users.is_admin',
                type: ListComponent.COLUMN_TYPE_BOOLEAN,
                field: 'isAdmin',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_BOOLEAN,
            } as ListColDefinition,
            {
                name: 'isPilot',
                text: 'users.is_pilot',
                type: ListComponent.COLUMN_TYPE_BOOLEAN,
                field: 'isPilot',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_BOOLEAN,
            } as ListColDefinition,
            {
                name: 'active',
                text: 'users.active',
                type: ListComponent.COLUMN_TYPE_BOOLEAN,
                field: 'active',
                sortable: true,
                filterType: FilterBarComponent.FILTER_TYPE_BOOLEAN,
            } as ListColDefinition,
        ];
    }

    ngOnInit() {

    }

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

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

    onSortChange = (sortState: Sort) => {
        this.firstNameOrderFilter = OrderFilter.Undefined;
        this.lastNameOrderFilter = OrderFilter.Undefined;
        switch (sortState.active) {
            case "firstName":
                this.firstNameOrderFilter = sortState.direction as OrderFilter;
                break;
            case "lastName":
                this.lastNameOrderFilter = sortState.direction as OrderFilter;
                break;
        }
    }

    navigateToUserDetails = (element: any) => {
        const user: UserJsonld = element as UserJsonld;
        this.router.navigate(['/user', this.appHelperService.extractId(user.id)]);
    }

}
