import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { IObjectListUIService } from '@objects-view/interfaces/object-list-ui-service';
import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { ConfigService } from '@shared/config/config.service';
import { IObjectUIService } from '@objects-view/interfaces/object-ui-service';
import { ITableData } from '@objects-view/interfaces/table-data/table-data';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { ITableRow } from '@objects-view/interfaces/table-data/table-row';
import { ColumnType } from '@objects-view/interfaces/table-data/column-definition';
import { IAction } from '@objects-view/interfaces/actions/action';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
import { TaxRulesApiService } from '@fleio-api/billing/tax-rules/tax-rules-api.service';
import { TaxRuleUIService } from './tax-rule-ui.service';
import { ITaxRuleModel } from '@fleio-api/billing/model/tax-rule.model';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';

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

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

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new TaxRuleUIService(
      object as ITaxRuleModel, permissions, state, this.router, this.config, this.taxRulesApiService,
    );
  }

  getTableData(objectList: FleioObjectsList<ITaxRuleModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Name`, enableSort: false, fieldName: 'name',
            flex: '1 0 200px', flexXs: '1 0 60px'
          },
          {
            type: ColumnType.Value, displayName: $localize`Level`, enableSort: true, fieldName: 'level',
            flex: '60px', hide: { xs: true}
          },
          {
            type: ColumnType.Value, displayName: $localize`Country`, enableSort: true, fieldName: 'country',
            flex: '1 0 130px', flexXs: '1 0 80px'
          },
          {
            type: ColumnType.Value, displayName: $localize`State`, enableSort: true, fieldName: 'state',
            flex: '1 0 80px',
            hide: { xs: true}
          },
          {
            type: ColumnType.Value, displayName: $localize`Rate`, enableSort: true, fieldName: 'rate',
            flex: '1 0 80px',
            hide: { xs: true, sm: true}
          },
          {
            type: ColumnType.Value, displayName: $localize`Start date`, enableSort: true, fieldName: 'start_date',
            flex: '1 0 80px',
            hide: { xs: true, sm: true}
          },
          {
            type: ColumnType.Value, displayName: $localize`End date`, enableSort: true, fieldName: 'end_date',
            flex: '1 0 80px',
            hide: { xs: true, sm: true}
          },
          {
            type: ColumnType.Actions, displayName: '', enableSort: false, fieldName: '(actions)',
          },
        ],
        columnNames: ['name', 'level', 'country', 'state', 'rate', 'start_date', 'end_date', '(actions)'],
        showStatus: false,
      },
      rows: [],
    };

    for (const object of objectList.objects) {
      const rowUIService = this.getObjectUIService(object, objectList.permissions, 'table-view');
      let endDate = '';
      if (object.end_date) {
        endDate = this.datePipe.transform(object.end_date);
      }
      let startDate = '';
      if (object.start_date) {
        startDate = this.datePipe.transform(object.start_date);
      }
      const row: ITableRow = {
        cells: {
          name: {text: object.name},
          level: {text: object.level.toString()},
          country: {text: object.country},
          state: {text: object.state},
          rate: {text: object.rate.toString()},
          start_date: {
            text: startDate,
            title: object.start_date ? this.datePipe.getTitleAttributeValue(object.start_date) : $localize`n/a`,
          },
          end_date: {
            text: endDate,
            title: object.end_date ? this.datePipe.getTitleAttributeValue(object.end_date) : $localize`n/a`,
          },
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object,
      };

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

    return tableData;
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [
      new RouterLinkAction({
        icon: {name: 'add'},
        name: $localize`Create`,
        tooltip: $localize`Create new tax rule`,
        noPermissions: !objectList.permissions['taxrules.create'],
        routerUrl: this.config.getPanelUrl(`billing/tax-rules/create`),
        router: this.router,
      }),
    ];
  }

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