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 { HeatResourceTypesApiService } from '@fleio-api/openstack/orchestration/heat-resource-types/heat-resource-types-api.service';
import { HeatResourceTypeUiService } from './heat-resource-type-ui.service';
import { IHeatResourceTypeModel } from '@fleio-api/openstack/orchestration/model/heat-resource-type.model';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';

@Injectable({
  providedIn: 'root'
})
export class HeatResourceTypeListUiService implements IObjectListUIService {
  public noItemsMessage = $localize`No heat resource types`;

  private readonly datePipe: DateFormatterPipe;

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

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new HeatResourceTypeUiService(
      object as IHeatResourceTypeModel, permissions, state, this.router, this.config,
      this.heatResourceTypesApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IHeatResourceTypeModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: false, fieldName: 'name',
            // flex: '100%', flexXs: '1 0 100px'
          },
        ],
        columnNames: [
          'name',
        ],
        showStatus: true,
      },
      rows: [],
    };

    for (const heatResourceType of objectList.objects) {
      const rowUIService = this.getObjectUIService(heatResourceType, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          name: {text: heatResourceType.name},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: heatResourceType,
      };

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

    return tableData;
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [];
  }

  getRefreshInterval(): number {
    return 360000000;
  }
}
