import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { ConfigService } from '@shared/config/config.service';
import { Router } from '@angular/router';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { ILBPoolModel } from '@fleio-api/openstack/load-balancing/model/lb-pool.model';
import { LBPoolsApiService } from '@fleio-api/openstack/load-balancing/pools/lb-pools-api.service';
import { ILBHealthMonitorModel } from '@fleio-api/openstack/load-balancing/model/lb-health-monitor.model';
import {
  LBHealthMonitorsApiService
} from '@fleio-api/openstack/load-balancing/health-monitors/lb-health-monitors-api.service';
import { ILBMemberModel } from '@fleio-api/openstack/load-balancing/model/lb-member.model';
import { LBMembersApiService } from '@fleio-api/openstack/load-balancing/members/lb-members-api.service';

export class PoolsUtils {
  static getPoolActions(
    pool: ILBPoolModel, lbPoolsApiService: LBPoolsApiService,
    config: ConfigService, router: Router, loadBalancerId: FleioId, listenerId = null,
  ) {
    const actions = [];
    const queryParams = {} as Record<string, string>;
    if (listenerId) {
      queryParams.listenerId = listenerId;
    }
    actions.push(
      new RouterLinkAction({
          name: $localize`Edit pool`,
          tooltip: $localize`Edit pool`,
          icon: {name: 'edit'},
          router,
          routerUrl: config.getPanelUrl(
            `openstack/load-balancers/${loadBalancerId}/pools/${pool.id}/edit`,
          ),
          queryParams: queryParams,
          propagateQueryParams: ['region'],
        }
      )
    );
    actions.push(new ApiCallAction(
      {
        object: pool,
        icon: {name: 'delete'},
        tooltip: $localize`Delete pool`,
        name: $localize`Delete pool`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete pool`,
          message: $localize`Are you sure you want to delete pool ${pool.name || '(none)'}?`
        },
        successMessage: $localize`Pool delete scheduled`,
        errorMessage: $localize`Failed to delete pool, please contact support`,
        apiService: lbPoolsApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: config.getPanelUrl(
          'openstack/load-balancers', loadBalancerId,
        ),
        redirectUrlFragment: 'pools',
        propagateQueryParams: ['region'],
      }
    ));
    return actions;
  }

  static getMonitorActions(
    monitor: ILBHealthMonitorModel, lbHealthMonitorsApiService: LBHealthMonitorsApiService,
    config: ConfigService, router: Router, loadBalancerId: FleioId, poolId: FleioId,
  ) {
    const actions = [];
    actions.push(
      new RouterLinkAction({
          name: $localize`Edit health monitor`,
          tooltip: $localize`Edit health monitor`,
          icon: {name: 'edit'},
          router,
          routerUrl: config.getPanelUrl(
            `openstack/load-balancers/${loadBalancerId}/pools/${poolId}/health-monitor-edit/${monitor.id}`,
          ),
          propagateQueryParams: ['region'],
        }
      )
    );
    actions.push(new ApiCallAction(
      {
        object: monitor,
        icon: {name: 'delete'},
        tooltip: $localize`Delete health monitor`,
        name: $localize`Delete health monitor`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete health monitor`,
          message: $localize`Are you sure you want to delete health monitor ${monitor.name || '(none)'}?`
        },
        successMessage: $localize`Health monitor delete scheduled`,
        errorMessage: $localize`Failed to delete health monitor, please contact support`,
        apiService: lbHealthMonitorsApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: config.getPanelUrl(
          `openstack/load-balancers/${loadBalancerId}/pools/${poolId}`
        ),
        redirectUrlFragment: 'health-monitors',
        propagateQueryParams: ['region'],
      }
    ));
    return actions;
  }

  static getMemberActions(
    member: ILBMemberModel, lbMembersApiService: LBMembersApiService,
    config: ConfigService, router: Router, loadBalancerId: FleioId, poolId: FleioId,
  ) {
    const actions = [];
    actions.push(
      new RouterLinkAction({
          name: $localize`Edit member`,
          tooltip: $localize`Edit member`,
          icon: {name: 'edit'},
          router,
          routerUrl: config.getPanelUrl(
            `openstack/load-balancers/${loadBalancerId}/pools/${poolId}/member-edit/${member.id}`,
          ),
          propagateQueryParams: ['region'],
        }
      )
    );
    actions.push(new ApiCallAction(
      {
        object: member,
        icon: {name: 'delete'},
        tooltip: $localize`Delete member`,
        name: $localize`Delete member`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete member`,
          message: $localize`Are you sure you want to delete member ${member.name || '(none)'}?`
        },
        successMessage: $localize`Member delete scheduled`,
        errorMessage: $localize`Failed to delete member, please contact support`,
        apiService: lbMembersApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: config.getPanelUrl(
          `openstack/load-balancers/${loadBalancerId}/pools/${poolId}`
        ),
        redirectUrlFragment: 'members',
        propagateQueryParams: ['region'],
      }
    ));
    return actions;
  }
}
