import test, { expect, type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';
import { IClaimDetails, ICustomerDetails } from '@business-logic/types/CustomerDetails';
import { getClientAuthByClientTag, getClientSignature } from '@impl/api/AdminServiceApiUtil';
import { ICertificateInfo, IClientSignatureRequest } from '@business-logic/types/Authentication';
import { buildToken, getTimestamp } from '@impl/utils/TokenUtils';

export class WelcomePage extends BasePage {
    readonly page: Page;
    readonly policyNumber: Locator;
    readonly policyZip: Locator;
    readonly damageDate: Locator;
    readonly damageCause: Locator;
    readonly phoneNumber: Locator;
    readonly emailAddress: Locator;
    readonly city: Locator;
    readonly state: Locator;
    readonly cookieCloseButton: Locator;
    url = process.env['BASE_URL']! + '/?issPage=welcome-page';
    issPageValue = 'welcome-page';

    constructor(page: Page) {
        super(page);
        this.page = page;
        this.policyNumber = page.getByRole('textbox', { name: 'Policy number' });
        this.policyZip = page.getByRole('textbox', { name: 'Policy ZIP' });
        this.damageDate = page.getByRole('textbox', { name: 'When did the damage occur?<' });
        this.damageCause = page.locator('#damageCauseQuestionField');
        this.phoneNumber = page.getByRole('textbox', { name: 'Best number to reach you' });
        this.emailAddress = page.getByRole('textbox', { name: 'Email address' });
        this.city = page.getByRole('textbox', { name: 'In which city did the damage' });
        this.state = page.locator('select[name="\\38 fdf9dc2e13e430eb57529499dceb3eb"]');
        this.cookieCloseButton = page.getByRole('button', { name: 'Close' });
    }

    async goto(clientTag: string) {
        await this.page.goto(process.env['BASE_URL']! + `/?issPage=entry-page&ClientTag=${clientTag}`);
        await this.waitForURL(this.url);
        await this.cookieCloseButton.click();
    }

    async gotoWithAuthentication(clientTag: string) {
        const clientAuth = await getClientAuthByClientTag(clientTag);
        const certificate = clientAuth.certificateInfo[0] as ICertificateInfo;
        let formData: Map<string, string> = new Map<string, string>();
        formData.set("Timestamp", getTimestamp())
        const request: IClientSignatureRequest = {
            clientTag: clientTag,
            token: buildToken(clientAuth, formData),
            certificateFileName: certificate.name,
            certificateKey: certificate.key,
            certificateAlgorithm: certificate.algorithm,
            certificateType: certificate.type
        }
        const result = await getClientSignature(request);
        await this.page.goto(process.env['BASE_URL']! + `/?issPage=entry-page&ClientTag=${clientTag}&token=${request.token}&signature=${result.signature}`);
        await this.waitForURL(this.url);
        await this.logReferralNumber();
        await this.cookieCloseButton.click();
    }

    async hasCityInfo() {
        await expect.soft(this.damageCause).toBeVisible();
        return this.city.isVisible();
    }

    async populatePage(customerDetails: ICustomerDetails, claimDetails: IClaimDetails, isFillCityInfo: boolean) {
        await this.policyNumber.fill(claimDetails.policyNumber);
        await this.policyZip.fill(customerDetails.address.postalCode);
        await this.damageDate.click();
        await this.damageDate.fill(claimDetails.damageDate);
        await this.damageCause.selectOption(claimDetails.damageCause);
        await this.damageCause.press('Tab');
        await this.phoneNumber.fill(customerDetails.phoneNumber);
        // Phone number fixed 12/5. Can't start with 1 or 0
        await this.emailAddress.fill(customerDetails.email);

        if (isFillCityInfo) {
            await this.city.fill(customerDetails.address.city);
            await this.state.selectOption(customerDetails.address.state);
        }
    }
}