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 { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
import { EmailMessageLogApiService } from '@fleio-api/logger/emai-message-log-api.service';
import { EmailMessageLogUiService } from './email-message-log-ui.service';
import { IEmailMessageLogModel } from '@fleio-api/logger/model/email-message-log.model';

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

  private readonly datePipe: DateFormatterPipe;

  constructor(
    private router: Router, private config: ConfigService,
    private emailMessageLogApiService: EmailMessageLogApiService,
  ) {
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new EmailMessageLogUiService(
      object as IEmailMessageLogModel, permissions, state, this.router, this.config,
      this.emailMessageLogApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<IEmailMessageLogModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Subject`, enableSort: true, fieldName: 'subject',
            flex: '200px', flexXs: '1 0 20%'
          },
          {
            type: ColumnType.Value, displayName: $localize`Sent to`, enableSort: true, fieldName: 'to',
            flex: 'auto'
          },
          {
            type: ColumnType.Value, displayName: $localize`Sent on`, enableSort: true, fieldName: 'created_at',
            flex: '180px', hide: {xs: true}
          },
          {
            type: ColumnType.Value, displayName: $localize`Client`, enableSort: true, fieldName: 'client',
            flex: '250px', hide: {xs: true, sm: true}
          },
          {
            type: ColumnType.Value, displayName: $localize`User`, enableSort: true, fieldName: 'user',
            flex: '250px', hide: {xs: true, sm: true, md: true,}
          },
        ],
        columnNames: [
          'subject', 'to', 'created_at', 'client', 'user',
        ],
        showStatus: true,
      },
      rows: [],
    };

    for (const emailMessageLog of objectList.objects) {
      const rowUIService = this.getObjectUIService(emailMessageLog, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          subject: {text: emailMessageLog.subject},
          to: {text: emailMessageLog.to},
          created_at: {text: this.datePipe.toDateTime(emailMessageLog.created_at)},
          client: {text: emailMessageLog.client?.display || $localize`n/a`},
          user: {text: emailMessageLog.user ? (emailMessageLog.user.full_name || emailMessageLog.user.email) : $localize`n/a`},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: emailMessageLog,
      };

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

    return tableData;
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [];
  }

  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;
  }
}
