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, StatusType, StatusValue } 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 { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { HypervisorApiService } from '@fleio-api/openstack/hypervisor/hypervisor-api.service';
import { IHypervisorModel } from '@fleio-api/openstack/model/hypervisor.model';
import { HypervisorInfoTabComponent } from './tabs/hypervisor-info-tab/hypervisor-info-tab.component';

export class HypervisorUiService extends ObjectUIServiceBase<IHypervisorModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly hypervisorApiService: HypervisorApiService;
  private readonly datePipe: DateFormatterPipe;

  constructor(
    object: IHypervisorModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, hypervisorApiService: HypervisorApiService
  ) {
    super(object, permissions, state);
    this.router = router;
    this.config = config;
    this.hypervisorApiService = hypervisorApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    if (this.object.state === 'down') {
      return {type: StatusType.Defined, value: StatusValue.Error};
    }
    if (this.object.status === 'enabled') {
      return {type: StatusType.Defined, value: StatusValue.Enabled};
    } else {
      return {type: StatusType.Defined, value: StatusValue.Disabled};
    }
  }

  getTitle(): ITitle {
    const statusSubText = this.object.status === 'enabled' ? $localize`Enabled` : $localize`Disabled`;
    const stateSubText = this.object.state === 'up' ? $localize`Up` : $localize`Down`;
    switch (this.state) {
      default:
        return {
          text: `${this.object.host_name}`,
          subText: `${stateSubText} & ${statusSubText}`
        };
    }
  }

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

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

    switch (this.state) {
      default:
        break;
    }

    return actions;
  }

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

  getCardFields(): IDataField[] {
    return [
      {name: $localize`Region`, value: this.object.region},
      {name: $localize`Host IP`, value: this.object.host_ip},
      {name: $localize`Type`, value: this.object.hypervisor_type},
    ] as IDataField[];
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: HypervisorInfoTabComponent,
          }
        ];
    }
  }

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