import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { TextButtonComponent } from '@isa/ui/buttons';
import { SkeletonLoaderDirective } from '@isa/ui/skeleton-loader';
import { injectTabId } from '@isa/core/tabs';
import { DomainCheckoutService } from '@domain/checkout';
import { injectFeedbackDialog } from '@isa/ui/dialog';
import { RewardSelectionService } from '../service/reward-selection.service';
import { Router } from '@angular/router';
import { CheckoutNavigationService } from '@shared/services/navigation';

@Component({
  selector: 'lib-reward-selection-trigger',
  templateUrl: './reward-selection-trigger.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [TextButtonComponent, SkeletonLoaderDirective],
  providers: [RewardSelectionService],
})
export class RewardSelectionTriggerComponent {
  #router = inject(Router);
  #tabId = injectTabId();
  #feedbackDialog = injectFeedbackDialog();
  #rewardSelectionService = inject(RewardSelectionService);
  #domainCheckoutService = inject(DomainCheckoutService);
  #checkoutNavigationService = inject(CheckoutNavigationService);

  canOpen = this.#rewardSelectionService.canOpen;
  isLoading = this.#rewardSelectionService.isLoading;

  async openRewardSelectionDialog() {
    const tabId = this.#tabId();
    const dialogResult = await this.#rewardSelectionService.open({
      closeText: 'Abbrechen',
    });

    if (dialogResult && tabId) {
      await this.#reloadShoppingCart(tabId);

      // Wenn der User alles 0 setzt, bleibt man einfach auf der aktuellen Seite
      if (dialogResult.rewardSelectionItems?.length === 0) {
        await this.#feedback();
      }

      if (dialogResult.rewardSelectionItems?.length > 0) {
        const hasRegularCartItems = dialogResult.rewardSelectionItems?.some(
          (item) => item?.cartQuantity > 0,
        );
        const hasRewardCartItems = dialogResult.rewardSelectionItems?.some(
          (item) => item?.rewardCartQuantity > 0,
        );

        await this.#feedback();

        // Wenn Nutzer im Warenkorb ist und alle Items als Prämie setzt -> Navigation zum Prämien Checkout
        if (!hasRegularCartItems && hasRewardCartItems) {
          await this.#navigateToRewardCheckout(tabId);
        }

        // Wenn Nutzer im Prämien Checkout ist und alle Items in den Warenkorb setzt -> Navigation zu Warenkorb
        if (hasRegularCartItems && !hasRewardCartItems) {
          await this.#navigateToCheckout(tabId);
        }
      }
    }
  }

  async #reloadShoppingCart(tabId: number) {
    await this.#domainCheckoutService.reloadShoppingCart({
      processId: tabId,
    });
  }

  async #feedback() {
    this.#feedbackDialog({
      data: { message: 'Auswahl gespeichert' },
    });
  }

  async #navigateToRewardCheckout(tabId: number) {
    await this.#router.navigate([`/${tabId}`, 'reward', 'cart']);
  }

  async #navigateToCheckout(tabId: number) {
    await this.#checkoutNavigationService
      .getCheckoutReviewPath(tabId)
      .navigate();
  }
}
