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.profileLoc.saveButton.click();
            await profilePage.waitAllRequestCompeleted();
            await verifyScreenshot('error-missing-info.png', profilePage.profileLoc.mainContainer);
        });

        await test.step('Nhập password không đủ điều kiện (6 kí tự)', async () => {
            await profilePage.reload();
            await profilePage.waitAllRequestCompeleted();
            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.profileLoc.saveButton.click();
            await profilePage.waitAllRequestCompeleted();

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

        await test.step('Không nhập confirmation password', async () => {
            await profilePage.reload();
            await profilePage.profileLoc.currentPasswordInput.fill(conf.data.password);
            await profilePage.profileLoc.newPasswordInput.fill(conf.data.valid_password);
            await profilePage.profileLoc.saveButton.click();
            await profilePage.waitAllRequestCompeleted();

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

        await test.step('Nhập sai current password', async () => {
            await profilePage.reload();
            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.profileLoc.saveButton.click();
            await profilePage.waitAllRequestCompeleted();

            await verifyScreenshot('error-wrong-current-pwd.png', profilePage.profileLoc.mainContainer);
            await profilePage.waitForSecond(2);
        });
    });
});

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.profileLoc.saveButton.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.profileLoc.saveButton.click();
            await profilePage.waitAllRequestCompeleted();
        });
    });
});