import { Component } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IDetailsData } from '@objects-view/interfaces/details/details-data';
import {
  CartDetailsOverviewComponent
} from '@shared/common-tabs/billing/orders/cart-details-overview/cart-details-overview.component';
import { IObjectController } from '@objects-view/interfaces/object-controller';
import { StaticObjectController } from '@objects-view/static-object-controller';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';

@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.scss']
})
export class CartComponent {
  constructor(
    private activatedRoute: ActivatedRoute,
    private config: ConfigService,
    private router: Router,
  ) {
  }

  public detailsData: IDetailsData = {
    header: {
      title: {
        text: $localize`Current cart details`,
      }
    },
    tabs: [
      {
        tabName: $localize`Overview`,
        tabIdentifier: 'overview',
        component: CartDetailsOverviewComponent,
      },
    ],
    detailsActions: this.getDetailsActions(),
  };

  public objectController: IObjectController = new StaticObjectController(
    this.detailsData,
    null,
    {
      cartItems: this.activatedRoute.snapshot.data.cartItems,
    },
  );

  getDetailsActions() {
    let orderUrl: string;
    if (this.activatedRoute.snapshot.data.cartItems?.client) {
      orderUrl = this.config.getPanelUrl(`orders/select-product?filtering=cycles__currency__code:${this.activatedRoute.snapshot.data.cartItems.currency}`);
    } else {
      orderUrl = this.config.getPanelUrl('orders/create');
    }
    const detailsActions = [
      new RouterLinkAction({
          name: $localize`Order additional services`,
          routerUrl: orderUrl,
          router: this.router,
          noPermissions: !this.activatedRoute?.snapshot?.data?.cartItems?.item_permissions['cartitems.create'],
        }
      ),
    ] as any;
    detailsActions.push(new CallbackAction({
      name: $localize`Place order`,
      noPermissions: !this.activatedRoute?.snapshot?.data?.cartItems?.cart_permissions['cart.order'],
    }));
    return detailsActions;
  }
}
