import { type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';
import { VehicleLookupType } from 'safelite-playwright-core';
import { ProgressBarPercentages } from 'framework/localTypes/Enums';
import { IVehicleDetails } from 'safelite-playwright-core';
import { VinLookupPage } from './VinLookupPage';
import { VehicleLookupAddressPage } from './VehicleLookupAddressPage';
import { VehicleLookupLicensePage } from './VehicleLookupLicensePage';
import { step } from 'framework/localTypes/Step';
import { ITestData } from 'framework/TestData';

export class EstimatePage extends BasePage {
    readonly page: Page;
    readonly vinLookupButton: Locator;
    readonly addressLookupButton: Locator; 
    readonly licenseLookupButton: Locator;
    readonly vinLookupPage: VinLookupPage;
    readonly zipLookupButton: Locator;
    readonly vehicleLookupAddressPage: VehicleLookupAddressPage;
    readonly vehicleLookupLicensePage: VehicleLookupLicensePage;

    constructor(page: Page) {
        super(page);
        this.page = page;
        this.vinLookupButton = page.locator('label').filter({ hasText: 'Provide my VIN' }).locator('div');
        this.zipLookupButton = page.locator('label').filter({ hasText: 'I\'d rather not share my VIN' }).locator('div');
        this.addressLookupButton = page.getByLabel('Provide my home address', { exact: true });
        this.licenseLookupButton = page.getByLabel('Provide my license plate #', { exact: true });
        this.vinLookupPage = new VinLookupPage(page);
    }

    async vehicleLookup(vehicleDetails: IVehicleDetails) {
        switch (vehicleDetails.vehicleLookupType) {
            case VehicleLookupType.Address:
                await this.selectAddressLookup();
                await this.nextPage();
                break;
            case VehicleLookupType.LicensePlateNumber:
                await this.selectLicenseLookup();
                await this.nextPage();
                break;
            case VehicleLookupType.Vin:
                await this.selectVinLookup();
                await this.nextPage();
                break;
            case VehicleLookupType.Zip:
                await this.selectZipLookup();
                await this.nextPage();
                break;
            default:
                console.error('EstimatePage >> DATA ISSUE: VehicleLookupType not provided');
                break;
        }
    }

    async selectVinLookup(){
        await this.vinLookupButton.click();
    }

    async selectAddressLookup(){
        await this.addressLookupButton.click();
    }

    async selectLicenseLookup(){
        await this.licenseLookupButton.click();
    }

    async selectZipLookup(){
        await this.zipLookupButton.click();
    }

    @step("EstimatePage >> Select Lookup Type")
    async handleEstimatePage(testData: Partial<ITestData>) {
        const { vehicleDetails } = testData;
        await this.validateProgressBar(ProgressBarPercentages.EstimatePage);
        await this.vehicleLookup(vehicleDetails!);
    }
}
