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 { PeriodicTaskLogUiService } from './periodic-task-log-ui.service';
import { IPeriodicTaskLogModel } from '@fleio-api/logger/model/periodic-task-log.model';
import * as _moment from 'moment';

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

  private readonly datePipe: DateFormatterPipe;

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

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new PeriodicTaskLogUiService(
      object as IPeriodicTaskLogModel, permissions, state, this.config,
    );
  }

  getTableData(objectList: FleioObjectsList<IPeriodicTaskLogModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Task name`, enableSort: true, fieldName: 'task_name',
            flex: '150px', flexXs: '1 0 34%'
          },
          {
            type: ColumnType.Value, displayName: $localize`Start date`, enableSort: true, fieldName: 'start_dt',
            flex: '180px',
          },
          {
            type: ColumnType.Value, displayName: $localize`Duration`, enableSort: false, fieldName: 'duration',
            flex: '180px', hide: {xs: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Summary`, enableSort: false, fieldName: 'summary',
            hide: {xs: true, sm: true}, wrap: true,
          },
        ],
        columnNames: [
          'task_name', 'start_dt', 'duration', 'summary',
        ],
        showStatus: true,
      },
      rows: [],
    };

    for (const periodicTaskLog of objectList.objects) {
      const rowUIService = this.getObjectUIService(periodicTaskLog, objectList.permissions, 'table-view');
      let duration = 0;
      if (!!periodicTaskLog.end_dt && !!periodicTaskLog.start_dt) {
        // @ts-ignore
        duration = (new Date(periodicTaskLog.end_dt)) - (new Date(periodicTaskLog.start_dt));
      }

      const row: ITableRow = {
        cells: {
          task_name: {text: periodicTaskLog.task_name},
          start_dt: {text: this.datePipe.toDateTime(periodicTaskLog.start_dt)},
          duration: {text: duration > 0 ? _moment.duration(duration).humanize() : $localize`n/a`},
          summary: {text: periodicTaskLog.summary || $localize`n/a`},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: periodicTaskLog,
      };

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