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 { MatDialog } from '@angular/material/dialog';
import { LoadBalancersApiService } from '@fleio-api/openstack/load-balancing/load-balancers/load-balancers-api.service';
import { LoadBalancerUiService } from './load-balancer-ui.service';
import { ILoadBalancerModel } from '@fleio-api/openstack/load-balancing/model/load-balancer.model';

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

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

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

  getTableData(objectList: FleioObjectsList<ILoadBalancerModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`ID`, enableSort: false, fieldName: 'id',
            flex: '1 0 200px', flexXs: '45%',
          },
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: false, fieldName: 'name',
            flex: '1 0 100px', flexXs: '45%',
          },
          {
            type: ColumnType.Value, displayName: $localize`Operating status`, enableSort: false,
            fieldName: 'operating_status',
            flex: '1 0 100px',
            hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Provisioning status`, enableSort: false,
            fieldName: 'provisioning_status',
            flex: '1 0 100px',
            hide: {xs: true, sm: true, md: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Admin state up`, enableSort: false,
            fieldName: 'admin_state_up',
            flex: '1 0 50px',
            hide: {xs: true},
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
            hide: {sm: true},
          },
        ],
        columnNames: ['id', 'name', 'operating_status', 'provisioning_status', 'admin_state_up', '(actions)'],
        showStatus: true,
      },
      rows: [],
    };

    for (const loadBalancer of objectList.objects) {
      const rowUIService = this.getObjectUIService(loadBalancer, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          id: {text: `${loadBalancer.id}`},
          name: {text: loadBalancer.name},
          operating_status:{text: loadBalancer.operating_status},
          provisioning_status:{text: loadBalancer.provisioning_status},
          admin_state_up:{text: loadBalancer.admin_state_up ? $localize`Yes`: $localize`No`},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: loadBalancer,
      };

      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 load balancer`,
        tooltip: objectList.disableCreate ? $localize`Load balancer creation not supported for this region` : $localize`Create new load balancer`,
        icon: {name: 'add'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('openstack/load-balancers/create'),
        propagateQueryParams: ['region'],
        disabled: objectList.disableCreate,
      })
    ];
  }

  getRefreshInterval(): number {
    return this.config.getDefaultRefreshInterval(false);
  }
}
