import { Router } from '@angular/router';
import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { ConfigService } from '@shared/config/config.service';
import { INotificationModel } from '@fleio-api/notifications/model/notification.model';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { UserNotificationsApiService } from '@fleio-api/notifications/user-notifications-api.service';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { IObjectStatus, StatusType, StatusValue } from '@objects-view/interfaces/object-status';
import { ITitle } from '@objects-view/interfaces/card-data/card-title';
import { IAction } from '@objects-view/interfaces/actions/action';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { NotificationDetailsMessageComponent } from '@shared/common-tabs/notifications/notification-details-message/notification-details-message.component';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';


export class NotificationsUiService extends ObjectUIServiceBase<INotificationModel> {
  private statusMap: Map<string, IObjectStatus> = new Map<string, IObjectStatus>([
    ['pending', { type: StatusType.Defined, value: StatusValue.Warning }],
    ['seen', { type: StatusType.Defined, value: StatusValue.Canceled }],
  ]);
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly userNotificationsApiService: UserNotificationsApiService;
  private readonly datePipe: DateFormatterPipe;


  constructor(
    notifications: INotificationModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, userNotificationsApiService: UserNotificationsApiService
  ) {
    super(notifications, permissions, state);
    this.router = router;
    this.config = config;
    this.userNotificationsApiService = userNotificationsApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    if (this.object.status) {
      switch (this.object.status) {
        case 'pending':
        case 'sent':
          return  {type: StatusType.Defined, value: StatusValue.Warning};
        case 'error':
          return  {type: StatusType.Defined, value: StatusValue.Error};
        case 'seen':
          return  {type: StatusType.Defined, value: StatusValue.Canceled};
        default:
          return {type: StatusType.Defined, value: StatusValue.Canceled};
      }
    }

    let status: IObjectStatus = { type: StatusType.None, value: StatusValue.None };
    if (this.statusMap.has(this.object.status)) {
      status = this.statusMap.get(this.object.status);
    }
    return status;
  }

  getTitle(): ITitle {
    switch (this.state) {
      default:
        return {
          text: `${this.object.title}`,
          subText: `${this.datePipe.transform(this.object.generated)}`,
        };
    }
  }

  getActions(): IAction[] {
    const actions = [];
    if (this.object.status === 'seen') {
      actions.push(new ApiCallAction(
        {
          object: this.object,
          icon: {name: 'visibility_off'},
          name: $localize`Mark as unread`,
          tooltip: $localize`Mark as unread`,
          confirmOptions: {
            confirm: true,
            title: $localize`Mark notification as unread`,
            message: $localize`Are you sure you want to mark notification as unread?`,
          },
          apiService: this.userNotificationsApiService,
          refreshAfterExecute: false,
          redirectAfterExecute: true,
          redirectUrl: this.config.getPanelUrl('user/notifications'),
          callType: CallType.Post,
          apiAction: 'unseen'
        }
      ));
    } else {
      actions.push(new ApiCallAction(
        {
          object: this.object,
          icon: {name: 'visibility'},
          name: $localize`Mark as read`,
          tooltip: $localize`Mark as read`,
          confirmOptions: {
            confirm: true,
            title: $localize`Mark notification as read`,
            message: $localize`Are you sure you want to mark notification as read?`,
          },
          apiService: this.userNotificationsApiService,
          callType: CallType.Post,
          apiAction: 'view'
        }
      ));
    }
    return actions;
  }

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

  getDetailsActions(): IAction[] {
    const actions = [];

    switch (this.state) {
      case 'details':
        actions.push(new RouterLinkAction({
            name: $localize`Back`,
            routerUrl: this.config.getPrevUrl(`user/notifications`),
            router: this.router,
          }
        ));
        if (this.object.status === 'pending') {
          actions.push(new CallbackAction({name: $localize`Mark as read`}));
        } else if (this.object.status === 'seen') {
          actions.push(new CallbackAction({name: $localize`Mark as unread`}));
        }
        break;
      default:
        break;
    }

    return actions;
  }

  getCardFields(): IDataField[] {
    return [
      {
        name: $localize`Status`,
        value: this.object.status
      }
    ];
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Details`,
            tabIdentifier: 'details',
            component: NotificationDetailsMessageComponent,
          }
        ];
    }
  }

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