/*******************************
NE PAS MODIFIER, FICHIER GENERE
*******************************/

/**
 * Software Name : UUV
 *
 * SPDX-License-Identifier: MIT
 *
 * This software is distributed under the MIT License,
 * see the "LICENSE" file for more details
 *
 * Authors: NJAKO MOLOM Louis Fredice & SERVICAL Stanley
 * Software description: Make test writing fast, understandable by any human
 * understanding English or French.
 */


import {
  click,
  COOKIE_NAME,
  deleteCookieByName,
  findWithRoleAndName,
  findWithRoleAndNameAndChecked,
  findWithRoleAndNameAndContent,
  findWithRoleAndNameAndContentDisable,
  findWithRoleAndNameAndContentEnable,
  findWithRoleAndNameAndUnchecked,
  findWithRoleAndNameFocused,
  getPageOrElement,
  getTimeout,
  notFoundWithRoleAndName,
  withinRoleAndName
} from "../../../core-engine";
import { Then, When } from "../../../../../preprocessor/run/world";
import { expect } from "@playwright/test";

// Begin of General Section

/**
 * Selects the element whose [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types) and accessible [name](https://russmaxdesign.github.io/html-elements-names/) are specified <br />⚠ remember to deselect the element with <b>[I reset context](#i-reset-context)</b> if you're no longer acting on it
 * */
When(`within a spin button named {string}`, async function(name: string) {
  return await withinRoleAndName(this, "spinbutton", name);
});

/**
 * Checks that an Html element exists with the specified [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types) and [name](https://russmaxdesign.github.io/html-elements-names/)
 * */
Then(`I should see a spin button named {string}`, async function(name: string) {
  await findWithRoleAndName(this, "spinbutton", name);
});

/**
 * Checks that an Html element does not exists with the specified [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types) and [name](https://russmaxdesign.github.io/html-elements-names/)
 * */
Then(
 `I should not see a spin button named {string}`,
 async function(name: string) {
   await notFoundWithRoleAndName(this, "spinbutton", name);
 }
);

// End of General Section

// Begin of Type Section

/**
 * Writes the sentence given as a parameter inside the specified field (useful for example to fill in a form field)
 * */
When(`I type the sentence {string} in the spin button named {string}`, async function(textToType: string, name: string) {
  await getPageOrElement(this).then(async (element) => {
    const byRole = await element.getByRole("spinbutton", { name: name, exact: true });
    await expect(byRole).toHaveCount(1, { timeout: await getTimeout(this) });
    await byRole.type(textToType);
    await deleteCookieByName(this, COOKIE_NAME.SELECTED_ELEMENT);
  });
});

/**
 * Writes the sentence given as a parameter inside the specified field (useful for example to fill in a form field)
 * */
When(`I enter the value {string} in the spin button named {string}`, async function(textToType: string, name: string) {
    await getPageOrElement(this).then(async (element) => {
        const byRole = await element.getByRole("spinbutton", { name: name, exact: true });
        await expect(byRole).toHaveCount(1);
        await byRole.type(textToType);
        await deleteCookieByName(this, COOKIE_NAME.SELECTED_ELEMENT);
    });
});

// End of Type Section
// Begin of Content Section

/**
 * Checks that an Html element exists with the specified [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types), [name](https://russmaxdesign.github.io/html-elements-names/) and content
 * */
Then(
 `I should see a spin button named {string} and containing {string}`,
 async function(name: string, expectedTextContent: string) {
   await findWithRoleAndNameAndContent(this, "spinbutton", name, expectedTextContent);
 }
);

/**
 * Checks that an Html element exists with the specified [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types), [name](https://russmaxdesign.github.io/html-elements-names/) and content, and with the disabled attribute set to true
 * */
Then(
 `I should see a spin button named {string} and containing {string} disabled`,
 async function(name: string, expectedTextContent: string) {
   await findWithRoleAndNameAndContentDisable(this, "spinbutton", name, expectedTextContent);
 }
);

/**
 * Checks that an Html element exists with the specified [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types), [name](https://russmaxdesign.github.io/html-elements-names/) and content, and with the disabled attribute set to false
 * */
Then(
 `I should see a spin button named {string} and containing {string} enabled`,
 async function(name: string, expectedTextContent: string) {
   await findWithRoleAndNameAndContentEnable(this, "spinbutton", name, expectedTextContent);
 }
);

// End of Content Section

// Begin of Keyboard Section

/**
 * Checks that the Html element with the specified [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types) and [name](https://russmaxdesign.github.io/html-elements-names/) is focused<br/><a target='_blank' href='https://github.com/e2e-test-quest/uuv/blob/main/example/en-keyboard.feature'>Examples</a>
 * */
Then(
 `I should see a spin button named {string} keyboard focused`,
 async function(name: string) {
   await findWithRoleAndNameFocused(this, "spinbutton", name);
 }
);

/**
 * Move to the previous html element that can be reached with Tab and checks that the Html element with the specified [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types) and [name](https://russmaxdesign.github.io/html-elements-names/) is focused<br/><a target='_blank' href='https://github.com/e2e-test-quest/uuv/blob/main/example/en-keyboard.feature'>Examples</a>
 * */
Then(
 `the previous keyboard element focused should be a spin button named {string}`,
 async function(name: string) {
   await this.page.keyboard.press("ShiftLeft+Tab");
   await findWithRoleAndNameFocused(this, "spinbutton", name);
 }
);

/**
 * Move to the next html element that can be reached with Tab and checks that the Html element with the specified [accessible role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles#aria_role_types) and [name](https://russmaxdesign.github.io/html-elements-names/) is focused<br/><a target='_blank' href='https://github.com/e2e-test-quest/uuv/blob/main/example/en-keyboard.feature'>Examples</a>
 * */
Then(
 `the next keyboard element focused should be a spin button named {string}`,
 async function(name: string) {
   await this.page.keyboard.press("Tab");
   await findWithRoleAndNameFocused(this, "spinbutton", name);
 }
);
// End of Keyboard Section


