import { Injectable } from '@angular/core';
import { IObjectListUIService } from '@objects-view/interfaces/object-list-ui-service';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { IObjectUIService } from '@objects-view/interfaces/object-ui-service';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { ITableData } from '@objects-view/interfaces/table-data/table-data';
import { ColumnType } from '@objects-view/interfaces/table-data/column-definition';
import { ITableRow } from '@objects-view/interfaces/table-data/table-row';
import { IAction } from '@objects-view/interfaces/actions/action';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { RolesApiService } from '@fleio-api/client-user/roles/roles-api.service';
import { RoleUiService } from './role-ui.service';
import { IRoleModel } from '@fleio-api/client-user/roles/model/role.model';
import { YesNoPipe } from '@shared/ui/common/pipes/yes-no.pipe';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';

@Injectable({
  providedIn: 'root'
})
export class RoleListUiService implements IObjectListUIService {
  public noItemsMessage = $localize`No roles`;
  private datePipe: DateFormatterPipe;
  private yesNoPipe: YesNoPipe;

  constructor(
    private router: Router, private config: ConfigService,
    private rolesApiService: RolesApiService,
  ) {
    this.datePipe = new DateFormatterPipe(this.config);
    this.yesNoPipe = new YesNoPipe();
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new RoleUiService(
      object as IRoleModel, permissions, state, this.router, this.config, this.rolesApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IRoleModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name',
            flex: '1 0 150px', flexXs: '70',
          },
          {
            type: ColumnType.Value, displayName: $localize`Public`, enableSort: false, fieldName: 'public',
            flex: '50px', hide: {xs: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Created at`, enableSort: true, fieldName: 'created_at',
            flex: '100px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Updated at`, enableSort: true, fieldName: 'updated_at',
            flex: '100px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Parent`, enableSort: true, fieldName: 'parent',
            flex: '1 0 150px', hide: {xs: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Owner`, enableSort: true, fieldName: 'owner',
            flex: '1 0 150px', flexXs: '70',
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
          },
        ],
        columnNames: ['name', 'public', 'created_at', 'updated_at', 'parent', 'owner', '(actions)'],
        showStatus: true,
      },
      rows: [],
    };

    for (const role of objectList.objects) {
      const rowUIService = this.getObjectUIService(role, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          name: {text: role.name},
          public: {text: this.yesNoPipe.transform(role.public)},
          created_at: {
            text: this.datePipe.transform(role.created_at),
            title: this.datePipe.getTitleAttributeValue(role.created_at)
          },
          updated_at: {
            text: this.datePipe.transform(role.updated_at),
            title: this.datePipe.getTitleAttributeValue(role.updated_at)
          },
          parent: {text: role.parent_display},
          owner: {text: role.owner_display},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: role,
      };

      tableData.trackByFunction = (index, item: ITableRow) => item.object.id;
      tableData.rows.push(row);
    }

    return tableData;
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [
      new RouterLinkAction({
        name: $localize`Create role`,
        tooltip: $localize`Create role`,
        icon: {name: 'add'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('clients-users/roles/create')
      })
    ];
  }

  getRefreshInterval(): number {
    if (this.config && this.config.current && this.config.current.settings &&
      this.config.current.settings.refreshIntervals) {
      return this.config.current.settings.refreshIntervals.defaultInterval;
    }
    return 10000;
  }
}
