import { type Locator, type Page, expect, test } from '@playwright/test';
import { BasePage } from './BasePage';
import { SideDoorDamage, VehicleDamage, WindshieldDamage } from '@business-logic/types/Enums';
import TestSuccessAlert from '@business-logic/types/TestSuccessAlert';

export class VehicleDamagePage extends BasePage {
    readonly page: Page;
    readonly windshieldChkBox: Locator;
    readonly crackButton: Locator;
    readonly chipButton: Locator;
    readonly sideDoorButton: Locator;
    readonly driverSideButton: Locator;
    readonly passengerSideButton: Locator;
    readonly driverQuarterPanelChkBox: Locator;
    readonly driverFrontDoorChkBox: Locator;
    readonly driverBackDoorChkBox: Locator;
    readonly driverSlidingDoorChkBox: Locator;
    readonly driverVentGlassChkBox: Locator;
    readonly passengerQuarterPanelChkBox: Locator;
    readonly passengerFrontDoorChkBox: Locator;
    readonly passengerBackDoorChkBox: Locator;
    readonly passengerVentGlassChkBox: Locator;
    readonly rearWindowChkBox: Locator;
    readonly rearStationaryBttn: Locator;
    readonly rearSlidingGlassBttn: Locator;
    readonly editVehicleLink: Locator;

    readonly noReplacementAvailableAlert: Locator;
    readonly bothReplaceRepairAlert: Locator;
    url = process.env['BASE_URL']! + '/fmg/?fmgPage=vehicle-damage';

    constructor(page: Page) {
        super(page);
        this.page = page;
        this.windshieldChkBox = this.page.locator('[buttonlabel="Windshield"]');
        this.crackButton = this.page.locator('[buttonlabel="Crack"]');
        this.chipButton = this.page.locator('[buttonlabel="Chip(s)"]');
        this.sideDoorButton = this.page.locator('[buttonlabel="Side door"]');
        this.driverSideButton = this.page.locator('[buttonlabel="Driver side"]');
        this.passengerSideButton = this.page.locator('[buttonlabel="Passenger side"]');
        this.driverQuarterPanelChkBox = this.page.locator('[aria-labelledby="driverSideOptions"]').locator('[buttonlabel="Quarter panel"]');
        this.driverFrontDoorChkBox = this.page.locator('[aria-labelledby="driverSideOptions"]').locator('[buttonlabel="Front door"]');
        this.driverBackDoorChkBox = this.page.locator('[aria-labelledby="driverSideOptions"]').locator('[buttonlabel="Back door"]');
        this.driverVentGlassChkBox = this.page.locator('[aria-labelledby="driverSideOptions"]').locator('[buttonlabel="Vent glass"]');
        this.driverSlidingDoorChkBox = this.page.locator('[aria-labelledby="driverSideOptions"]').locator('[buttonlabel="Sliding door"]');
        this.passengerQuarterPanelChkBox = this.page.locator('[aria-labelledby="passengerSideOptions"]').locator('[buttonlabel="Quarter panel"]');
        this.passengerFrontDoorChkBox = this.page.locator('[aria-labelledby="passengerSideOptions"]').locator('[buttonlabel="Front door"]');
        this.passengerVentGlassChkBox = this.page.locator('[aria-labelledby="passengerSideOptions"]').locator('[buttonlabel="Vent glass"]');
        this.passengerBackDoorChkBox = this.page.locator('[aria-labelledby="passengerSideOptions"]').locator('[buttonlabel="Back door"]');
        this.rearWindowChkBox = this.page.locator('[buttonlabel="Rear window"]');
        this.noReplacementAvailableAlert = this.page.locator('.alert-danger.widget-name-NoReplacementAvailableError');
        this.bothReplaceRepairAlert = this.page.locator('div.alert-danger.widget-name-HasReplacementConflict');
        this.rearStationaryBttn = this.page.locator('label').filter({ hasText: 'Stationary' });
        this.rearSlidingGlassBttn = this.page.locator('label').filter({ hasText: 'Glass with slider' });
        this.editVehicleLink = this.page.getByRole('link', { name: 'Edit vehicle' });
    }

    async selectDamage(vehicleDamage: VehicleDamage[]) {
        for (const damage of vehicleDamage) {
            switch(damage) {
                case VehicleDamage.WindshieldOneChip:
                    await this.windshieldChkBox.check();
                    await this.selectChips('1');
                    break;
                case VehicleDamage.WindshieldTwoChips:
                    await this.windshieldChkBox.check();
                    await this.selectChips('2');
                    break;
                case VehicleDamage.WindshieldThreeChips:
                    await this.windshieldChkBox.check();
                    await this.selectChips('3');
                    break;
                case VehicleDamage.WindshieldCrack:
                    await this.windshieldChkBox.check();
                    await this.selectCrack();
                    break;
                case VehicleDamage.DriverFrontDoor:
                    await this.sideDoorButton.check();
                    await this.driverSideButton.check();
                    await this.driverFrontDoorChkBox.check();
                    break;
                case VehicleDamage.DriverRearDoor:
                    await this.sideDoorButton.check();
                    await this.driverSideButton.check();
                    await this.driverBackDoorChkBox.check();
                    break;
                case VehicleDamage.DriverQuarterPanel:
                    await this.sideDoorButton.check();
                    await this.driverSideButton.check();
                    await this.driverQuarterPanelChkBox.check();
                    break;
                case VehicleDamage.DriverVentGlass:
                    await this.sideDoorButton.check();
                    await this.driverSideButton.check();
                    await this.driverVentGlassChkBox.check();
                    break;                    
                case VehicleDamage.DriverSlidingDoor:
                    await this.sideDoorButton.check();
                    await this.driverSideButton.check();
                    await this.driverSlidingDoorChkBox.check();
                    break;    
                case VehicleDamage.PassengerFrontDoor:
                    await this.sideDoorButton.check();
                    await this.passengerSideButton.check();
                    await this.passengerFrontDoorChkBox.check();
                    break;
                case VehicleDamage.PassengerRearDoor:
                    await this.sideDoorButton.check();
                    await this.passengerSideButton.check();
                    await this.passengerBackDoorChkBox.check();
                    break;
                case VehicleDamage.PassengerQuarterPanel:
                    await this.sideDoorButton.check();
                    await this.passengerSideButton.check();
                    await this.passengerQuarterPanelChkBox.click();
                    break;
                case VehicleDamage.PassengerVentGlass:
                    await this.sideDoorButton.check();
                    await this.passengerSideButton.check();
                    await this.passengerVentGlassChkBox.check();
                    break;                           
                case VehicleDamage.RearWindow:
                    await this.selectRearWindowDamage();
                    if (await this.rearStationaryBttn.isVisible()) {
                        await this.rearStationaryBttn.click();
                    }
                    break;
                case VehicleDamage.RearSliding:
                    await this.selectRearWindowDamage();
                    if (await this.rearSlidingGlassBttn.isVisible()) {
                        await this.rearSlidingGlassBttn.click();
                    }
                    break;
            }
        }
    }

    async selectCrack(){
        await this.crackButton.check();
    }

    async selectChips(numChips: string){
        await this.chipButton.check();
        await this.page.getByText(numChips, {exact: true}).click();
    }

    async selectRearWindowDamage(){
        await this.rearWindowChkBox.check();
    }

    async checkForBothRepairReplaceAlertMessage(): Promise<void> {
        await expect(this.bothReplaceRepairAlert).toBeVisible();
        const alertMessage = await this.bothReplaceRepairAlert.textContent();
        console.log(`Alert encountered: ${alertMessage}`);
        expect(alertMessage).toContain("You'll need to schedule separate appointmentsVehicle service requiring both glass repair and replacement must be scheduled separately, as they're performed by different technicians. Continue scheduling your first service now, and then come back to schedule the second service.")
    }

    async checkForRepairOnlyAlertMessage(): Promise<void> {
        await expect(this.noReplacementAvailableAlert).toBeVisible();
        const alertMessage = await this.noReplacementAvailableAlert.textContent();
        console.log(`Alert encountered: ${alertMessage}`);
        expect(alertMessage).toContain("Service not availableWe're sorry, but we currently offer only repair service for your vehicle type. Need help with next steps? Call us at800-394-0288.")
    }
}