import { Injectable } from '@angular/core';
import { IObjectListUIService } from '@objects-view/interfaces/object-list-ui-service';
import { ConfigService } from '@shared/config/config.service';
import { MatDialog } from '@angular/material/dialog';
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 { CallbackAction } from '@objects-view/actions/callback-action';
import { map } from 'rxjs/operators';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { VolumeSnapshotsApiService } from '@fleio-api/openstack/volume/volume-snapshots-api.service';
import { VolumeSnapshotUiService } from './volume-snapshot-ui.service';
import { IVolumeSnapshotModel } from '@fleio-api/openstack/model/volume-snapshot.model';
import { EditVolumeSnapshotDialogComponent } from '@shared/common-dialogs/openstack/volume-snapshots/edit-volume-snapshot-dialog/edit-volume-snapshot-dialog.component';

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

  constructor(
    private config: ConfigService,
    private volumeSnapshotsApiService: VolumeSnapshotsApiService,
    private matDialog: MatDialog,
  ) {
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new VolumeSnapshotUiService(
      object as IVolumeSnapshotModel, permissions, state, this.config, this.volumeSnapshotsApiService,
      this.matDialog,
    );
  }

  getTableData(objectList: FleioObjectsList<IVolumeSnapshotModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name',
            flex: '1 0 400px', flexXs: '95%',
          },
          {
            type: ColumnType.Value, displayName: $localize`Status`, enableSort: true, fieldName: 'status',
            flex: '100px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Size`, enableSort: true, fieldName: 'size',
            flex: '80px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Volume`, enableSort: false, fieldName: 'volume',
            flex: '180px', hide: {xs: true, sm: true, md: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Client`, enableSort: false, fieldName: 'client',
            flex: '180px', hide: {xs: true, sm: true, md: true},
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
            hide: 'xs',
          },
        ],
        columnNames: ['name', 'status', 'size', 'volume', 'client', '(actions)'],
        showStatus: true,
      },
      rows: [],
    };

    for (const volumeSnapshot of objectList.objects) {
      const rowUIService = this.getObjectUIService(volumeSnapshot, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          name: {text: volumeSnapshot.name || volumeSnapshot.id.toString()},
          status: {text: volumeSnapshot.status || ''},
          size: {text: volumeSnapshot.size ? $localize`${volumeSnapshot.size} GB` : ''},
          volume: {text: volumeSnapshot.volume ? volumeSnapshot.volume.toString() : $localize`n/a`},
          client: {
            text: volumeSnapshot.client ? volumeSnapshot.client.display : $localize`n/a`,
            url: volumeSnapshot.client ? this.config.getPanelUrl(
              `clients-users/clients/${volumeSnapshot.client.id}`,
            ) : null,
          },
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: volumeSnapshot,
      };

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

    return tableData;
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [
      new CallbackAction(
        {
          object: null,
          icon: {name: 'add'},
          tooltip: $localize`Create volume snapshot`,
          name: $localize`Create`,
          noPermissions: !objectList.permissions['volumesnapshots.create'],
          callback: () => this.matDialog.open(
              EditVolumeSnapshotDialogComponent, {
                data: {},
                autoFocus: false,
              }).afterClosed().pipe(map(result => {
              if (!result) {
                return;
              }
              return {message: $localize`Snapshot create scheduled`} as IActionResult;
            })),
          refreshAfterExecute: true,
        }
      )
    ];
  }

  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;
  }
}
