import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { PricingPlanUIService } from './openstack-plan-ui.service';
import { IPricingPlanModel } from '@fleio-api/openstack/model/pricing-plan.model';
import { IObjectListUIService } from '@objects-view/interfaces/object-list-ui-service';
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 { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { IAction } from '@objects-view/interfaces/actions/action';
import { MatDialog } from '@angular/material/dialog';

@Injectable({
  providedIn: 'root',
})
export class PricingPlanListUIService implements IObjectListUIService {
  public noItemsMessage = $localize`No OpenStack plans`;

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

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

  getTableData(objectList: FleioObjectsList<IPricingPlanModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name',
            flex: '30', flexXs: '70',
          },
          {
            type: ColumnType.Value, displayName: $localize`Currency`, enableSort: true, fieldName: 'currency',
          },
          {
            type: ColumnType.Value, displayName: $localize`Is default`, enableSort: false, fieldName: 'is_default',
            hide: 'xs',
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
            hide: 'xs',
          },
        ],
        columnNames: ['name', 'currency', 'is_default', '(actions)'],
      },
      rows: [],
    };

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

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