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 { HeatStacksApiService } from '@fleio-api/openstack/orchestration/heat-stacks/heat-stacks-api.service';
import { HeatStackUiService } from './heat-stack-ui.service';
import { IHeatStackModel } from '@fleio-api/openstack/orchestration/model/heat-stack.model';
import { FormatHelper } from '@shared/ui-api/helpers/format-helper';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';

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

  private readonly datePipe: DateFormatterPipe;
  private readonly formatHelper: FormatHelper;

  constructor(
    private router: Router, private config: ConfigService,
    private heatStacksApiService: HeatStacksApiService,
  ) {
    this.datePipe = new DateFormatterPipe(this.config);
    this.formatHelper = new FormatHelper();
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new HeatStackUiService(
      object as IHeatStackModel, permissions, state, this.router, this.config, this.heatStacksApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IHeatStackModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name',
            flex: '150px', flexXs: '1 0 100px'
          },
          {
            type: ColumnType.Value, displayName: $localize`Status`, enableSort: true, fieldName: 'status',
            flex: '150px', flexXs: '1 0 100px'
          },
          {
            type: ColumnType.Value, displayName: $localize`Created`, enableSort: true, fieldName: 'created_at',
            hide: {xs: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Updated`, enableSort: true, fieldName: 'updated_time',
            hide: {xs: true, sm: true, md: true},
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
          },
        ],
        columnNames: [
          'name', 'status', 'created_at', 'updated_time', '(actions)'
        ],
        showStatus: true,
      },
      rows: [],
    };

    for (const heatStack of objectList.objects) {
      const rowUIService = this.getObjectUIService(heatStack, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          name: {text: heatStack.stack_name || heatStack.id as string},
          status: {text: this.formatHelper.formatStatus(heatStack.stack_status)},
          created_at: {
            text: this.datePipe.transform(heatStack.created_at),
            title: this.datePipe.getTitleAttributeValue(heatStack.created_at),
          },
          updated_time: {
            text: heatStack.updated_time ? this.datePipe.transform(heatStack.created_at) : $localize`n/a`,
            title: heatStack.updated_time ? this.datePipe.getTitleAttributeValue(heatStack.created_at) : $localize`n/a`
          },
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: heatStack,
      };

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

    return tableData;
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [
      new RouterLinkAction({
        name: $localize`Preview heat stack`,
        tooltip: $localize`Preview heat stack`,
        icon: {name: 'preview'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('openstack/heat-stacks/preview'),
      }),
      new RouterLinkAction({
        name: $localize`Create heat stack`,
        tooltip: $localize`Create heat stack`,
        icon: {name: 'add'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('openstack/heat-stacks/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;
  }
}
