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

export class VehicleDamagePage extends BasePage {
    readonly page: Page;
    readonly windshieldChkBox: Locator;
    readonly crackButton: Locator;
    readonly chipButton: Locator;
    readonly singleWindshieldButton: Locator;
    readonly splitWindshieldDriverSideButton: Locator;
    readonly splitWindshieldPassengerSideButton: 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 separateApptsWarning: Locator;
    readonly editVehicleButton: Locator;
    issPageValue = '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.singleWindshieldButton = this.page.locator('[buttonlabel="Single windshield"]');
        this.splitWindshieldDriverSideButton = this.page.locator('[buttonlabel="Split windshield, driver side"]');
        this.splitWindshieldPassengerSideButton = this.page.locator('[buttonlabel="Split windshield, passenger side"]');
        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.separateApptsWarning = this.page.locator('[class*="widget-name-HasReplacementConflict"]');
        this.editVehicleButton = this.page.getByRole('link', { name: 'Edit vehicle' });
    }

    async checkSeparateApptsWarning() {
        // Select conflict
        await this.windshieldChkBox.check();
        await this.chipButton.check();
        await this.rearWindowChkBox.check();

        // Expect warning
        await expect.soft(this.separateApptsWarning).toBeAttached();

        // Undo changes
        await this.crackButton.check();
        await this.windshieldChkBox.uncheck();
        await expect.soft(this.crackButton).not.toBeVisible();
        await this.rearWindowChkBox.uncheck();
    }

    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.WindshieldCrackSingleWindshield:
                    await this.windshieldChkBox.check();
                    await this.selectCrack();
                    await this.singleWindshieldButton.check();
                    break;
                case VehicleDamage.WindshieldCrackDriverSide:
                    await this.windshieldChkBox.check();
                    await this.selectCrack();
                    await this.splitWindshieldDriverSideButton.check();
                    break;
                case VehicleDamage.WindshieldCrackPassengerSide:
                    await this.windshieldChkBox.check();
                    await this.selectCrack();
                    await this.splitWindshieldPassengerSideButton.check();
                    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();
                    break;
            }
        }
    }

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

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

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