import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { OpenstackPlanListComponent } from './openstack-plan-list/openstack-plan-list.component';
import { OpenstackPlanDetailsComponent } from './openstack-plan-details/openstack-plan-details.component';
import { OpenstackPlanEditComponent } from './openstack-plan-edit/openstack-plan-edit.component';
import { OpenstackPlanCreateComponent } from './openstack-plan-create/openstack-plan-create.component';
import { PricingPlanListResolver } from '@fleio-api/openstack/pricing-plan/pricing-plan-list.resolver';
import { PricingPlanResolver } from '@fleio-api/openstack/pricing-plan/pricing-plan.resolver';
import { PricingPlanCreateOptionsResolver } from '@fleio-api/openstack/pricing-plan/pricing-plan-create-options.resolver';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { AuthGuard } from '@shared/auth/auth.guard';
import { PricingRuleCreateOptionsResolver } from '@fleio-api/openstack/pricing-rule/pricing-rule-create-options.resolver';
import { OpenstackPlanCreateRuleComponent } from './openstack-plan-create-rule/openstack-plan-create-rule.component';
import { PricingRuleResolver } from '@fleio-api/openstack/pricing-rule/pricing-rule.resolver';
import { OpenstackPlanRuleEditComponent } from './openstack-plan-rule-edit/openstack-plan-rule-edit.component';
import { PricingPlanPermissionsResolver } from '@fleio-api/openstack/pricing-plan/pricing-plan-permissions.resolver';

const routes: Routes = [
  {
    path: '',
    component: OpenstackPlanListComponent,
    resolve: {
      pricingPlans: PricingPlanListResolver,
      createOptions: PricingPlanCreateOptionsResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.plans',
        search: {
          show: true,
          placeholder: $localize`Search plans ...`,
        },
        ordering: {
          default: {
            field: 'name',
            display: $localize`Name`,
            direction: OrderingDirection.Ascending
          },
          options: [
            {
              field: 'name',
              display: $localize`Name`
            },
            {
              field: 'currency',
              display: $localize`Currency`
            },
          ],
        },
        subheader: {
          objectName: 'OpenStack plan',
          objectNamePlural: 'OpenStack plans',
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.pricingPlans;
          }
        },
        getBreadCrumbDetail: () => $localize`OpenStack plans`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: OpenstackPlanCreateComponent,
    resolve: {
      createOptions: PricingPlanCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create new pricing plan`,
      } as IRouteConfig,
    },
  },
  {
    path: ':id',
    component: OpenstackPlanDetailsComponent,
    resolve: {
      pricingPlan: PricingPlanResolver,
      permissions: PricingPlanPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Pricing plan ${data.pricingPlan.name}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id/create-rule',
    component: OpenstackPlanCreateRuleComponent,
    resolve: {
      pricingPlan: PricingPlanResolver,
      createOptions: PricingRuleCreateOptionsResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.plans',
        getBreadCrumbDetail: () => $localize`Create pricing rule`,
      } as IRouteConfig,
    },
  },
  {
    path: ':id/edit-rule',
    component: OpenstackPlanRuleEditComponent,
    resolve: {
      pricingRule: PricingRuleResolver,
      createOptions: PricingRuleCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit pricing rule ${data.pricingRule.display_name}`,
      } as IRouteConfig,
    },
  },
  {
    path: ':id/edit',
    component: OpenstackPlanEditComponent,
    resolve: {
      pricingPlan: PricingPlanResolver,
      createOptions: PricingPlanCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit pricing plan ${data.pricingPlan.name}`,
      } as IRouteConfig,
    },
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class OpenstackPlansRoutingModule { }
