import { expect, type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';
import { IClaimDetails, IPaymentDetails } from '@business-logic/types/CustomerDetails';
import { PaymentType, ServicePackage } from '@business-logic/types/Enums';
import { PaymentPage } from './PaymentPage';
import { AfterpayPage } from './AfterpayPage';
import { PaypalPage } from './PaypalPage';

export class PaymentMethodPage extends BasePage {
    readonly page: Page;
    readonly paypalPage: PaypalPage;
    readonly paymentPage: PaymentPage;

    readonly payNowButton: Locator;
    readonly paypalButton: Locator;
    readonly payInFourButton: Locator;
    readonly payAtAppointmentButton: Locator;

    readonly textReminderYesButton: Locator;
    readonly textReminderNoButton: Locator;
    readonly continueToCheckoutButton: Locator;
    readonly submitButton: Locator;

    issPageValue = 'payment-method';

    constructor(page: Page) {
        super(page);
        this.page = page;
        this.paypalPage = new PaypalPage(page);
        this.paymentPage = new PaymentPage(page);

        this.payNowButton = this.page.locator('[buttonlabel="Pay now"]'); // credit and paypal options behind this button
        this.paypalButton = this.page.frameLocator('iframe[name="card-frame"]').locator('div[id="paypalParentDiv"]');
        this.payInFourButton = this.page.locator('[buttonlabel="Pay in 4 installments"]'); // Afterpay        
        this.payAtAppointmentButton = this.page.locator('[buttonlabel="Pay at my appointment"]'); 
        
        this.textReminderYesButton = this.page.locator('div.button-content', { hasText: /^yes$/i });
        this.textReminderNoButton = this.page.locator('div.button-content', { hasText: 'No' });
        this.continueToCheckoutButton = this.page.getByRole('button', { name: 'Continue to checkout' });
        this.submitButton = this.page.getByRole('button', { name: 'Submit' });
    }

    async executePayment(paymentDetails: IPaymentDetails, claimDetails: IClaimDetails, servicePackage: ServicePackage) {
        const browserContext = this.page.context();

        switch (paymentDetails.paymentType) {
            case PaymentType.Credit:
                await this.payNowButton.click();
                await this.textReminderYesButton.click();
                await this.continueToCheckoutButton.click();
                await this.selectCreditCard(paymentDetails);
                break;
            case PaymentType.Paypal:
                await this.payNowButton.click();
                await this.textReminderYesButton.click();
                await this.continueToCheckoutButton.click();
                await this.selectPaypal();
                await this.paypalPage.completePaypalPurchase(paymentDetails);
                break;
            case PaymentType.AfterPay:
                await this.payInFourButton.click();
                await this.textReminderYesButton.click();
                await this.continueToCheckoutButton.click();

                // Capture popup
                const afterpayPopup = await browserContext.waitForEvent('page');
                const afterpayPage = new AfterpayPage(afterpayPopup);

                // Execute payment
                await afterpayPage.executeAfterpayPayment(paymentDetails, claimDetails, servicePackage);
                break;

            case PaymentType.PayAtService:
                await this.payAtAppointmentButton.click();
                await this.textReminderYesButton.click();
                await this.submitButton.click();
                break;
            default:
                console.error('PaymentMethodPage >> Logic for this payment method unimplemented');
                break;
        }
    }

    async selectPaypal() {
        await this.paypalButton.click();
    }

    async selectCreditCard(paymentDetails: IPaymentDetails) {
        await this.paymentPage.populateCreditCardDetails(paymentDetails);
    }
 
    async selectPayAtAppointment() {
        await this.payAtAppointmentButton.click();
    }

    async submitOrderWithoutPIA() {
        await this.textReminderYesButton.click();
        await this.submitButton.click();
    }
}