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 { VolumesApiService } from '@fleio-api/openstack/volume/volumes-api.service';
import { VolumeUiService } from './volume-ui.service';
import { IVolumeModel } from '@fleio-api/openstack/model/volume.model';
import { MatDialog } from '@angular/material/dialog';

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

  constructor(
    private router: Router, private config: ConfigService,
    private volumesApiService: VolumesApiService,
    private matDialog: MatDialog,
  ) {
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new VolumeUiService(
      object as IVolumeModel, permissions, state, this.router, this.config, this.volumesApiService,
      this.matDialog,
    );
  }

  getTableData(objectList: FleioObjectsList<IVolumeModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name',
            flex: '1 0 200px', flexXs: '1 0 140px'
          },
          {
            type: ColumnType.Value, displayName: $localize`Region`, enableSort: false, fieldName: 'region',
            flex: '1 0 110px',
          },
          {
            type: ColumnType.Value, displayName: $localize`Avail. zone`, enableSort: true,
            fieldName: 'availability_zone', flex: '1 0 120px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Size`, enableSort: false, fieldName: 'size',
            flex: '1 0 70px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
            flex: '0 0 220px',
          },
        ],
        columnNames: ['name', 'region', 'availability_zone', 'size', '(actions)'],
        showStatus: true,
        visibleActions: 3,
      },
      rows: [],
    };

    for (const volume of objectList.objects) {
      const rowUIService = this.getObjectUIService(volume, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          name: {text: volume.name || volume.id.toString()},
          region: {text: volume.region || ''},
          availability_zone: {text: volume.availability_zone || ''},
          size: {text: volume.size ? $localize`${volume.size} GB` : ''},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: volume,
      };

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