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 { HypervisorUiService } from './hypervisor-ui.service';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
import { HypervisorApiService } from '@fleio-api/openstack/hypervisor/hypervisor-api.service';
import { IHypervisorModel } from '@fleio-api/openstack/model/hypervisor.model';

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

  private readonly datePipe: DateFormatterPipe;

  constructor(
    private router: Router, private config: ConfigService,
    private hypervisorApiService: HypervisorApiService,
  ) {
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new HypervisorUiService(
      object as IHypervisorModel, permissions, state, this.router, this.config, this.hypervisorApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IHypervisorModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'host_name',
            flex: '200px',
          },
          {
            type: ColumnType.Value, displayName: $localize`Status`, enableSort: true, fieldName: 'status',
            flex: '100px',
          },
          {
            type: ColumnType.Value, displayName: $localize`State`, enableSort: true, fieldName: 'state',
            flex: '100px',
          },
          {
            type: ColumnType.Value, displayName: $localize`Region`, enableSort: true, fieldName: 'region',
            flex: '150px', hide: {xs: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Host IP`, enableSort: true, fieldName: 'host_ip',
            flex: '150px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Type`, enableSort: false, fieldName: 'type',
            flex: '150px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Actions, displayName: '', enableSort: false, fieldName: '(actions)',
          },
        ],
        columnNames: ['host_name', 'status', 'state', 'region', 'host_ip', 'type', '(actions)'],
        showStatus: true,
      },
      rows: [],
    };

    for (const object of objectList.objects) {
      const rowUIService = this.getObjectUIService(object, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          host_name: {text: object.host_name},
          status: {text: object.status},
          state: {text: object.state},
          region: {text: object.region},
          host_ip: {text: object.host_ip},
          type: {text: object.hypervisor_type},
        },
        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 [];
  }

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