import { expect, type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';
import { IEndorsementDetails } from 'safelite-playwright-core';
import { EndorsementType } from 'safelite-playwright-core';
import { InsuranceBasePage } from './InsuranceBasePage';
import { step } from 'framework/localTypes/Step';
import { ITestData } from 'framework/TestData';

export class EndorsementsPage extends InsuranceBasePage {
    readonly page: Page;
    readonly educatorYesButton: Locator;
    readonly educatorNoButton: Locator;

    constructor(page: Page) {
        super(page);
        this.page = page;
        this.educatorYesButton = this.page.getByRole('link', { name: 'Yes' });
        this.educatorNoButton = this.page.getByRole('link', { name: 'Yes' });
    }

    async verifyEndorsements(endorsements: IEndorsementDetails[]) {
        for (const endorsement of endorsements) {
            switch(endorsement.endorsementType) {
                case EndorsementType.Educator:
                    if (endorsement.isOnPolicy) {
                        await expect.soft(this.educatorYesButton).toBeAttached();
                    } else {
                        await expect.soft(this.educatorYesButton).not.toBeAttached();
                    }
                    break;
                case EndorsementType.EmployeeParking:
                    // TODO: Implement
                    break;
            }
        }
    }

    async selectEndorsements(endorsements: IEndorsementDetails[]) {
        for (const endorsement of endorsements) {
            if (endorsement.isOnPolicy) {
                switch (endorsement.endorsementType) {
                    case EndorsementType.Educator:
                        if (endorsement.isClickYes) {
                            await this.educatorYesButton.click();
                        } else {
                            await this.educatorNoButton.click();
                        }
                        break;
                    case EndorsementType.EmployeeParking:
                        // TODO: Implement
                        break;
                }
            }
        }
    }

    @step("EndorsementsPage >> Select endorsements: ")
    async handleEndorsementsPage(testData: Partial<ITestData>) {
        const { endorsements } = testData;
        await this.verifyEndorsements(endorsements!);
        await this.selectEndorsements(endorsements!);
        await this.nextPage();
    }
}