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

@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() {
    const detailsActions = [
      new RouterLinkAction({
          name: $localize`Order additional services`,
          routerUrl: this.config.getPanelUrl(`billing/order`),
          router: this.router,
          noPermissions: !this.activatedRoute?.snapshot?.data?.cartItems?.item_permissions['cartitems.create'],
        }
      ),
    ] as any;
    if (!this.config.authService?.userData?.user?.id) {
      detailsActions.push(
        new RouterLinkAction({
            name: $localize`Have an account? Login to order`,
            routerUrl: this.config.getPanelUrl(`login`),
            router: this.router,
          }
        )
      );
      detailsActions.push(
        new RouterLinkAction({
            name: $localize`Register to order`,
            routerUrl: this.config.getPanelUrl(`signup`),
            router: this.router,
          }
        )
      );
    } else {
      detailsActions.push(new CallbackAction({
        name: $localize`Place order`,
        noPermissions: !this.activatedRoute?.snapshot?.data?.cartItems?.cart_permissions['cart.order'],
      }));
    }
    return detailsActions;
  }
}
