import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { ITitle } from '@objects-view/interfaces/card-data/card-title';
import { IAction } from '@objects-view/interfaces/actions/action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { IObjectStatus } from '@objects-view/interfaces/object-status';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { IVolumeType } from '@fleio-api/openstack/model/volume-type.model';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { VolumeTypesApiService } from '@fleio-api/openstack/volume/volume-types-api.service';
import { EntityAuditLogComponent } from '../../common-components/entity-audit-log/entity-audit-log.component';
import { VolumeTypeEditFormComponent } from './tabs/volume-type-edit-form/volume-type-edit-form.component';
import {
  VolumeTypeDetailsOverviewComponent
} from './tabs/volume-type-details-overview/volume-type-details-overview.component';

export class VolumeTypeUiService extends ObjectUIServiceBase<IVolumeType> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly volumeTypesApiService: VolumeTypesApiService;

  constructor(
    volumeType: IVolumeType, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, volumeTypesApiService: VolumeTypesApiService,
  ) {
    super(volumeType, permissions, state);
    this.router = router;
    this.config = config;
    this.volumeTypesApiService = volumeTypesApiService;
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'edit':
        return {
          text: $localize`Edit volume type ${this.object.name || this.object.id}`,
        };

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

      default:
        return {
          text: `${this.object.name || this.object.id}`,
        };
    }
  }

  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(`openstack/volume-types/${this.object.id}/edit`),
      router: this.router,
      noPermissions: !this.permissions['volumetypes.update'],
    }));

    actions.push(new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        name: $localize`Delete`,
        tooltip: $localize`Delete volume type`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete volume type`,
          message: $localize`Are you sure you want to delete volume type ${this.object.name || this.object.id}?`,
        },
        successMessage: $localize`Volume type delete queued`,
        noPermissions: !this.permissions['volumetypes.destroy'],
        apiService: this.volumeTypesApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('openstack/volume-types'),
      }
    ));

    return actions;
  }

  getDetailsActions(): IAction[] {
    const actions = [];
    switch (this.state) {
      case 'create':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/volume-types`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/volume-types/${this.object.id}`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      default:
        break;
    }
    return actions;
  }

  getDetailsLink(): string {
    return this.config.getPanelUrl(`openstack/volume-types/${this.object.id}`);
  }

  getCardFields(): IDataField[] {
    const fields = [];

    fields.push({
      name: $localize`Description`,
      value: this.object.description || $localize`n/a`,
    });

    fields.push({
      name: $localize`Region`,
      value: this.object.region,
    });

    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: VolumeTypeEditFormComponent,
          },
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: VolumeTypeEditFormComponent,
          },
        ];
      case 'details':
        return [
          {
            tabName: $localize`Details`,
            tabIdentifier: 'details',
            component: VolumeTypeDetailsOverviewComponent,
          },
          {
            tabName: $localize`Audit log`,
            tabIdentifier: 'audit-log',
            component: EntityAuditLogComponent,
            featureName: 'utils.activitylog',
            data: {
              entity_type: 'volume_type'
            }
          }
        ];
    }
  }

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

  getObjectDetailsRefreshInterval(): number {
    switch (this.state) {
      case 'details':
        return this.config.getDefaultRefreshInterval();
      default:
        return null;
    }
  }
}
