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 } 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 { IConfigurationModel } from '@fleio-api/configurations/model/configuration.model';
import { ConfigurationsApiService } from '@fleio-api/configurations/configurations-api.service';
import { OpenstackPluginConfigurationComponent } from '../../plugins/openstack/openstack-plugin-configuration/openstack-plugin-configuration.component';
import { DomainsPluginConfigurationComponent } from '../../plugins/domains/domains-plugin-configuration/domains-plugin-configuration.component';
import {
  ConfigurationDetailsBillingFormComponent
} from './tabs/configuration-details-billing-form/configuration-details-billing-form.component';
import { ConfigurationEditFormComponent } from './tabs/configuration-edit-form/configuration-edit-form.component';


export class ConfigurationUiService extends ObjectUIServiceBase<IConfigurationModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly configurationsApiService: ConfigurationsApiService;

  constructor(
    configuration: IConfigurationModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, configurationsApiService: ConfigurationsApiService
  ) {
    super(configuration, permissions, state);
    this.router = router;
    this.config = config;
    this.configurationsApiService = configurationsApiService;
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'details':
        return {
          text: $localize`Configuration ${this.object.name}`,
          subText: `${this.object.client_count} clients on this configuration`,
        };

      case 'edit':
        return {
          text: $localize`Edit ${this.object.name}`,
        };

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

      default:
        return {
          text: `${this.object.name}`,
          subText: $localize`${this.object.client_count} clients on this configuration`,
        };
    }
  }

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

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

    const deleteAction = new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: $localize`Delete configuration`,
        name: $localize`Delete`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete configuration`,
          message: $localize`Are you sure you want to delete configuration ${this.object.name}?`,
        },
        successMessage: $localize`Configuration deleted`,
        errorMessage: $localize`Failed to delete configuration, check logs for details`,
        apiService: this.configurationsApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('settings/configurations'),
        noPermissions: !this.permissions['configurations.destroy'],
      }
    );

    if (this.object.is_default) {
      deleteAction.disabled = true;
      deleteAction.tooltip = $localize`Default configuration cannot be deleted`;
    }

    actions.push(deleteAction);

    return actions;
  }

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

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

    return actions;
  }

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

  getCardFields(): IDataField[] {
    const fields = [];
    fields.push({
      value: this.object.description ? this.object.description : $localize`No description`,
    });

    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Billing`,
            tabIdentifier: 'billing',
            component: ConfigurationDetailsBillingFormComponent,
          },
          {
            tabName: $localize`OpenStack`,
            tabIdentifier: 'openstack',
            featureName: 'openstack',
            component: OpenstackPluginConfigurationComponent,
          },
          {
            tabName: $localize`Domains`,
            tabIdentifier: 'domains',
            featureName: 'plugins.domains',
            component: DomainsPluginConfigurationComponent,
          },
        ];
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: ConfigurationEditFormComponent,
          },
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: ConfigurationEditFormComponent,
          },
        ];
    }
  }

  getCardTags(): string[] {
    const tags = [];
    if (this.object.is_default) {
      tags.push($localize`default`);
    }

    return tags;
  }
}
