import { ClientFunction } from "testcafe";
import homepage from "../pages/HomePage";
import customerpage from "../pages/CustomerPage";
import loginpage from "../pages/LoginPage";
import registerpage from "../pages/RegisterPage";
import browsepage from "../pages/BrowsePage";
import wishlistpage from "../pages/WishListPage";
import cartpage from '../pages/CartPage'
import checkoutpage from '../pages/CheckoutPage'
import { getKeywords }  from "../dataBase/MongoDbConnection";
import myorderpage from '../pages/MyOrdersPage'

const userData = require("../data/data.json")

const URL ="https://demo.nopcommerce.com/";
const pageURL = ClientFunction(()=>window.location.href);
const getURL = ClientFunction(() => window.location.href);

fixture("Registration Page").page(URL).beforeEach(async t =>{
        await t
            .setTestSpeed(1)
            .setPageLoadTimeout(0);
    });;

test.meta({
    ID: '1',
    SEVERITY: 'low',
    STORY: 'NSC-1431',
    TEST_RUN: '1'
}).page("https://demo.nopcommerce.com/")
("Assert Home page", async (t) => {
     const keyword = await getKeywords();
    await t 
    .expect(pageURL()).eql(URL)
    .takeScreenshot()
    .expect(homepage.subtitleHeader.exists).ok()
});

test.meta({
    ID: '2',
    SEVERITY: 'medium',
    STORY: 'NSC-1431',
    TEST_RUN: '1'
}).page("https://demo.nopcommerce.com/")
("Validate register user data in database", async (t) => {
     const keyword = await getKeywords();
    await t
      .expect(keyword[0].firstName).eql(userData.firstName)
      .expect(keyword[0].lastName).eql(userData.lastName)
      .expect(keyword[0].country).eql(userData.country)
      .expect(keyword[0].cityTxt).eql(userData.cityTxt)
      .expect(keyword[0].addressTxt).eql(userData.addressTxt)
      .expect(keyword[0].phoneTxt).eql(userData.phoneTxt)
      .expect(keyword[0].date).eql(userData.date)
      .expect(keyword[0].month).eql(userData.month)
      .expect(keyword[0].year).eql(userData.year)
});

test.meta({
    ID: '3',
    SEVERITY: 'blocker',
    STORY: 'NSC-1431',
    TEST_RUN: '1'
}).page("https://demo.nopcommerce.com/")
("User Register and Login  page", async (t) => {
    var randomNumber = Math.floor(Math.random() * 10000);
    var userEmail = 'hasaneqbal'+randomNumber+'@test.com';
    await t 
    .click(homepage.RegisterLink)
    .expect(pageURL()).contains("register")
    .click(registerpage.GenderOption)
    .typeText(registerpage.FirstName,userData.firstName)
    .typeText(registerpage.LastName,userData.lastName);
    await registerpage.selectDay(userData.date);
    await registerpage.selectMonth(userData.month);
    await registerpage.selectYear(userData.year);
    await t
     .typeText(registerpage.Email,userEmail)
     .typeText(registerpage.Password,userData.password)
     .typeText(registerpage.ConfirmPassword,userData.password)
     .click(registerpage.RegisterButton)
     .expect(registerpage.SuccessfullMessage.exists).ok()
     .click(homepage.LogoutLink) 
     .click(homepage.LoginLink)
     .expect(loginpage.accountHeader.exists).ok()
     .typeText(loginpage.emailInput,userEmail)
     .typeText(loginpage.passwordInput,userData.password)
     .click(loginpage.submitButton)
     .click(homepage.MyAccountLink)
     .expect(customerpage.ordersLink.exists).ok()
     .click(customerpage.ordersLink)
     .expect(customerpage.noOrdersLabel.exists).ok()
     .click(homepage.Books)
     .click(browsepage.addToWishList)
     .wait(3000)
     .click(wishlistpage.WishListSuccessMessageClose)
     .click(homepage.WishListLink)
     .click(wishlistpage.AddToCartCheckBox)
     .click(wishlistpage.AddToCartButton)
     .click(cartpage.termsLabel)
     .click(cartpage.checkoutBtn)
     .expect(getURL()).contains(userData.checkoutURL);
     await checkoutpage.selectCountry(userData.country);
     await t
         .takeScreenshot()
         .typeText(checkoutpage.cityTxt,userData.cityTxt)
         .typeText(checkoutpage.addressTxt,userData.addressTxt)
         .typeText(checkoutpage.zipTxt,userData.zipTxt)
         .typeText(checkoutpage.phoneTxt,userData.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)
         .click(homepage.MyAccountLink)
         .click(customerpage.ordersLink)  
     
    
});


