import test, { expect, type Locator, type Page } from '@playwright/test';
import { error } from 'console';

/**
 * Base class for all page objects.
 * Provides common functionality for interacting with pages.
 */
export class BasePage {
    // Common locators
    readonly page: Page;
    readonly continueButton: Locator;
    readonly backButton: Locator;
    readonly pageSpinner: Locator;
    readonly buttonLoadSpin: Locator;
    readonly hamburgerMenu: Locator;

    constructor(page: Page){
        this.page = page;
        this.continueButton = page.locator('[id="infoBox"]').getByRole('button');
        this.backButton = page.locator('[id="infoBox"]').getByRole('link');
        this.pageSpinner = page.getByRole('status');
        this.buttonLoadSpin = page.getByRole('alert');
        this.hamburgerMenu = this.page.getByRole('button', { name: 'Hamburger Menu (modal window)' });
    }
    
    async nextPage() {
        const startingUrl = this.page.url();
        await expect(async () => {
            const currentUrl = this.page.url();
            if (currentUrl === startingUrl) {
                await this.continueButton.click({ timeout: 1000 });
            }
            expect(currentUrl).not.toEqual(startingUrl);          
        }).toPass({ timeout: 240_000 });
    }
    
    async previousPage() {
        const startingUrl = this.page.url();
        await expect(async () => {
            const currentUrl = this.page.url();
            if (currentUrl === startingUrl) {
                await this.backButton.click({ timeout: 1000 });
            }
            expect(currentUrl).not.toEqual(startingUrl);          
        }).toPass({ timeout: 240_000 });
    }

    async validateURL(url:string){
        await expect(this.pageSpinner).toHaveCount(0, {timeout: 60000});
        await this.page.waitForURL(url);
    }

    async fillAndValidate(element: Locator, value: string){
        await expect(async () => {
            await element.clear();
            await element.fill(value);
            await expect(element).toHaveValue(value);
        }).toPass();
    }

    async clickWithRetry(element, page) {
        const timeout = 5000; // milli seconds
        const startTime = Date.now();
        while (Date.now() - startTime < timeout) {
            try {
                if (await element.isEnabled()) {
                    await element.click();
                    await element.keyboard.press('Tab');
                    return; // Exit loop if click succeeds
                }
            } catch (error) {
                // Ignore error and retry
            }
            await page.waitForTimeout(100); // Small delay before retrying
        }
        console.log(`Failed to click the the element within ${timeout/1000} seconds` + error);
    }

    async logReferralNumber() {
        let mainLocalStorage = JSON.parse(await this.page.evaluate('localStorage.getItem(\'main\')'));
        let referralNumber = mainLocalStorage.order.referralNumber as number;
        let referralSequenceNumber = mainLocalStorage.order.referralSequenceNumber as number;
        if (referralNumber == null) {
            for (let i = 1; i <= 20; i++) {
                if (!referralNumber == null) break;
                await this.page.waitForTimeout(500);
                mainLocalStorage = JSON.parse(await this.page.evaluate('localStorage.getItem(\'main\')'));
                referralNumber = mainLocalStorage.order.referralNumber as number;
                referralSequenceNumber = mainLocalStorage.order.referralSequenceNumber as number;
            }
        }

        await test.step(`Referral Number:${referralNumber} Referral Sequence Number:${referralSequenceNumber}`, async () => {
            console.log(`Referral Number:${referralNumber}`);
            console.log(`Referral Sequence Number:${referralSequenceNumber}`);
        });

    }
}