import { Injectable } from '@angular/core';
import { IObjectListUIService } from '@objects-view/interfaces/object-list-ui-service';
import { ConfigService } from '@shared/config/config.service';
import { Router } from '@angular/router';
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 { ClustersApiService } from '@fleio-api/openstack/cluster/clusters-api.service';
import { ClusterUiService } from './cluster-ui.service';
import { IClusterModel } from '@fleio-api/openstack/model/cluster.model';
import { MatDialog } from '@angular/material/dialog';
import { ApiUsersApiService } from '@fleio-api/openstack/api-user/api-users-api.service';
import { NotificationService } from '@shared/ui-api/notification.service';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';

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

  constructor(
    private router: Router, private config: ConfigService,
    private clustersApiService: ClustersApiService, private matDialog: MatDialog,
    private apiUsersApiService: ApiUsersApiService, private notificationService: NotificationService,
  ) {
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new ClusterUiService(
      object as IClusterModel, permissions, state, this.router, this.config, this.clustersApiService,
      this.matDialog, this.apiUsersApiService, this.notificationService,
    );
  }

  getTableData(objectList: FleioObjectsList<IClusterModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name', flex: '1 0 180px',
            flexXs: '110px'
          },
          {
            type: ColumnType.Value, displayName: $localize`Status`, enableSort: true, fieldName: 'display_status',
             hide: {xs: true, sm: true}
          },
          {
            type: ColumnType.Value, displayName: $localize`Cluster template`, enableSort: false,
            fieldName: 'cluster_template',
             hide: {sm: true, xs: true}
          },
          { type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)' },
        ],
        columnNames: ['name', 'display_status', 'cluster_template', '(actions)'],
        showStatus: true,
        visibleActions: 3,
      },
      rows: [],
    };

    for (const object of objectList.objects) {
      const rowUIService = this.getObjectUIService(object, objectList.permissions, 'table-view');
      let template = '';
      if (object.cluster_template) {
        template = object.cluster_template.name || object.cluster_template.id as string;
      } else {
        template = $localize`n/a`;
      }
      const row: ITableRow = {
        cells: {
          name: {text: object.name},
          display_status: {text: object.display_status},
          cluster_template: {text: template},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object,
      };

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