import { Locator, Page, expect } from "@playwright/test";
import { CommonPage } from "../../utils/tinymce";

export class SellerManageProfile {

    readonly page: Page;

    readonly saveButton: Locator;
    readonly successToast: Locator;

    readonly updateSuccessToast: Locator;

    readonly editButton: Locator;

    readonly sellerMetaTitleInput: Locator;
    readonly sellerMetaDescriptionInput: Locator;
    readonly sellerMetaKeywordsInput: Locator;
    readonly sellerSaveButton: Locator;
    readonly sellerDescriptionIframe: string;
    readonly sellerPhoneNumber: Locator;

    readonly address: Locator;
    readonly addressCity: Locator;
    readonly addressCountry: Locator;
    readonly addressState: Locator;
    readonly addressZip: Locator;
    readonly commissionToggle: Locator;
    readonly productType: Locator;
    readonly businessName: Locator;

    constructor(page: Page) {
        this.page = page;

        this.saveButton = page.getByRole("button", { name: "Save" });
        this.successToast = page
            .locator("text=Seller Created Successfully.")
            .first();

        this.updateSuccessToast = page
            .locator("text=Your Profile is updated successfully")
            .first();

        this.editButton = page.locator('span[title="Edit"]').nth(0);
        // this.sellerNameInput = page.locator('input[name="name"]');
        this.sellerDescriptionIframe = "#business_description_ifr";
        this.sellerMetaTitleInput = page.locator('input[name="meta_title"]');
        this.sellerMetaDescriptionInput = page.locator(
            'textarea[name="meta_description"]'
        );
        this.sellerMetaKeywordsInput = page.locator(
            'input[name="meta_keywords"]'
        );
        this.sellerSaveButton = page
            .getByRole("button", { name: "Save" })
            .nth(0);
        this.sellerPhoneNumber = page.locator('input[name="phone"]');

        this.address = page.locator('textarea[name="address"]');
        this.addressCity = page.locator('input[name="city"]');
        this.addressCountry = page.locator('select[name="country"]');
        this.addressState = page.locator('select[name="state"]');
        this.addressZip = page.locator('input[name="postcode"]');
        this.commissionToggle = page.locator(".relative > label");
        this.productType = page.locator(
            'select[name="allowed_product_types[]"]'
        );
        this.businessName = page.locator('input[name="business_name"]');
    }

    async gotoProfile() {
        await this.page.goto("seller/profile");
    }

    async updateSellerProfile(
        description: string,
        address: string,
        businessname: string,
        city: string,
        country: string,
        state: string,
        zip: string,
        number: Number
    ) {
        const commonPage = new CommonPage(this.page);

        await commonPage.fillInTinymce(
            this.sellerDescriptionIframe,
            description
        );
        await this.sellerPhoneNumber.fill(number.toString());
        await this.address.fill(address);
        await this.addressCity.fill(city);
        await this.addressCountry.selectOption(country);
        await this.addressState.selectOption(state);
        await this.addressZip.fill(zip);
        await this.sellerMetaTitleInput.fill(businessname);
        await this.sellerSaveButton.click();
        await expect(this.updateSuccessToast).toBeVisible();
    }
}
