import { IPricingPlanModel } from '@fleio-api/openstack/model/pricing-plan.model';
import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { IObjectStatus } 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 { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { CallbackAction } from '@objects-view/actions/callback-action';
import {
  PricingPlanDeleteComponent
} from '@shared/common-dialogs/settings/openstack/openstack-plans/pricing-plan-delete/pricing-plan-delete.component';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { MatDialog } from '@angular/material/dialog';
import { Observable } from 'rxjs';
import {
  PricingPlanDetailsOverviewComponent
} from './tabs/pricing-plan-details-overview/pricing-plan-details-overview.component';
import { PricingPlanEditFormComponent } from './tabs/pricing-plan-edit-form/pricing-plan-edit-form.component';
import { PricingRuleEditFormComponent } from './tabs/pricing-rule-edit-form/pricing-rule-edit-form.component';

export class PricingPlanUIService extends ObjectUIServiceBase<IPricingPlanModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly matDialog: MatDialog;


  constructor(
    pricingPlan: IPricingPlanModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, matDialog: MatDialog,
  ) {
    super(pricingPlan, permissions, state);
    this.matDialog = matDialog;
    this.router = router;
    this.config = config;
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

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

      case 'edit':
        return {
          text: $localize`Edit ${this.object.name}`,
        };

      case 'create':
        return {
          text: $localize`Create pricing plan`,
        };

      case 'create-rule':
        return {
          text: $localize`Create pricing rule`,
        };

      case 'edit-rule':
        return {
          // @ts-ignore
          text: $localize`Edit ${this.object.display_name}`,
        };

      default:
        return {
          text: `${this.object.name}`,
        };
    }
  }

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

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

    actions.push(new RouterLinkAction({
        icon: {name: 'add'},
        name: $localize`Create pricing rule`,
        tooltip: $localize`Create pricing rule`,
        routerUrl: this.config.getPanelUrl(`settings/openstack-plans/${this.object.id}/create-rule`),
        router: this.router,
      }
    ));

    actions.push(new CallbackAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: $localize`Delete`,
        name: $localize`Delete`,
        noPermissions: !this.permissions['billing-plan.destroy'],
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('settings/openstack-plans'),
        callback: () => new Observable<IActionResult>((subscriber) => {
          this.matDialog.open(
            PricingPlanDeleteComponent, {
              data: {
                planToDelete: this.object,
                isStaff: true,
              },
              autoFocus: false,
            }).afterClosed().subscribe(result => {
            subscriber.next({success: !!result, message: null});
            subscriber.complete();
          })
        })
      }
    ));

    return actions;
  }

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

  getCardFields(): IDataField[] {
    return [
      {
        name: $localize`Services count`,
        value: `${this.object.services_count}`
      },
      {
        name: $localize`Pricing rules count`,
        value: `${this.object.pricing_rules.length}`
      },
      {
        name: $localize`Currency`,
        value: `${this.object.currency}`
      },
    ];
  }

  getCardTags(): string[] {
    const tags = [];
    if (this.object.is_default) {
      tags.push($localize`default`);
    }
    return tags;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: PricingPlanDetailsOverviewComponent,
          },
        ];
      case 'edit':
      case 'create':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: PricingPlanEditFormComponent,
          },
        ];
      case 'create-rule':
        return [
          {
            tabName: $localize`Create rule`,
            tabIdentifier: 'create-rule',
            component: PricingRuleEditFormComponent,
          },
        ];
      case 'edit-rule':
        return [
          {
            tabName: $localize`Edit rule`,
            tabIdentifier: 'edit-rule',
            component: PricingRuleEditFormComponent,
          },
        ];
    }
  }

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

    switch (this.state) {
      case 'create':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`settings/openstack-plans`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create`}));
        break;
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`settings/openstack-plans`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      case 'details':
        actions.push(new RouterLinkAction({
            name: $localize`Back`,
            routerUrl: this.config.getPrevUrl(`settings/openstack-plans`),
            router: this.router,
          }
        ));
        actions.push(new RouterLinkAction({
            name: $localize`Create pricing rule`,
            routerUrl: this.config.getPanelUrl(`settings/openstack-plans/${this.object.id}/create-rule`),
            router: this.router,
          }
        ));
        break;
      case 'create-rule':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`settings/openstack-plans`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create`}));
        break;
      case 'edit-rule':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`settings/openstack-plans`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      default:
        break;
    }

    return actions;
  }
}
