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 { MessageTemplatesApiService } from '@fleio-api/message-templates/message-templates-api.service';
import { MessageTemplateUIService } from './message-template-u-i.service';
import { IMessageTemplateModel } from '@fleio-api/message-templates/model/message-template.model';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';

@Injectable({
  providedIn: 'root'
})
export class MessageTemplateListUIService implements IObjectListUIService {
  public noItemsMessage = $localize`No message templates`;

  constructor(
    private router: Router, private config: ConfigService,
    private messageTemplatesApiService: MessageTemplatesApiService,
  ) {
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new MessageTemplateUIService(
      object as IMessageTemplateModel, permissions, state, this.router, this.config,
      this.messageTemplatesApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IMessageTemplateModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: true, fieldName: 'name',
            flex: '400px', flexXs: '1 0 150px'
          },
          {
            type: ColumnType.Value, displayName: $localize`Category`, enableSort: false, fieldName: 'category',
            flex: '100px', flexXs: '90px',
          },
          {
            type: ColumnType.Value, displayName: $localize`Title`, enableSort: false, fieldName: 'title',
            flex: '1 0 300px',
            hide: {xs: true, sm: true},
          },
          {type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)'},
        ],
        columnNames: ['name', 'category', 'title', '(actions)'],
        showStatus: true,
      },
      rows: [],
    };

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

      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 message template`,
        tooltip: $localize`Create new message template`,
        icon: {name: 'add'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('settings/message-templates/create'),
        noPermissions: !objectList.permissions['messagetemplates.create'],
      })
    ];
  }

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