import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
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 { ApiCallAction, CallType } from '@objects-view/actions/api-call-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 { IRoleModel } from '@fleio-api/client-user/roles/model/role.model';
import { RolesApiService } from '@fleio-api/client-user/roles/roles-api.service';
import { RoleEditFormComponent } from '@shared/common-tabs/clients-users/roles/role-edit-form/role-edit-form.component';
import {
  RoleDetailsOverviewComponent
} from '@shared/common-tabs/clients-users/roles/role-details-overview/role-details-overview.component';
import {
  RoleDetailsPermissionsComponent
} from '@shared/common-tabs/clients-users/roles/role-details-permissions/role-details-permissions.component';

export class RoleUiService extends ObjectUIServiceBase<IRoleModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly rolesApiService: RolesApiService;
  private readonly datePipe: DateFormatterPipe;

  constructor(
    role: IRoleModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, rolesApiService: RolesApiService
  ) {
    super(role, permissions, state);
    this.router = router;
    this.config = config;
    this.rolesApiService = rolesApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'edit':
        return {
          text: $localize`Edit role ${this.object.display_name}`,
        };

      case 'create':
        return {
          text: $localize`Create new role`,
        };

      case 'details':
        return {
          text: `${this.object.display_name}`,
        };

      default:
        return {
          text: `${this.object.display_name}`,
        };
    }
  }

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

    actions.push(new RouterLinkAction({
        icon: {name: 'edit', class: 'fl-icons'},
        name: $localize`Edit`,
        tooltip: this.object.default ? $localize`Default roles cannot be edited` : $localize`Edit role`,
        routerUrl: this.config.getPanelUrl(`clients-users/roles/${this.object.id}/edit`),
        router: this.router,
        disabled: this.object.default,
      }
    ));

    const deleteAction = new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: this.object.default ? $localize`Default roles cannot be deleted` : $localize`Delete role`,
        name: $localize`Delete`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete role`,
          message: $localize`Are you sure you want to delete role ${this.object.display_name}?`,
        },
        successMessage: $localize`Role deleted`,
        errorMessage: $localize`Failed to delete role, check logs for details`,
        apiService: this.rolesApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('clients-users/roles'),
        disabled: this.object.default,
      }
    );

    actions.push(deleteAction);

    if (!this.object.public) {
      actions.push(new ApiCallAction(
        {
          object: this.object,
          icon: {name: 'visibility_on'},
          tooltip: $localize`Set public (will be visible to end-users)`,
          name: $localize`Set public`,
          successMessage: $localize`Role set as public`,
          errorMessage: $localize`Failed to set role as public, check logs for details`,
          apiService: this.rolesApiService,
          callType: CallType.Post,
          apiAction: 'set-public',
          refreshAfterExecute: true,
        }
      ));
    }

    if (this.object.public) {
      actions.push(new ApiCallAction(
        {
          object: this.object,
          icon: {name: 'visibility_off'},
          tooltip: $localize`Set private (will not be visible to end-users)`,
          name: $localize`Set private`,
          successMessage: $localize`Role set as private`,
          errorMessage: $localize`Failed to set role as private, check logs for details`,
          apiService: this.rolesApiService,
          callType: CallType.Post,
          apiAction: 'clear-public',
          refreshAfterExecute: true,
        }
      ));
    }

    return actions;
  }

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

    switch (this.state) {
      case 'create':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`clients-users/roles`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create`}));
        break;
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`clients-users/roles`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      default:
        break;
    }

    return actions;
  }

  getDetailsLink(): string {
    return this.config.getPanelUrl(`clients-users/roles/${this.object.id}`);
  }

  getCardFields(): IDataField[] {
    const fields = [
      {name: $localize`Name`, value: this.object.display_name},
      {
        name: $localize`Created at`,
        value: this.datePipe.transform(this.object.created_at),
        title: this.datePipe.getTitleAttributeValue(this.object.created_at),
      },
      {name: $localize`Owner`, value: this.object.owner_display || $localize`n/a`},
      {name: $localize`Parent`, value: this.object.parent_display || $localize`n/a`},
    ];

    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: RoleEditFormComponent,
          },
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: RoleEditFormComponent,
          },
        ];
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: RoleDetailsOverviewComponent,
          },
          {
            tabName: $localize`Permissions`,
            tabIdentifier: 'permissions',
            component: RoleDetailsPermissionsComponent,
          },
        ];
    }
  }

  getCardTags(): string[] {
    const tags = [];
    return tags;
  }
}
