import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { IObjectListUIService } from '@objects-view/interfaces/object-list-ui-service';
import { ConfigService } from '@shared/config/config.service';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { ITableData } from '@objects-view/interfaces/table-data/table-data';
import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { NotificationsUiService } from './notifications-ui.service';
import { INotificationModel } from '@fleio-api/notifications/model/notification.model';
import { UserNotificationsApiService } from '@fleio-api/notifications/user-notifications-api.service';
import { IObjectUIService } from '@objects-view/interfaces/object-ui-service';
import { IAction } from '@objects-view/interfaces/actions/action';
import { ColumnType } from '@objects-view/interfaces/table-data/column-definition';
import { ITableRow } from '@objects-view/interfaces/table-data/table-row';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';


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

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

  getTableData(objectList: FleioObjectsList<INotificationModel>): ITableData {
    const tableData: ITableData = {
      header: {
        columns: [
          {type: ColumnType.Value, displayName: $localize`Title`, enableSort: false, fieldName: 'title',
            flex: '1 0 200px', flexXs: '150px'},
          {type: ColumnType.Value, displayName: $localize`Generated`, enableSort: true, fieldName: 'generated'},
          {type: ColumnType.Value, displayName: $localize`Status`, enableSort: false, fieldName: 'status'},
          {type: ColumnType.Actions, displayName: $localize`Actions`, enableSort: false, fieldName: '(actions)'},
        ],
        columnNames: ['title', 'generated', 'status', '(actions)'],
        showStatus: true,
      },
      rows: []
    };
    const notSeenStatuses = ['pending', 'sent', 'error'];
    for (const notification of objectList.objects) {
      const rowUIService = this.getObjectUIService(notification, objectList.permissions, 'table-view');
      const notSeen = notSeenStatuses.indexOf(notification.status) > -1;
      const row: ITableRow = {
        cells: {
          title: {text: notification.title, textBold: notSeen},
          generated: {
            text: this.datePipe.transform(notification.generated),
            title: this.datePipe.getTitleAttributeValue(notification.generated),
            textBold: notSeen
          },
          status: {text: notification.status, textBold: notSeen},
        },
        icon: rowUIService.getIcon(),
        status: rowUIService.getStatus(),
        actions: rowUIService.getActions(),
        url: rowUIService.getDetailsLink(),
        object: notification,
      };

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

  getObjectUIService(
    object: IBaseFleioObjectModel, permissions: IPermissionsModel, state: string
  ): IObjectUIService {
    return new NotificationsUiService(
      object as INotificationModel, permissions, state, this.router, this.config, this.userNotificationsApiService,
    );
  }

  getActions(objectList: FleioObjectsList<IBaseFleioObjectModel>): IAction[] {
    return [
      new ApiCallAction(
        {
          icon: {name: 'visibility'},
          name: $localize`Mark all as read`,
          tooltip: $localize`Mark all as read`,
          confirmOptions: {
            confirm: true,
            title: $localize`Mark all notification as read`,
            message: $localize`Are you sure you want to mark all notification as read?`,
          },
          apiService: this.userNotificationsApiService,
          callType: CallType.Post,
          apiAction: 'mark_all_as_read'
        }
      ),
    ];
  }

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