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 { 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 { 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 { EntityAuditLogComponent } from '../../common-components/entity-audit-log/entity-audit-log.component';
import { IRBACPolicyModel } from '@fleio-api/openstack/model/rbac-policy.model';
import { RbacPolicyCreateFormComponent } from './tabs/rbac-policy-create-form/rbac-policy-create-form.component';
import {
  RbacPolicyDetailsOverviewComponent
} from './tabs/rbac-policy-details-overview/rbac-policy-details-overview.component';
import { RbacPoliciesApiService } from '@fleio-api/openstack/rbac-policy/rbac-policies-api.service';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { RbacPolicyEditFormComponent } from './tabs/rbac-policy-edit-form/rbac-policy-edit-form.component';

export class RbacPolicyUiService extends ObjectUIServiceBase<IRBACPolicyModel> {
  constructor(
    networkModel: IRBACPolicyModel, permissions: IPermissionsModel, state: string,
    private router: Router, private config: ConfigService, private rbacPoliciesApiService: RbacPoliciesApiService,
  ) {
    super(networkModel, permissions, state);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

  getTitle(additionalObjects?): ITitle {
    switch (this.state) {
      case 'edit':
        return {
          text: $localize`Edit RBAC policy`,
        };

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

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

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

    actions.push(new RouterLinkAction({
      icon: {name: 'edit'},
      name: $localize`Edit`,
      tooltip: $localize`Edit RBAC policy`,
      routerUrl: this.config.getPanelUrl(`openstack/rbac-policies/${this.object.id}/edit`),
      router: this.router,
      noPermissions: !this.permissions['rbacpolicies.update'],
    }));

    actions.push(new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: $localize`Delete RBAC policy`,
        name: $localize`Delete`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete RBAC policy`,
          message: $localize`Are you sure you want to delete RBAC policy?`,
        },
        successMessage: $localize`RBAC policy delete queued`,
        apiService: this.rbacPoliciesApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('openstack/rbac-policies'),
        noPermissions: !this.permissions['rbacpolicies.destroy'],
      }
    ));

    return actions;
  }

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

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

    return actions;
  }

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

  getCardFields(): IDataField[] {
    let objectUrl: string = null;
    switch (this.object.object_type) {
      case 'network':
        objectUrl = this.config.getPanelUrl(`openstack/networks/${this.object.object_id}`);
        break;
      case 'subnetpool':
        objectUrl = this.config.getPanelUrl(`openstack/subnet-pools/${this.object.object_id}`);
        break;
    }
    return [
      {
        name: $localize`Project`,
        value: `${this.object.fleio_project.name}`,
        url: this.object.fleio_project.id ? this.config.getPanelUrl(`openstack/projects/${this.object.fleio_project.id}`) : null,
      },
      {
        name: $localize`Target project`,
        value: `${this.object.target_project}`,
      },
      {
        name: $localize`Object type`,
        value: `${this.object.object_type}, ${this.object.action_display}`,
      },
      {
        name: $localize`Object`,
        value: `${this.object.object_display}`,
        url: objectUrl,
      },
    ];
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: RbacPolicyCreateFormComponent,
          },
        ];
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: RbacPolicyDetailsOverviewComponent,
          },
          {
            tabName: $localize`Audit log`,
            tabIdentifier: 'audit-log',
            component: EntityAuditLogComponent,
            featureName: 'utils.activitylog',
            data: {
              entity_type: 'rbac_policy'
            }
          }
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: RbacPolicyEditFormComponent,
          },
        ];
    }
  }

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

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