import { ClientFunction } from 'testcafe';
import homepage from '../pages/HomePage';
import searchresults from '../pages/SearchResultPage'
import productdetails from '../pages/ProductDetailsPage'
import cartpage from '../pages/CartPage'
import checkoutpage from '../pages/CheckoutPage'
import loginpage from "../pages/LoginPage";

const URL = 'https://demo.nopcommerce.com/';
const getURL = ClientFunction(() => window.location.href);
var randomNumber = Math.floor(Math.random() * 10000);
var userEmail = 'hasan' + randomNumber + '@email.com';

fixture`A guest user visits the web application, search for an available product and place an order`
    .page(URL).beforeEach(async t => {
        await t
            .setTestSpeed(1)
            .setPageLoadTimeout(1);
    });

    test.meta({
        ID: '1',
        SEVERITY: 'Medium',
        STORY: 'INS-452',
        TEST_RUN: 'test run id or identifier'
    })("Assert Home page", async (t) => {
        await t
            .expect(getURL()).eql(URL)
            .takeScreenshot()
            .expect(homepage.subtitleHeader.exists).ok()
    });
    
    test.meta({
        ID: '2',
        SEVERITY: 'Low',
        STORY: 'INS-452',
        TEST_RUN: 'test run id or identifier'
    })("Change Currency Test", async (t) => {
        await homepage.changeCurrency('Euro')
    });


test.meta({
    ID: '3',
    SEVERITY: 'blocker',
    STORY: 'INS-452',
    TEST_RUN: 'test run id or identifier'
})("As a guest, the purchase of a product is successful.", async (t) => {
    await homepage.search(t.fixtureCtx.searchKeyWord);
    await t
        .click(searchresults.productTitle)
        .expect(getURL()).contains(t.fixtureCtx.searchKeyWordInURL)
        .expect(productdetails.productPrice.exists).ok()
        .click(productdetails.addToCart)
        .expect(productdetails.successMessage.exists).ok()
        .wait(3000)
        .click(homepage.CartLink)
        .click(cartpage.termsLabel)
        .click(cartpage.checkoutBtn)
        .expect(getURL()).contains(t.fixtureCtx.checkoutGuestURL)
        .click(loginpage.checkoutAsGuestUser)
        .typeText(checkoutpage.firstName, t.fixtureCtx.firstName)
        .typeText(checkoutpage.lastName, t.fixtureCtx.lastName)
        .typeText(checkoutpage.emailAddress, userEmail);
    await checkoutpage.selectCountry( t.fixtureCtx.country);
    await t
        .typeText(checkoutpage.cityTxt, t.fixtureCtx.cityTxt)
        .typeText(checkoutpage.addressTxt, t.fixtureCtx.addressTxt)
        .typeText(checkoutpage.zipTxt, t.fixtureCtx.zipTxt)
        .typeText(checkoutpage.phoneTxt, t.fixtureCtx.phoneTxt)
        .click(checkoutpage.continueBtn)
        .click(checkoutpage.nextDayOption)
        .click(checkoutpage.nextShippingBtn)
        .click(checkoutpage.nextPaymentBtn)
        .click(checkoutpage.nextConfirmBtn)
        .click(checkoutpage.confirmOrderBtn);
       // .expect(checkoutpage.orderConfirmationMessage.exists).Ok()
        //.click(checkoutpage.viewOrderDetailsLink);
});

