import { Injectable } from '@angular/core';
import { CommonJournalListUiService } from '@common-object-list-ui/billing/journal/common-journal-list-ui.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 { IJournalEntryModel } from '@fleio-api/billing/model/journal-entry.model';
import { ColumnType } from '@objects-view/interfaces/table-data/column-definition';
import { ITableData } from '@objects-view/interfaces/table-data/table-data';
import { JournalUiService } from '../../billing/journal/journal-ui.service';

@Injectable({
  providedIn: 'root',
})
export class ClientDetailsJournalListUiService extends CommonJournalListUiService {
  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new JournalUiService(
      object as IJournalEntryModel, permissions, state, this.router, this.config,
      this.journalsApiService, this.matDialog, this.gatewayApiService,
      this.notificationService, this.journalEditTrackerService,
    );
  }

  getCells(journalEntry, sourceInfoSubText, sourceInfoSubTextLink, destinationInfoSubText, destinationInfoSubTextLink,
           destinationAmount) {
    const creditLeftDisplay = this.currencyPipe.transform(
      journalEntry.client_credit_left,
      journalEntry.client_credit_left_currency,
      this.config.authService.currencyDisplayMode,
    );
    return {
      date_added: {
        text: `${this.datePipe.transform(journalEntry.date_added)}`,
        title: `${this.datePipe.getTitleAttributeValue(journalEntry.date_added)}`,
      },
      source: {text: journalEntry.source_info.name, subText: sourceInfoSubText, subTextLink: sourceInfoSubTextLink},
      destination: {
        text: journalEntry.destination_info.name, subText: destinationInfoSubText,
        subTextLink: destinationInfoSubTextLink
      },
      destination_amount: destinationAmount,
      credit_left: {text: creditLeftDisplay},
    };
  }

  getTableDataDict(): ITableData {
    return {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Date`, enableSort: true, fieldName: 'date_added',
            flex: '180px', flexXs: '105px'
          },
          {
            type: ColumnType.Value, displayName: $localize`Source info`, enableSort: true, fieldName: 'source',
            flex: '300px', hide: {xs: true, md: true, sm: true}
          },
          {
            type: ColumnType.Value,
            displayName: $localize`Destination info`,
            enableSort: true,
            fieldName: 'destination',
            flex: '250px',
            flexXs: '1 0 50px'
          },
          {
            type: ColumnType.Value,
            displayName: $localize`Prepaid credit balance`,
            enableSort: false,
            fieldName: 'credit_left',
            flex: '150px',
            hide: {xs: true, sm: true,}
          },
          {
            type: ColumnType.Currency,
            displayName: $localize`Amount`,
            enableSort: true,
            fieldName: 'destination_amount',
          },
          {
            type: ColumnType.Actions, displayName: '', enableSort: false, fieldName: '(actions)',
            hide: {xs: true}
          },
        ],
        columnNames: ['date_added', 'source', 'destination', 'credit_left', 'destination_amount', '(actions)'],
        showStatus: false,
        visibleActions: 2,
      },
      rows: [],
      alwaysShowActions: true,
    };
  }
}
