import { Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { ConfigService } from '@shared/config/config.service';
import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { IObjectStatus } 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 { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { IPredefinedReplyCategoryModel } from '@fleio-api/plugins/tickets/model/predefined-reply-category.model';
import { PredefinedReplyCategoryApiService } from '@fleio-api/plugins/tickets/predefined-reply-category-api.service';
import { PredefinedReplyCategoryEditFormComponent } from './tabs/predefined-reply-category-edit-form/predefined-reply-category-edit-form.component';
import { PredefinedReplyCategoryDetailsOverviewComponent } from './tabs/predefined-reply-category-details-overview/predefined-reply-category-details-overview.component';
import { PredefinedReplyEditFormComponent } from './tabs/predefined-reply-edit-form/predefined-reply-edit-form.component';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';

export class PredefinedReplyUiService extends ObjectUIServiceBase<IPredefinedReplyCategoryModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly predefinedReplyCategoryApiService: PredefinedReplyCategoryApiService;
  private readonly matDialog: MatDialog;
  private readonly datePipe: DateFormatterPipe;

  constructor(
    predefinedReplyCategory: IPredefinedReplyCategoryModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, predefinedReplyCategoryApiService: PredefinedReplyCategoryApiService,
    matDialog: MatDialog,
  ) {
    super(predefinedReplyCategory, permissions, state);
    this.matDialog = matDialog;
    this.router = router;
    this.config = config;
    this.predefinedReplyCategoryApiService = predefinedReplyCategoryApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'create-category':
        return {
          text: $localize`Add category`,
        };
      case 'add-predefined-reply':
        return {
          text: $localize`Add predefined reply for ${this.object.name} category`,
        };
      case 'edit-predefined-reply':
        return {
          text: $localize`Edit predefined reply`,
        };
      default:
        return {
          text: `${this.object.name}`,
        };
    }
  }

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

    actions.push(new RouterLinkAction({
        icon: {name: 'edit', class: 'fl-icons'},
        name: $localize`Edit`,
        tooltip: $localize`Edit`,
        routerUrl: this.config.getPanelUrl(
          `tickets/predefined-replies/edit-category/${this.object.id}`
        ),
        noPermissions: !this.permissions['predefined_reply_categories.update'],
        router: this.router,
      }
    ));

    actions.push(new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: $localize`Delete`,
        name: $localize`Delete`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete predefined reply category`,
          message: $localize`Are you sure you want to delete predefined reply category ${this.object.name}?`,
        },
        successMessage: $localize`Predefined reply category deleted`,
        apiService: this.predefinedReplyCategoryApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('tickets/predefined-replies'),
        noPermissions: !this.permissions['predefined_reply_categories.destroy'],
      }
    ));

    return actions;
  }

  getDetailsLink(): string {
    return this.config.getPanelUrl(`tickets/predefined-replies/category/${this.object.id}`);
  }

  getCardFields(): IDataField[] {
    return [
      {
        name: $localize`Predefined replies`,
        value: `${this.object.replies_count}`
      },
      {
        name: $localize`Created at`,
        value: this.datePipe.transform(this.object.created_at),
        title: this.datePipe.getTitleAttributeValue(this.object.created_at),
      },
    ];
  }

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

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'create-category':
        return [
          {
            tabName: $localize`Create category`,
            tabIdentifier: 'create-category',
            component: PredefinedReplyCategoryEditFormComponent,
          },
        ];
      case 'add-predefined-reply':
        return [
          {
            tabName: $localize`Add predefined reply`,
            tabIdentifier: 'add-predefined-reply',
            component: PredefinedReplyEditFormComponent,
          },
        ];
      case 'edit-predefined-reply':
        return [
          {
            tabName: $localize`Edit predefined reply`,
            tabIdentifier: 'edit-predefined-reply',
            component: PredefinedReplyEditFormComponent,
          },
        ];
      case 'edit-category':
        return [
          {
            tabName: $localize`Edit category`,
            tabIdentifier: 'edit-category',
            component: PredefinedReplyCategoryEditFormComponent,
          },
        ];
      case 'details':
        return [
          {
            tabName: $localize`Category details`,
            tabIdentifier: 'category-details',
            component: PredefinedReplyCategoryDetailsOverviewComponent,
          },
        ];
      default:
        return [];
    }
  }

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

    switch (this.state) {
      case 'details':
        actions.push(new CallbackAction({ name: $localize`Add new predefined reply` }));
        break;
      case 'create-category':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`tickets/predefined-replies`),
            router: this.router,
          }
        ));

        actions.push(new CallbackAction({ name: $localize`Save` }));
        break;
      case 'add-predefined-reply':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(
              `tickets/predefined-replies/category/${this.object.id}`
            ),
            router: this.router,
          }
        ));

        actions.push(new CallbackAction({ name: $localize`Save` }));
        break;
      case 'edit-predefined-reply':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(
              `tickets/predefined-replies/category/${this.object.id}`
            ),
            router: this.router,
          }
        ));

        actions.push(new CallbackAction({ name: $localize`Save` }));
        break;
      case 'edit-category':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`tickets/predefined-replies/${this.object.id}`),
            router: this.router,
          }
        ));

        actions.push(new CallbackAction({ name: $localize`Save` }));
        break;
      default:
        break;
    }

    return actions;
  }
}
