import {act, fireEvent, render, screen, waitFor} from "@testing-library/react";
import React from 'react';
import MockAppWithProviders from "../../../../../../../mocks/AppWithProvider";
import CreateForm from "../../CreateForm";

function selectDropdownItem(screen, openerTestId, itemText) {
    const opener = screen.getByTestId(openerTestId);
    fireEvent.mouseDown(opener);
    const selectItem = screen.queryByText(itemText);
    fireEvent.click(selectItem);
}

function setInputValue(screen, inputElement, value) {
    fireEvent.change(inputElement, {target: {value}});
}

describe("CreateForm Component From Expert Management", () => {
    describe("Rendering", () => {
        it("Create Button Rendered", () => {
            render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>);
            const CreateButton = screen.getByRole('button', {name: "افزودن"});
            expect(CreateButton).toBeInTheDocument();
        });
        it("Dialog Header Rendered", () => {
            render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>);
            const CreateDialogHeader = screen.queryByText("افزودن");
            expect(CreateDialogHeader).toBeInTheDocument();
        });
    });
    describe("Behavioral", () => {
        it("by Clicking Create Button Dialog Should Append To Document", async () => {
            render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>);
            const CreateButton = screen.getByRole('button', {name: "افزودن"});
            fireEvent.click(CreateButton);
            await act(() => {
                const CreateDialog = screen.getByRole('dialog', {name: "افزودن"})
                expect(CreateDialog).toBeInTheDocument();
            });
        });
    });
    describe("Form Submission", () => {
        it('Should request to api and if get success close dialog', async () => {
            render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>);

            const CreateButton = screen.getByRole('button', {name: "افزودن"});
            fireEvent.click(CreateButton);

            const submitButton = screen.queryByText('ثبت');

            const nameInput = screen.queryByLabelText('نام کامل');
            const usernameInput = screen.queryByLabelText('نام کاربری');
            const emailInput = screen.queryByLabelText('پست الکترونیک');
            const phoneNumberInput = screen.queryByLabelText('شماره همراه');
            const telephoneIdInput = screen.queryByLabelText('کد تلفن');
            const nationalIdInput = screen.queryByLabelText('کد ملی');
            const passwordInput = screen.queryByLabelText('رمز عبور');
            const positionInput = screen.queryByLabelText('سمت');

            selectDropdownItem(screen, "option-opener-province", "آذربایجان شرقی");
            selectDropdownItem(screen, "option-opener-role", "ادمین");
            selectDropdownItem(screen, "option-opener-gender", "مرد");

            setInputValue(screen, nameInput, 'nameTest');
            setInputValue(screen, usernameInput, 'usernameTest');
            setInputValue(screen, emailInput, 'emailTest@gmail.com');
            setInputValue(screen, phoneNumberInput, '0914577458');
            setInputValue(screen, telephoneIdInput, '091');
            setInputValue(screen, nationalIdInput, 'nationalIdTest');
            setInputValue(screen, passwordInput, 'passwordTest12');
            setInputValue(screen, positionInput, 'positionTest');

            expect(submitButton).not.toBeDisabled();
            fireEvent.click(submitButton);

            await waitFor(() => {
                const dialogContent = screen.queryByTestId('create-dialog-content');
                expect(dialogContent).not.toBeInTheDocument();
            });
        });
        it('Should request to api and if get error keep previous data', async () => {
            render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>);

            const CreateButton = screen.getByRole('button', {name: "افزودن"});
            fireEvent.click(CreateButton);

            const submitButton = screen.queryByText('ثبت');

            const nameInput = screen.queryByLabelText('نام کامل');
            const usernameInput = screen.queryByLabelText('نام کاربری');
            const emailInput = screen.queryByLabelText('پست الکترونیک');
            const phoneNumberInput = screen.queryByLabelText('شماره همراه');
            const telephoneIdInput = screen.queryByLabelText('کد تلفن');
            const nationalIdInput = screen.queryByLabelText('کد ملی');
            const passwordInput = screen.queryByLabelText('رمز عبور');
            const positionInput = screen.queryByLabelText('سمت');
            const provinceInput = screen.getByTestId("input-province-id")
            const roleInput = screen.getByTestId("input-role-id")
            const genderInput = screen.getByTestId("input-gender")

            selectDropdownItem(screen, "option-opener-province", "آذربایجان شرقی");
            selectDropdownItem(screen, "option-opener-role", "ادمین");
            selectDropdownItem(screen, "option-opener-gender", "مرد");

            setInputValue(screen, nameInput, 'nameTest');
            setInputValue(screen, usernameInput, 'usernameTest');
            setInputValue(screen, emailInput, 'emailTest@gmail.com');
            setInputValue(screen, phoneNumberInput, '0914577458');
            setInputValue(screen, telephoneIdInput, '091');
            setInputValue(screen, nationalIdInput, 'nationalIdTest');
            setInputValue(screen, passwordInput, 'passwordTest12');
            setInputValue(screen, positionInput, 'positionTest');

            expect(submitButton).not.toBeDisabled();
            await fireEvent.click(submitButton);

            await waitFor(() => {
                expect(nameInput).toHaveValue('nameTest');
                expect(usernameInput).toHaveValue('usernameTest');
                expect(emailInput).toHaveValue('emailTest@gmail.com');
                expect(phoneNumberInput).toHaveValue('0914577458');
                expect(telephoneIdInput).toHaveValue('091');
                expect(nationalIdInput).toHaveValue('nationalIdTest');
                expect(passwordInput).toHaveValue('passwordTest12');
                expect(positionInput).toHaveValue('positionTest');
                expect(provinceInput).toHaveValue('1');
                expect(roleInput).toHaveValue('1');
                expect(genderInput).toHaveValue('male');
            });
        });
    })
});