import { test, expect } from '../../fixtures/index';
import { LoginPage } from '../../pom/login.page';
import { ProfilePage } from '../../pom/profile/profile.page';
import { prepareFile, getFileDownloadedPathFromUrl } from '../../utils/file';
import { takeScreenshot, verifyScreenshot } from '../../utils/screenshot';

test.describe('UI change password tests', async () => {
    let loginPage: LoginPage;
    let profilePage: ProfilePage;

    test.beforeEach(async ({ page, conf }) => {
        loginPage = new LoginPage(page);
        profilePage = new ProfilePage(page);

        await loginPage.open();
        await loginPage.login(conf.data.username, conf.data.password);
        await expect(loginPage.baseLoc.dashboardContainer).toBeVisible();
    });

    test('PROFILE_004 - Verify màn change password UI', {
        tag: ['@PROFILE_004', '@profile', '@ui']
    }, async ({ conf }) => {

        // 1. Click sang menu "change password"
        // 2. Click save mà chưa nhập thông tin
        // 3. Nhập password không đủ điều kiện (6 kí tự)
        // 4. Không nhập confirmation password
        // 5. Nhập sai current password

        await test.step('Click sang menu "changePassword"', async () => {
            await profilePage.navigateToMenu("ChangePassword");
            await expect(profilePage.profileLoc.title).toBeVisible();
            await expect(profilePage.profileLoc.title).toContainText('Change Password');
        });

        await test.step('Click save mà chưa nhập thông tin', async () => {
            await profilePage.waitForSecond(1);
            await profilePage.dashboardLoc.buttonByText("Update").click();
            // await profilePage.waitAllRequestCompeleted();
            await verifyScreenshot('error-missing-info.png', profilePage.profileLoc.tabContentChangePass);
        });

        await test.step('Nhập password không đủ điều kiện (6 kí tự)', async () => {
            await profilePage.waitForSecond(2);
            await profilePage.profileLoc.currentPasswordInput.fill(conf.data.password);
            await profilePage.profileLoc.newPasswordInput.fill(conf.data.invalid_password);
            await profilePage.profileLoc.confirmPasswordInput.fill(conf.data.invalid_password);
            await profilePage.dashboardLoc.buttonByText("Update").click();
            await profilePage.waitForSecond(1);

            await verifyScreenshot('error-password.png', profilePage.profileLoc.tabContentChangePass);
        });

        await test.step('Không nhập confirmation password', async () => {
            await profilePage.waitForSecond(2);
            await profilePage.profileLoc.currentPasswordInput.fill(conf.data.password);
            await profilePage.profileLoc.newPasswordInput.fill(conf.data.valid_password);
            await profilePage.dashboardLoc.buttonByText("Update").click({ force: true });
            await profilePage.waitForSecond(1);

            await verifyScreenshot('error-missing-confirmation-password.png', profilePage.profileLoc.tabContentChangePass);
        });

        await test.step('Nhập sai current password', async () => {
            await profilePage.waitForSecond(2);
            await profilePage.profileLoc.currentPasswordInput.fill(conf.data.invalid_password);
            await profilePage.profileLoc.newPasswordInput.fill(conf.data.valid_password);
            await profilePage.profileLoc.confirmPasswordInput.fill(conf.data.valid_password);
            await profilePage.dashboardLoc.buttonByText("Update").click();
            await profilePage.waitForSecond(1);
            await verifyScreenshot('error-wrong-current-pwd.png', profilePage.profileLoc.mainContainer);

        });
    });
});

test.describe('Function change password tests', async () => {
    let loginPage: LoginPage;
    let profilePage: ProfilePage;

    test.beforeEach(async ({ page, conf }) => {
        loginPage = new LoginPage(page);
        profilePage = new ProfilePage(page);

        await loginPage.open();
        await loginPage.login(conf.data.username, conf.data.password);
        await expect(loginPage.baseLoc.dashboardContainer).toBeVisible();
        await profilePage.navigateToMenu("ChangePassword");
        await expect(profilePage.profileLoc.title).toBeVisible();
        await expect(profilePage.profileLoc.title).toContainText('Change Password');
    });

    // test.beforeEach(async ({ page, conf }) => {
    //     await profilePage.navigateToMenu("ChangePassword");
    //     await expect(profilePage.profileLoc.title).toContainText('Change Password');
    //     await profilePage.waitAllRequestCompeleted();
    // });


    test('PROFILE_005 - Verify màn change password function', {
        tag: ['@PROFILE_005', '@profile', '@function']
    }, async ({ conf }) => {
        test.slow();
        // 1. Điền đầy đủ các thông tin, bấm save
        // 2. Logout ra và đăng nhập lại

        await test.step('Điền đầy đủ các thông tin và kèm space, bấm save', async () => {
            await profilePage.profileLoc.currentPasswordInput.fill(` ${conf.data.password}   `);
            await expect(profilePage.profileLoc.currentPasswordInput).toHaveValue(conf.data.password);
            await profilePage.profileLoc.newPasswordInput.fill(` ${conf.data.new_password}   `);
            await expect(profilePage.profileLoc.newPasswordInput).toHaveValue(conf.data.new_password);
            await profilePage.profileLoc.confirmPasswordInput.fill(` ${conf.data.new_password}   `);
            await expect(profilePage.profileLoc.confirmPasswordInput).toHaveValue(conf.data.new_password);
            await profilePage.dashboardLoc.buttonByText("Update").click();
            await profilePage.waitAllRequestCompeleted();
        });

        await test.step('Logout ra và đăng nhập lại', async () => {
            await profilePage.logout();
            await loginPage.open();
            await loginPage.login(conf.data.username, conf.data.new_password);
            await expect(loginPage.baseLoc.dashboardContainer).toBeVisible();
        });

        await test.step('Reset password', async () => {
            // Reset password to default value
            await profilePage.navigateToMenu("ChangePassword");
            await expect(profilePage.profileLoc.title).toContainText('Change Password');

            await profilePage.profileLoc.currentPasswordInput.fill(conf.data.new_password);
            await profilePage.profileLoc.newPasswordInput.fill(conf.data.password);
            await profilePage.profileLoc.confirmPasswordInput.fill(conf.data.password);
            await profilePage.dashboardLoc.buttonByText("Update").click();
            await profilePage.waitAllRequestCompeleted();
        });
    });
});