import { expect, type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';
import { IPaymentDetails } from '@business-logic/types/CustomerDetails';
import { PaymentType } 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 payAtServiceButton: Locator;
    readonly havePaymentReadyMsg: Locator;
    readonly payNowButton: Locator;
    readonly payInFourButton: Locator;
    readonly paypalButton: Locator;
    readonly paymentPage: PaymentPage;
    readonly paypalPage: PaypalPage;
    issPageValue = 'payment-method';

    constructor(page: Page) {
        super(page);
        this.page = page;
        this.payAtServiceButton = this.page.locator('[buttonlabel="Pay at my appointment"]'); //this.page.getByText('Pay at time of service');
        this.havePaymentReadyMsg = this.page.getByText('Please have payment ready during your appointment.'); // it will be displayed when there is no payment option        
        this.payNowButton = this.page.locator('[buttonlabel="Pay now"]');
        this.payInFourButton = this.page.locator('[buttonlabel="Pay in 4 installments"]');
        this.paypalButton = this.page.frameLocator('iframe[name="card-frame"]').locator('div[id="paypalParentDiv"]');
        this.paymentPage = new PaymentPage(page);
        this.paypalPage = new PaypalPage(page);
    }

    async executePayment(paymentDetails: IPaymentDetails) {
        const browserContext = this.page.context();

        switch (paymentDetails.paymentType) {
            case PaymentType.Credit:
                await this.selectCreditCard();
                await this.paymentPage.populateCreditCardDetails(paymentDetails);
                break;
            case PaymentType.Paypal:
                await this.selectPaypal();
                await this.paypalPage.completePaypalPurchase(paymentDetails);
                break;
            case PaymentType.AfterPay:
                await this.payInFourButton.click();
                await this.nextPage();

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

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

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

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

    async selectCreditCard() {
        await this.payNowButton.click();
        await this.nextPage();
    }

    async selectPayAtService() {
        await this.payAtServiceButton.click();
    }

    // async validateAmountDue(customer){
    //     await this.amountDueDropDown.click();
    //     await expect(this.deductibleAmountTextField).toContainText(`${customer.deductibleAmount}`);
    // }
}