import { Locator, Page, expect } from "@playwright/test";
import { WebAllLocators } from "../locator";

export class SellerProductCheckout {
    readonly page: Page;
    readonly locators: WebAllLocators;

    constructor(page: Page) {
        this.page = page;
        this.locators = new WebAllLocators(page);
    }

    async adminMarketplaceProductSectionGoto() {
        await this.page.goto("admin/marketplace/products");
    }

    async sellerProductCheckoutProcess(
        companyname: string,
        firstname: string,
        lastname: string,
        email: string,
        streetaddress: string,
        country: string,
        state: string,
        city: string,
        zip: number,
        phonenumber: number
    ) {
        await this.locators.addToCartButton.click();
        await this.locators.ShoppingCartIcon.click();
        await this.locators.ContinueButton.click();
        await this.locators.companyName.fill(companyname);
        await this.locators.firstName.fill(firstname);
        await this.locators.lastName.fill(lastname);
        await this.locators.shippingEmail.fill(email);
        await this.locators.streetAddress.fill(streetaddress);
        await this.locators.billingCountry.selectOption(country);
        await this.locators.billingState.selectOption(state);
        await this.locators.billingCity.fill(city);
        await this.locators.billingZip.fill(zip.toString());
        await this.locators.billingTelephone.fill(phonenumber.toString());
        await this.page.waitForTimeout(2000);
        await this.locators.clickProcessButton.click();
        await this.page.waitForSelector("text=Free Shipping");
        await this.locators.chooseShippingMethod.click();
        await this.page.waitForTimeout(2000);
        await this.page.waitForSelector("text=Cash On Delivery");
        await this.locators.choosePaymentMethod.click();
        await this.page.waitForTimeout(2000);
        await this.locators.clickPlaceOrderButton.click();
        await this.page.waitForTimeout(2000);
        await this.page.waitForSelector("text=Thank you for your order!");
        await expect(this.locators.CheckoutsuccessPage).toBeVisible();
    }

    async sellerProductCheckoutProcessForCustomer(
        companyname: string,
        firstname: string,
        lastname: string,
        email: string,
        streetaddress: string,
        country: string,
        state: string,
        city: string,
        zip: number,
        phonenumber: number
    ) {
        await this.locators.addToCartButton.click();
        await this.locators.ShoppingCartIcon.click();
        await this.locators.ContinueButton.click();
        await this.locators.companyName.fill(companyname);
        await this.locators.firstName.fill(firstname);
        await this.locators.lastName.fill(lastname);
        await this.locators.shippingEmail.fill(email);
        await this.locators.streetAddress.fill(streetaddress);
        await this.locators.billingCountry.selectOption(country);
        await this.locators.billingState.selectOption(state);
        await this.locators.billingCity.fill(city);
        await this.locators.billingZip.fill(zip.toString());
        await this.locators.billingTelephone.fill(phonenumber.toString());
        await this.locators.clickSaveAddressButton.click();
        await this.page.waitForTimeout(2000);
        await this.locators.clickProcessButton.click();
        await this.page.waitForSelector("text=Free Shipping");
        await this.locators.chooseShippingMethod.click();
        await this.page.waitForTimeout(2000);
        await this.page.waitForSelector("text=Cash On Delivery");
        await this.locators.choosePaymentMethod.click();
        await this.page.waitForTimeout(2000);
        await this.locators.clickPlaceOrderButton.click();
        await this.page.waitForTimeout(2000);
        await this.page.waitForSelector("text=Thank you for your order!");
        await expect(this.locators.CheckoutsuccessPage).toBeVisible();
    }
}
