import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { OrderListComponent } from './order-list/order-list.component';
import { OrderDetailsComponent } from './order-details/order-details.component';
import { OrderListResolver } from '@fleio-api/billing/orders/order-list.resolver';
import { OrderResolver } from '@fleio-api/billing/orders/order.resolver';
import { AuthGuard } from '@shared/auth/auth.guard';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { OrderAddNewComponent } from './order-add-new/order-add-new.component';
import { SelectDomainComponent } from '@shared/common-tabs/billing/orders/select-domain/select-domain.component';
import { CartItemsListResolver } from '@fleio-api/billing/cart/cart-items-list.resolver';
import { CartComponent } from './cart/cart.component';
import { ProductListComponent } from '@shared/common-components/billing/order/product-list/product-list.component';
import { ProductListResolver } from '@fleio-api/billing/products/product-list.resolver';
import { OrderServicesRedirectResolver } from '@fleio-api/billing/orders/order-services-redirect.resolver';
import { OrderOptionsResolver } from '@fleio-api/billing/cart/order-options.resolver';
import { OrderProductComponent } from './order-product/order-product.component';
import { DomainOptionsComponent } from '@shared/common-tabs/billing/orders/domain-options/domain-options.component';
import { ClientCreateOptionsResolver } from '@fleio-api/client-user/client/client-create-options.resolver';
import { CartItemComponent } from '@shared/common-tabs/billing/orders/cart-item/cart-item.component';
import { CartItemResolver } from '@fleio-api/billing/cart/cart-item.resolver';
import { CartItemEditOptionsResolver } from '@fleio-api/billing/cart/cart-item-edit-options.resolver';
import { SelectProductRedirectResolver } from '@fleio-api/billing/orders/select-product-redirect.resolver';
import { OrderPermissionsResolver } from '@fleio-api/billing/orders/order-permissions.resolver';

const routes: Routes = [
  {
    path: '',
    component: OrderListComponent,
    resolve: {
      orders: OrderListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'billing.orders',
        getBreadCrumbDetail: () => $localize`Orders`,
        search: {
          show: true,
          placeholder: $localize`Search orders ...`,
        },
        ordering: {
          default: {
            field: 'order_date',
            display: $localize`Order date`,
            direction: OrderingDirection.Descending
          },
          options: [
            {
              field: 'id',
              display: $localize`Id`
            },
            {
              field: 'client',
              display: $localize`Client`
            },
            {
              field: 'user',
              display: $localize`User`
            },
            {
              field: 'order_date',
              display: $localize`Order date`,
            },
            {
              field: 'status',
              display: $localize`Status`,
            }
          ],
        },
        filterConfig: {
          availableOptions: [
            {
              display: $localize`Client`,
              field: 'client',
              items: 'clients',
              type: FilterTypes.CustomModel,
              itemsDisplayField: 'display',
            },
            {
              display: $localize`User`,
              field: 'user',
              items: 'users',
              type: FilterTypes.CustomModel,
              itemsDisplayField: 'full_name',
            },
            {
              display: $localize`Order date`,
              field: 'order_date',
              type: FilterTypes.Date,
            },
            {
              display: $localize`Status`,
              field: 'status',
              type: FilterTypes.Choices
            },
          ]
        },
        subheader: {
          objectName: $localize`order`,
          objectNamePlural: $localize`orders`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.orders;
          }
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'select-product',
    component: ProductListComponent,
    canActivate: [AuthGuard],
    resolve: {
      products: ProductListResolver,
      selectProductRedirectResolver: SelectProductRedirectResolver,
    },
    data: {
      config: {
        feature: 'billing.orders',
        getBreadCrumbDetail: () => $localize`Select product`,
      }
    }
  },
  {
    path: 'order-product/:code',
    component: OrderProductComponent,
    canActivate: [AuthGuard],
    resolve: {
      orderOptions: OrderOptionsResolver,
    },
    data: {
      config: {
        feature: 'billing.orders',
        getBreadCrumbDetail: (data) => $localize`Order ${data.orderOptions.name}`,
      } as IRouteConfig,
    }
  },
  {
    path: 'order-product/:code/:cycle',
    component: OrderProductComponent,
    canActivate: [AuthGuard],
    resolve: {
      orderOptions: OrderOptionsResolver,
    },
    data: {
      config: {
        feature: 'billing.orders',
        getBreadCrumbDetail: (data) => $localize`Order ${data.orderOptions.name}`,
      } as IRouteConfig,
    }
  },
  {
    path: 'order-product/:code/:domain_action/:domain_name',
    component: OrderProductComponent,
    canActivate: [AuthGuard],
    resolve: {
      orderOptions: OrderOptionsResolver,
    },
    data: {
      config: {
        feature: 'billing.order',
        getBreadCrumbDetail: (data) => $localize`Order ${data.orderOptions.name}`,
      } as IRouteConfig,
    }
  },
  {
    path: 'create/:client',
    component: OrderAddNewComponent,
    resolve: {
      orderServicesRedirect: OrderServicesRedirectResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Create order`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'create',
    component: OrderAddNewComponent,
    resolve: {
      orderServicesRedirect: OrderServicesRedirectResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Create order`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'select-domain/:code',
    component: SelectDomainComponent,
    canActivate: [AuthGuard],
    resolve: {
    },
    data: {
      config: {
        feature: 'plugins.domains',
        getBreadCrumbDetail: () => $localize`Select domain`,
      } as IRouteConfig,
    }
  },
  {
    path: 'domain-options/:cart_id/:domain_action/:domain_name',
    component: DomainOptionsComponent,
    canActivate: [AuthGuard],
    resolve: {
      clientCreateOptions: ClientCreateOptionsResolver,
    },
    data: {
      config: {
        feature: 'plugins.domains',
        getBreadCrumbDetail: () => $localize`Domain options`,
      } as IRouteConfig,
    }
  },
  {
    path: 'cart/:id',
    component: CartItemComponent,
    resolve: {
      cartItem: CartItemResolver,
      orderOptions: CartItemEditOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.cartItem.name,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'cart',
    component: CartComponent,
    canActivate: [AuthGuard],
    resolve: {
      cartItems: CartItemsListResolver,
    },
    data: {
      config: {
        feature: 'billing.order',
        getBreadCrumbDetail: () => $localize`My cart`,
      } as IRouteConfig
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id',
    component: OrderDetailsComponent,
    resolve: {
      order: OrderResolver,
      permissions: OrderPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Order ${data.order.id}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
];

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