import test, { expect, type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';

export class ProviderPreferencePage extends BasePage {
    readonly page: Page;
    readonly scheduleNowButton: Locator;
    readonly findAnotherShopButton: Locator;

    readonly acknowledgeAdasCheckbox: Locator;
    readonly gotItButton: Locator;
    readonly tpaRecalModal: Locator;
    readonly learnMoreLink: Locator;
    readonly yesButton: Locator;
    readonly noButton: Locator;
    readonly acknowledgeCheckbox: Locator;
    readonly tpaRecalModalContinueButton: Locator;
    readonly stateLawModalText: Locator;
    readonly stateLawRepairModalText: Locator;
    readonly stateLawModalOkayButton: Locator;
    issPageValue = 'provider-preference';

    constructor(page: Page) {
        super(page);
        this.page = page;
        
        this.scheduleNowButton = this.page.getByRole('button', { name: 'Schedule now' });
        this.findAnotherShopButton = this.page.getByRole('link', { name: 'Find another shop' });

        //Recal modal
        this.tpaRecalModal = this.page.locator('#TPARecalModal');
        this.learnMoreLink = this.page.locator('#moreDetails');
        this.yesButton = this.page.getByText('Yes', { exact: true }); // schedule with Safelite 
        this.noButton = this.page.getByText('No', { exact: true }); // schedule with TPA
        this.acknowledgeAdasCheckbox = this.page.getByRole('checkbox', { name: /I acknowledge/i }) 
        this.tpaRecalModalContinueButton = this.page.locator('#modalbtn', { hasText: 'Continue' });

        // State law modal - is there a difference between repair and replacement modals? need to check
        this.stateLawModalText = this.page.getByText(/law prohibits us from requiring you.*You have the right to select the motor vehicle repair shop of your choice\./is);
        this.stateLawRepairModalText = this.page.getByText(/We are prohibited by law from requiring that repairs be done at a specific automotive repair dealer. You are entitled to select the auto body repair shop to repair damage covered by us\./is);
        this.stateLawModalOkayButton = this.page.getByRole('button', { name: 'Okay' })
    }

    async selectProvider(isSafelite = true) {
        if (isSafelite) {
            await this.scheduleNowButton.click();
            await this.nextPage();
        }
        else {
            await this.findAnotherShopButton.waitFor({ state: 'visible' });
            await this.findAnotherShopButton.click();
            await this.tpaRecalModal.waitFor({ state: 'visible' });
            await this.scheduleTPAWithAdas();
        }
    }

    async scheduleWithSafeliteADAS() {
        await this.learnMoreLink.click();
        await this.yesButton.click();
        await this.tpaRecalModalContinueButton.click();
    }

    async scheduleTPAWithAdas() {
        await this.learnMoreLink.click();
        await this.noButton.click();
        await this.acknowledgeAdasCheckbox.click();
        await this.tpaRecalModalContinueButton.click();
    }

    async scheduleTPAWithoutAdas() {
        await this.findAnotherShopButton.click();
    }

    async validateStateLawModalIsVisible() {
        await expect.soft(this.stateLawModalText).toBeVisible();
    }

    async validateStateLawRepairModalIsVisible() {
        await expect.soft(this.stateLawRepairModalText).toBeVisible();
    }
}