import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { ITitle } from '@objects-view/interfaces/card-data/card-title';
import { IObjectStatus, StatusType, StatusValue } from '@objects-view/interfaces/object-status';
import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { IAction } from '@objects-view/interfaces/actions/action';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { IFlavorModel } from '@fleio-api/openstack/model/flavor.model';
import { FlavorsApiService } from '@fleio-api/openstack/flavor/flavors-api.service';
import { OpenstackUtils } from '@fleio-api/openstack/openstack-utils';
import { EntityAuditLogComponent } from '../../common-components/entity-audit-log/entity-audit-log.component';
import { FlavorDetailsOverviewComponent } from './tabs/flavor-details-overview/flavor-details-overview.component';
import { FlavorEditFormComponent } from './tabs/flavor-edit-form/flavor-edit-form.component';
import {
  ShowToClientGroupsFormComponent
} from './tabs/show-to-client-groups-form/show-to-client-groups-form.component';
import {
  FlavorPropertiesEditFormComponent
} from './tabs/flavor-properties-edit-form/flavor-properties-edit-form.component';

export class FlavorUiService extends ObjectUIServiceBase<IFlavorModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly flavorsApiService: FlavorsApiService;


  constructor(
    flavor: IFlavorModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, flavorsApiService: FlavorsApiService,
  ) {
    super(flavor, permissions, state);
    this.router = router;
    this.config = config;
    this.flavorsApiService = flavorsApiService;
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    if (this.state === 'show-to-client-groups') {
      return {type: StatusType.None, value: StatusValue.None};
    }
    if (this.object.disabled) {
      return {type: StatusType.Defined, value: StatusValue.Disabled};
    } else {
      return {type: StatusType.Defined, value: StatusValue.Enabled};
    }
  }

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

      case 'show-to-client-groups':
        return {
          text: $localize`Show flavor ${this.object.name} to client groups`,
        };

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

      case 'clone':
        return {
          text: $localize`Clone flavor ${this.object.name}`,
        };

      case 'details':
      default:
        return {
          text: `${this.object.name}`,
          subText: `${this.object.disabled ? $localize`DISABLED` : $localize`ACTIVE`}, ${this.object.region}`,
        };
    }
  }

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

    actions.push(new RouterLinkAction({
        icon: {name: 'content_copy'},
        name: $localize`Clone`,
        tooltip: $localize`Clone`,
        routerUrl: this.config.getPanelUrl(`openstack/flavors/${this.object.id}/clone`),
        router: this.router,
        noPermissions: !this.permissions['flavors.create'],
      }
    ));

    actions.push(new RouterLinkAction({
        icon: {name: 'users', class: 'fl-icons'},
        name: $localize`Show to client groups`,
        tooltip: $localize`Show to client groups`,
        routerUrl: this.config.getPanelUrl(`openstack/flavors/${this.object.id}/show-to-client-groups`),
        router: this.router,
      }
    ));

    actions.push(new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        name: $localize`Delete`,
        tooltip: $localize`Delete`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete flavor`,
          message: $localize`Are you sure you want to delete flavor ${this.object.name}?`,
        },
        successMessage: $localize`Flavor delete queued`,
        errorMessage: $localize`Failed to delete flavor, check logs for details`,
        apiService: this.flavorsApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('openstack/flavors'),
        noPermissions: !this.permissions['flavors.destroy'],
      }
    ));

    return actions;
  }

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

    switch (this.state) {
      case 'create':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/flavors`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create`}));
        break;
      case 'clone':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/flavors`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Clone`}));
        break;
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/flavors`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({
          name: $localize`Save`,
          refreshAfterExecute: false,
        }));
        break;
      default:
        break;
    }

    return actions;
  }

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

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

    if (!this.object.show_for_end_user) {
      fields.push(
        {
          value: $localize`${this.object.vcpus} vCPUs, ${OpenstackUtils.MiBToUserFriendly(this.object.memory_mb, true)}, ${this.object.root_gb} GB disk`
        },
      )
    } else {
      fields.push(
        {
          value: $localize`${this.object.vcpus} vCPUs`
        },
        {
          value: $localize`${OpenstackUtils.MiBToUserFriendly(this.object.memory_mb, true)}, ${this.object.root_gb} GB disk`
        },
      )
    }

    fields.push(
      {
        name: $localize`Group`,
        value: this.object.flavor_group?.name || $localize`n/a`
      }
    );
    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: FlavorDetailsOverviewComponent,
          },
          {
            tabName: $localize`Audit log`,
            tabIdentifier: 'audit-log',
            component: EntityAuditLogComponent,
            featureName: 'utils.activitylog',
            data: {
              entity_type: 'flavor'
            }
          }
        ];
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: FlavorEditFormComponent,
          },
        ];
      case 'clone':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: FlavorEditFormComponent,
          },
        ];
      case 'show-to-client-groups':
        return [
          {
            tabName: $localize`Show to client groups`,
            tabIdentifier: 'show-to-client-groups',
            component: ShowToClientGroupsFormComponent,
          }
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: FlavorEditFormComponent,
          },
          {
            tabName: $localize`Edit properties`,
            tabIdentifier: 'edit-properties',
            component: FlavorPropertiesEditFormComponent,
          }
        ];
    }
  }

  getCardTags(): string[] {
    const tags = [];
    if (!this.object.show_for_end_user) {
      tags.push($localize`HIDDEN`);
    }
    return tags;
  }
}
