import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { IOperationModel } from '@fleio-api/utils/model/operation.model';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { OperationsApiService } from '@fleio-api/utils/operations/operations-api.service';
import { MatDialog } from '@angular/material/dialog';
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 { ApiCallAction } from '@objects-view/actions/api-call-action';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { OperationDetailsOverviewComponent } from '@shared/common-tabs/utilities/operations/operation-details-overview/operation-details-overview.component';

export class OperationUiService extends ObjectUIServiceBase<IOperationModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly operationsApiService: OperationsApiService;
  private readonly matDialog: MatDialog;


  constructor(
    operations: IOperationModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, operationsApiService: OperationsApiService, matDialog: MatDialog,
  ) {
    super(operations, permissions, state);
    this.matDialog = matDialog;
    this.router = router;
    this.config = config;
    this.operationsApiService = operationsApiService;
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    let status: IObjectStatus = { type: StatusType.None, value: StatusValue.None };
    if (this.object.status === 'in progress') {
      status = {
        type: StatusType.Changing,
        value: StatusValue.Waiting
      };
    } else if (this.object.status === 'succeeded') {
      status = {
        type: StatusType.Defined,
        value: StatusValue.Success
      };
    } else if (this.object.status === 'failed') {
      status = {
        type: StatusType.Defined,
        value: StatusValue.Failed
      };
    } else if (this.object.status === 'aborted') {
      status = {
        type: StatusType.Defined,
        value: StatusValue.Disabled
      };
    }
    return status;
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'details':
        return {
          text: $localize`Operation ${this.object.id}`,
        };

      default:
        return {
          text: $localize`Operation ${this.object.id}`,
          textBold: !this.object.seen
        };
    }
  }

  getActions(): IAction[] {
    const actions: IAction[] = [];
    if (!this.object.completed) {
      actions.push(new ApiCallAction(
        {
          object: this.object,
          icon: {name: 'cancel'},
          tooltip: $localize`Abort operation`,
          name: $localize`Abort operation`,
          confirmOptions: {
            confirm: true,
            title: $localize`Abort operation`,
            message: $localize`Are you sure you want to abort operation ${this.object.id}:object_id:?`
          },
          apiService: this.operationsApiService,
          apiAction: 'abort_operation',
        }
      ));
    }

    if (this.object.can_retry) {
      actions.push(new ApiCallAction(
        {
          object: this.object,
          icon: {name: 'reboot', class: 'fl-icons'},
          tooltip: $localize`Retry`,
          name: $localize`Retry`,
          confirmOptions: {
            confirm: true,
            title: $localize`Retry operation`,
            message: $localize`Are you sure you want to retry operation ${this.object.id}?`
          },
          apiService: this.operationsApiService,
          apiAction: 'retry',
        }
      ));
    }
    return actions;
  }

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

  getCardFields(): IDataField[] {
    return [
      {
        name: $localize`Status`,
        value: `${this.object.status}`
      },
      {
        name: $localize`Operation type`,
        value: `${this.object.operation_type}`
      },
    ];
  }

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

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

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

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

    return actions;
  }

  getObjectDetailsRefreshInterval(): number {
    switch (this.state) {
      case 'details':
        if (this.object.completed) {
          return this.config.getDefaultRefreshInterval();
        }
        return this.config?.current?.settings?.refreshIntervals?.operationDetailsInterval;
      default:
        return null;
    }
  }
}
