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 { RbacPolicyUiService } from './rbac-policy.ui.service';
import { IRBACPolicyModel } from '@fleio-api/openstack/model/rbac-policy.model';
import { RbacPoliciesApiService } from '@fleio-api/openstack/rbac-policy/rbac-policies-api.service';

@Injectable({
  providedIn: 'root'
})
export class RbacPoliciesListUiService implements IObjectListUIService {
  public noItemsMessage = $localize`No RBAC policies`;

  constructor(
    private router: Router, private config: ConfigService,
    private rbacPoliciesApiService: RbacPoliciesApiService,
  ) {
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new RbacPolicyUiService(
      object as IRBACPolicyModel, permissions, state, this.router, this.config, this.rbacPoliciesApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IRBACPolicyModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Project`, enableSort: true, fieldName: 'project',
            flex: '1 0 200px',
          },
          {
            type: ColumnType.Value, displayName: $localize`Target project`, enableSort: true,
            fieldName: 'target_project',
            flex: '1 0 150px',
          },
          {
            type: ColumnType.Value, displayName: $localize`Object`, enableSort: true,
            fieldName: 'object_type',
            flex: '1 0 150px',
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
          },
        ],
        columnNames: ['project', 'target_project', 'object_type', '(actions)'],
        showStatus: false,
      },
      rows: [],
    };

    for (const rbacPolicy of objectList.objects) {
      const rowUIService = this.getObjectUIService(
        rbacPolicy, objectList.permissions, 'table-view'
      ) as RbacPolicyUiService;

      let objectUrl: string = null;
      switch (rbacPolicy.object_type) {
        case 'network':
          objectUrl = this.config.getPanelUrl(`openstack/networks/${rbacPolicy.object_id}`);
          break;
        case 'subnetpool':
          objectUrl = this.config.getPanelUrl(`openstack/subnet-pools/${rbacPolicy.object_id}`);
          break;
      }

      const row: ITableRow = {
        cells: {
          project: {
            text: `${rbacPolicy.fleio_project.name}`,
          },
          target_project: {
            text: `${rbacPolicy.target_project}`,
            subText: `${rbacPolicy.action_display}`
          },
          object_type: {
            text: `${rbacPolicy.object_type}`,
            subText: `${rbacPolicy.object_display}`,
            subTextLink: objectUrl,
          }
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: rbacPolicy,
      };

      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 new RBAC policy`,
        tooltip: $localize`Create new RBAC policy`,
        icon: {name: 'add'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('openstack/rbac-policies/create'),
        noPermissions: !objectList.permissions['rbacpolicies.create'],
      }),
    ];
  }

  getRefreshInterval(): number {
    return this.config.getDefaultRefreshInterval();
  }
}
