import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { ITitle } from '@objects-view/interfaces/card-data/card-title';
import { IObjectStatus, StatusType, StatusValue } from '@objects-view/interfaces/object-status';
import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { IAction } from '@objects-view/interfaces/actions/action';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IInvoiceModel } from '@fleio-api/billing/model/invoice.model';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { InvoiceDetailsOverviewComponent } from '@billing-invoices-tabs/invoice-details-overview/invoice-details-overview.component';
import { IClientModel } from '@fleio-api/client-user/model/client.model';
import { InvoiceAddPaymentComponent } from './components/invoice-add-payment/invoice-add-payment.component';
import {
  PayMultipleInvoicesFormComponent
} from './tabs/pay-multiple-invoices-form/pay-multiple-invoices-form.component';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { CurrencyFormatterPipe } from '@shared/ui/common/pipes/currency-formatter.pipe';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';

export class InvoiceUIService extends ObjectUIServiceBase<IInvoiceModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly datePipe: DateFormatterPipe;
  private currencyPipe: CurrencyFormatterPipe;

  constructor(
    invoice: IInvoiceModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService,
  ) {
    super(invoice, permissions, state);
    this.router = router;
    this.config = config;
    this.datePipe = new DateFormatterPipe(config);
    this.currencyPipe = new CurrencyFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    let status: IObjectStatus = null;
    switch (this.object.status) {
      case 'paid':
        status = {type: StatusType.Defined, value: StatusValue.Paid};
        break;
      case 'canceled':
        status = {type: StatusType.Defined, value: StatusValue.Canceled};
        break;
      case 'refunded':
        status = {type: StatusType.Defined, value: StatusValue.Refunded};
        break;
      case 'unpaid':
        if (new Date(this.object.due_date).getTime() < new Date().getTime()) {
          status = {type: StatusType.Defined, value: StatusValue.Overdue};
        } else {
          status = {type: StatusType.Defined, value: StatusValue.Unpaid};
        }
        break;
    }

    if (['card-view', 'table-view', 'details'].includes(this.state)) {
      return status;
    }

    return null;
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'details':
        return {
          text: this.object.name,
          subText: this.object.status.toLocaleUpperCase(),
        };
      case 'pay-multiple-invoices':
        return {
          text: $localize`Pay multiple invoices`,
        };
      default:
        return {
          text: `${this.object.display_number}`,
          subText: this.object.status.toLocaleUpperCase(),
        };
    }
  }

  getActions(): IAction[] {
    return [];
  }

  getDetailsActions(): IAction[] {
    const actions = [];
    switch (this.state) {
      case 'pay-multiple-invoices':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`billing/invoices`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: 'Pay'}));
    }
    return actions;
  }

  getCardTags(): string[] {
    return [];
  }

  getDetailsLink(): string {
    return this.config.getPanelUrl(`billing/invoices/${this.object.id}`);
  }

  getCardFields(): IDataField[] {
    return [
      {
        name: $localize`Issue date`,
        value: this.datePipe.transform(this.object.issue_date),
        title: this.datePipe.getTitleAttributeValue(this.object.issue_date),
      },
      {
        name: $localize`Total`,
        value: this.currencyPipe.transform(this.object.total, this.object.currency, this.config.authService.currencyDisplayMode, null)
      },
      {
        name: $localize`Client`,
        value: this.object.client ? (this.object.client as IClientModel).display : $localize`n/a`,
      }
    ];
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Details`,
            tabIdentifier: 'details',
            component: InvoiceDetailsOverviewComponent,
          },
        ];
      case 'pay-multiple-invoices':
        return [
          {
            tabName: $localize`Pay multiple invoices`,
            tabIdentifier: 'pay-multiple-invoices',
            component: PayMultipleInvoicesFormComponent,
          }
        ];
      default:
        return null;
    }
  }

  getHeaderComponent(): any {
    return {
      position: 'header-right',
      component: InvoiceAddPaymentComponent,
    };
  }

  getObjectDetailsRefreshInterval(): number {
    switch (this.state) {
      case 'details':
        if (this.object.status === 'paid') {
          return this.config.getDefaultRefreshInterval();
        } else {
          return this.config.getDefaultRefreshInterval(true);
        }
      default:
        return null;
    }
  }
}
