import { Injectable } from '@angular/core';
import { IObjectListUIService } from '@objects-view/interfaces/object-list-ui-service';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
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 { ContainersApiService } from '@fleio-api/openstack/object-store/containers/containers-api.service';
import { ContainerUiService } from './container-ui.service';
import { IContainerModel } from '@fleio-api/openstack/object-store/model/container.model';
import { FormatHelper } from '@shared/ui-api/helpers/format-helper';

@Injectable({
  providedIn: 'root'
})
export class ContainerListUiService implements IObjectListUIService {
  public noItemsMessage = $localize`No containers`;
  private datePipe: DateFormatterPipe;
  private formatHelper = new FormatHelper();

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

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new ContainerUiService(
      object as IContainerModel, permissions, state, this.router, this.config, this.containersApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IContainerModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name',
            flex: '1 0 150px', flexXs: '70',
          },
          {
            type: ColumnType.Value, displayName: $localize`Object count`, enableSort: true, fieldName: 'count',
            flex: '270px', hide: {xs: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Size`, enableSort: true, fieldName: 'size' +
              '',
            flex: '1 0 70px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
          },
        ],
        columnNames: ['name', 'count', 'size', '(actions)'],
        showStatus: true,
      },
      rows: [],
    };

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

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

    return tableData;
  }

  // noinspection JSUnusedLocalSymbols
  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [
      new RouterLinkAction({
        name: $localize`Create container`,
        tooltip: objectList.disableCreate ? $localize`Container creation not supported for this region` : $localize`Create container`,
        icon: {name: 'add'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('openstack/object-store/create'),
        propagateQueryParams: ['region'],
        disabled: objectList.disableCreate,
      })
    ];
  }

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