import { ILBListenerModel } from '@fleio-api/openstack/load-balancing/model/lb-listener.model';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { LBListenersApiService } from '@fleio-api/openstack/load-balancing/listeners/lb-listeners-api.service';
import { ConfigService } from '@shared/config/config.service';
import { Router } from '@angular/router';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { LBPoliciesApiService } from '@fleio-api/openstack/load-balancing/policies/lb-policies-api.service';
import { ILBPolicyModel } from '@fleio-api/openstack/load-balancing/model/lb-policy.model';
import { IAction } from '@objects-view/interfaces/actions/action';
import { ILBRuleModel } from '@fleio-api/openstack/load-balancing/model/lb-rule.model';
import { LBRulesApiService } from '@fleio-api/openstack/load-balancing/rules/lb-rules-api.service';

export class ListenerUtils {
  static getListenerActions(
    listener: ILBListenerModel, lbListenersApiService: LBListenersApiService,
    config: ConfigService, router: Router, loadBalancerId: FleioId,
  ) {
    const actions = [];
    actions.push(
      new RouterLinkAction({
          name: $localize`Edit listener`,
          tooltip: $localize`Edit listener`,
          icon: {name: 'edit'},
          router,
          routerUrl: config.getPanelUrl(
            `openstack/load-balancers/${loadBalancerId}/listeners/${listener.id}/edit`,
          ),
          propagateQueryParams: ['region'],
        }
      )
    );
    actions.push(new ApiCallAction(
      {
        object: listener,
        icon: {name: 'delete'},
        tooltip: $localize`Delete listener`,
        name: $localize`Delete listener`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete listener`,
          message: $localize`Are you sure you want to delete listener ${listener.name || '(none)'}?`
        },
        successMessage: $localize`Listener delete scheduled`,
        errorMessage: $localize`Failed to delete listener, please contact support`,
        apiService: lbListenersApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: config.getPanelUrl(
          'openstack/load-balancers', loadBalancerId
        ),
        redirectUrlFragment: 'listeners',
        propagateQueryParams: ['region'],
      }
    ));
    return actions;
  }

  static getPolicyActions(
    policy: ILBPolicyModel, lbPoliciesApiService: LBPoliciesApiService, config: ConfigService,
    router: Router, loadBalancerId: FleioId, listenerId: FleioId) {
    const actions = [];
    actions.push(
      new RouterLinkAction({
          name: $localize`Edit policy`,
          tooltip: $localize`Edit policy`,
          icon: {name: 'edit'},
          router,
          routerUrl: config.getPanelUrl(
            `openstack/load-balancers/${loadBalancerId}/listeners/${listenerId}/policy-edit/${policy.id}`,
          ),
          propagateQueryParams: ['region'],
        }
      )
    );
    actions.push(new ApiCallAction(
      {
        object: policy,
        icon: {name: 'delete'},
        tooltip: $localize`Delete policy`,
        name: $localize`Delete policy`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete policy`,
          message: policy.name ?
            $localize`Are you sure you want to delete policy ${policy.name}?` :
            $localize`Are you sure you want to delete policy (none)?`,
        },
        successMessage: $localize`Policy delete scheduled`,
        errorMessage: $localize`Failed to delete policy, please contact support`,
        apiService: lbPoliciesApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: config.getPanelUrl(
          `openstack/load-balancers/${loadBalancerId}/listeners`, listenerId,
        ),
        redirectUrlFragment: 'l7-policies',
        propagateQueryParams: ['region'],
      }
    ));
    return actions;
  }

  static getRuleActions(
    rule: ILBRuleModel, lbRulesApiService: LBRulesApiService, config: ConfigService,
    router: Router, loadBalancerId: FleioId, listenerId: FleioId, policyId: FleioId,
  ): IAction[] {
    const actions: IAction[] = [];
    actions.push(
      new RouterLinkAction({
          name: $localize`Edit rule`,
          tooltip: $localize`Edit rule`,
          icon: {name: 'edit'},
          router,
          routerUrl: config.getPanelUrl(
            `openstack/load-balancers/${loadBalancerId}/listeners/${listenerId}/policy/${policyId}/rule-edit/`,
            rule.id,
          ),
          propagateQueryParams: ['region'],
        }
      )
    );
    actions.push(new ApiCallAction(
      {
        object: rule,
        icon: {name: 'delete'},
        tooltip: $localize`Delete rule`,
        name: $localize`Delete rule`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete rule`,
          message: $localize`Are you sure you want to delete rule?`,
        },
        successMessage: $localize`Rule delete scheduled`,
        errorMessage: $localize`Failed to delete rule, please contact support`,
        apiService: lbRulesApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: config.getPanelUrl(
          `openstack/load-balancers/${loadBalancerId}/listeners/${listenerId}/policy`, policyId,
        ),
        redirectUrlFragment: 'l7-rules',
        propagateQueryParams: ['region'],
      }
    ));
    return actions;
  }
}
