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 { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { ISubnetPoolModel } from '@fleio-api/openstack/subnet-pools/model/subnet-pool.model';
import { SubnetPoolsApiService } from '@fleio-api/openstack/subnet-pools/subnet-pools-api.service';
import { SubnetPoolDetailsOverviewComponent } from './tabs/subnet-pool-details-overview/subnet-pool-details-overview.component';
import { SubnetPoolEditFormComponent } from './tabs/subnet-pool-edit-form/subnet-pool-edit-form.component';
import { EntityAuditLogComponent } from '../../common-components/entity-audit-log/entity-audit-log.component';

export class SubnetPoolUiService extends ObjectUIServiceBase<ISubnetPoolModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly subnetPoolsApiService: SubnetPoolsApiService;
  private readonly datePipe: DateFormatterPipe;


  constructor(
    subnetPoolModel: ISubnetPoolModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, subnetPoolsApiService: SubnetPoolsApiService,
  ) {
    super(subnetPoolModel, permissions, state);
    this.router = router;
    this.config = config;
    this.subnetPoolsApiService = subnetPoolsApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

  getTitle(): ITitle {
    const shared = this.object.shared? $localize`SHARED` : $localize`NOT SHARED`;
    const subText = this.object.is_default? $localize`${shared} - DEFAULT`: shared;
    switch (this.state) {
      case 'edit':
        return {
          text: $localize`Edit subnet pool ${this.object.name}`,
          subText,
        };

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

      case 'details':
      default:
        return {
          text: this.object.name,
          subText,
        };
    }
  }

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

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

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

    return actions;
  }

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

    switch (this.state) {
      case 'create':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/subnet-pools`),
            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/subnet-pools`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      default:
        break;
    }

    return actions;
  }

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

  getCardFields(): IDataField[] {
    const fields = [
      {
        name: $localize`Created at`,
        value: this.datePipe.transform(this.object.created_at),
        title: this.datePipe.getTitleAttributeValue(this.object.created_at),
      },
      {
        name: $localize`Region`,
        value: this.object.region,
      },
      {
        value: this.object.prefixes.join(', '),
      },
    ];

    return fields;
  }

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

  getCardTags(): string[] {
    return null;
  }
}
