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 { IAction } from '@objects-view/interfaces/actions/action';
import { ITableRow } from '@objects-view/interfaces/table-data/table-row';
import { DomainApiService } from '@fleio-api/plugins/domains/domain-api.service';
import { DomainUiService } from './domain-ui.service';
import { IDomainModel } from '@fleio-api/plugins/domains/model/domain.model';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
import { ServicesApiService } from '@fleio-api/billing/services/service-api.service';

@Injectable({
  providedIn: 'root'
})
export class DomainListUiService implements IObjectListUIService {
  public noItemsMessage = $localize`No domains`;
  private datePipe: DateFormatterPipe;

  constructor(
    private router: Router, private config: ConfigService,
    private domainApiService: DomainApiService,
    private servicesApiService: ServicesApiService,
  ) {
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new DomainUiService(
      object as IDomainModel, permissions, state, this.router, this.config,
      this.domainApiService, this.servicesApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IDomainModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name',
            flex: '400px', flexXs: '80',
          },
          {
            type: ColumnType.Value, displayName: $localize`Created at`, enableSort: true, fieldName: 'created_at',
            flex: '180px', hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Status`, enableSort: true, fieldName: 'status',
            hide: {xs: true, sm: true},
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)',
          },
        ],
        columnNames: ['name', 'created_at', 'status', '(actions)'],
        showStatus: true,
      },
      rows: [],
    };

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

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

    return tableData;
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [
      new RouterLinkAction({
        name: $localize`Register domain`,
        tooltip: $localize`Register domain`,
        icon: {name: 'add'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('domains/register-domain'),
      })
    ];
  }

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