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 { OrderUiService } from './order-ui.service';
import { IOrderModel } from '@fleio-api/billing/model/order.model';
import { OrdersApiService } from '@fleio-api/billing/orders/order-api.service';
import { IAction } from '@objects-view/interfaces/actions/action';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
import { NotificationService } from '@shared/ui-api/notification.service';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';

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

  constructor(
    private router: Router,
    private config: ConfigService,
    private ordersApiService: OrdersApiService,
    private notificationService: NotificationService,
  ) {
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new OrderUiService(
      object as IOrderModel, permissions, state, this.router, this.config, this.ordersApiService,
      this.notificationService,
    );
  }

  getTableData(objectList: FleioObjectsList<IOrderModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {
            type: ColumnType.Value, displayName: $localize`Order #`, enableSort: true, fieldName: 'id',
            flex: '1 1 80px',
          },
          {
            type: ColumnType.Value, displayName: $localize`Client`, enableSort: true, fieldName: 'client',
            flex: '1 1 190px', hide: {xs: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`User`, enableSort: true, fieldName: 'user',
            flex: '1 1 110px', hide: {xs: true, sm: true, md: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Date`, enableSort: true, fieldName: 'date',
            flex: '1 1 170px', hide: {xs: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Status`, enableSort: true, fieldName: 'status',
            flex: '1 1 110px', hide: {xs: true, sm: true, md: true},
          },
          {
            type: ColumnType.Value, displayName: $localize`Payment status`, enableSort: true, fieldName: 'payment_status',
            flex: '1 1 120px', hide: {xs: true, sm: true, md: true},
          },
          {
            type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: true, fieldName: '(actions)',
          },
        ],
        columnNames: ['id', 'client', 'user', 'date', 'status', 'payment_status', '(actions)'],
        showStatus: true,
      },
      rows: [],
    };

    for (const order of objectList.objects) {
      const rowUIService = this.getObjectUIService(order, objectList.permissions, 'table-view');
      const row: ITableRow = {
        cells: {
          id: {text: order.id.toString()},
          client: {text: order.client ? order.client.display : $localize`n/a`},
          user: {text: order.user ? order.user.display : $localize`n/a`},
          date: {text: this.datePipe.toDateTime(order.order_date)},
          status: {text: order.status},
          payment_status: {text: order.invoice ? order.invoice.status : $localize`n/a`},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: order,
      };

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

    return tableData;
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [
      new RouterLinkAction({
        name: $localize`Create order`,
        tooltip: $localize`Create order`,
        icon: {name: 'add'},
        router: this.router,
        routerUrl: this.config.getPanelUrl('orders/create'),
      })
    ];
  }

  getRefreshInterval(): number {
    return this.config.getDefaultRefreshInterval(false);
  }
}
