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

const row = {
    original: {
        avatar: null,
        created_at: "2023-10-10T13:05:01.000000Z",
        email: "fisher.sigrid@yahoo.com",
        full_name: "Anibal Labadie",
        gender: "female",
        id: 85,
        national_id: "5944646138",
        phone_number: "09731311720",
        position: "boss1",
        province_fa: "اردبیل",
        province_id: 3,
        roles: [{
            created_at: "2023-10-10T13:03:18.000000Z",
            guard_name: "api",
            id: 2,
            name: "manager",
            name_fa: "مدیر"
        }],
        telephone_id: "tel-3110",
        updated_at: "2023-10-17T10:57:12.000000Z",
        username: "florence.kihn",
    }
}

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("Update Expert Button Rendered", () => {
            render(<MockAppWithProviders><UpdateForm row={row}/></MockAppWithProviders>);
            const UpdateButton = screen.getByTestId('update-button');
            expect(UpdateButton).toBeInTheDocument();
        });
    });
    describe("Behavioral", () => {
        it("by Clicking Update Button Dialog Should Append To Document", async () => {
            render(<MockAppWithProviders><UpdateForm row={row}/></MockAppWithProviders>);
            const UpdateButton = screen.getByTestId('update-button');
            fireEvent.click(UpdateButton);
            await act(() => {
                const UpdateDialog = screen.getByRole('dialog', {name: "ویرایش کارشناس"})
                expect(UpdateDialog).toBeInTheDocument();
            });
        });
    });
    describe("Form Submission", () => {
        it('Should request to api and if get success close dialog', async () => {
            render(<MockAppWithProviders><UpdateForm row={row}/></MockAppWithProviders>);

            const UpdateButton = screen.getByTestId('update-button');
            fireEvent.click(UpdateButton);

            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 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, 'email@gmail.com');
            setInputValue(screen, phoneNumberInput, '0914577458');
            setInputValue(screen, telephoneIdInput, '091');
            setInputValue(screen, nationalIdInput, 'nationalIdTest');
            setInputValue(screen, positionInput, 'positionTest');

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

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

            const UpdateButton = screen.getByTestId('update-button');
            fireEvent.click(UpdateButton);

            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 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, 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(positionInput).toHaveValue('positionTest');
                expect(provinceInput).toHaveValue('1');
                expect(roleInput).toHaveValue('1');
                expect(genderInput).toHaveValue('male');
            });
        });
    })
});