import * as React from "react";
import { render, screen, fireEvent, cleanup } from "@testing-library/react";
import reactToCSS from "react-style-object-to-css";

import { styleClean } from "@sc/utils";

import Checkboxes from "./Checkboxes";
import { data } from "./Checkboxes.stories";
import { styleData } from "../../FormBuilder.stories";

import { EditorMode } from "../../../../Editor/types";

const testId = "FormBuilder-FormFields-Checkboxes";
const descriptionTestId = "FormBuilder-Attributes-Description";
const requiredTestId = "FormBuilder-Attributes-Required";
const labelTestId = "FormBuilder-Attributes-Label";

const props = {
  data,
  styles: styleData,
  attributeContainerDataSettings: { hide: false },
};

describe("<Checkboxes /> Rendering Tests", () => {
  afterEach(cleanup);

  it(`Should render in the dom`, () => {
    render(<Checkboxes {...props} />);
    expect(screen.queryByTestId(testId)).toBeTruthy();
  });

  it(`Should render an EDITOR or LIVE version of the component based on the value of the mode prop`, () => {
    const testIdLive = "FormBuilder-FormFields-Checkboxes-LIVE";
    const testIdEditor = "FormBuilder-FormFields-Checkboxes-EDITOR";

    const { rerender } = render(
      <Checkboxes {...props} mode={EditorMode.LIVE} />
    );
    expect(screen.queryByTestId(testIdLive)).toBeTruthy();

    rerender(<Checkboxes {...props} mode={EditorMode.EDITOR} />);
    expect(screen.queryByTestId(testIdEditor)).toBeTruthy();
  });

  it(`Should render a regular list of checkboxes when the mode is live`, () => {
    const testId = "FormBuilder-FormFields-Checkboxes-Checkbox";
    render(<Checkboxes {...props} mode={EditorMode.LIVE} />);
    expect(screen.queryAllByTestId(testId).length).toEqual(data.length);
  });

  it(`Should render the AttributeContainer component when the mode is edit`, () => {
    const attrTestId = "FormBuilder-AttributeContainer";
    render(<Checkboxes {...props} mode={EditorMode.EDITOR} />);
    expect(screen.queryByTestId(attrTestId)).toBeTruthy();
  });

  test.skip(`Should render a question that asks whether or not to show it as a checkbox or a toggle switch`, () => {});

  it(`Should see that the <Label /> component has been rendered in the dom`, () => {
    render(<Checkboxes {...props} mode={EditorMode.EDITOR} />);
    expect(screen.queryByTestId(labelTestId)).toBeTruthy();
  });

  it(`Should see that the <Description /> component has been rendered in the dom`, () => {
    render(<Checkboxes {...props} mode={EditorMode.EDITOR} />);
    expect(screen.queryByTestId(descriptionTestId)).toBeTruthy();
  });

  it(`Should see that the <Column /> component has been rendered in the dom`, () => {
    const columnsTestId = "FormBuilder-Attributes-Columns";
    render(<Checkboxes {...props} mode={EditorMode.EDITOR} />);
    expect(screen.queryByTestId(columnsTestId)).toBeTruthy();
  });

  it(`Should see that the <Required /> component has been rendered in the dom`, () => {
    render(<Checkboxes {...props} mode={EditorMode.EDITOR} />);
    expect(screen.queryByTestId(requiredTestId)).toBeTruthy();
  });

  it(`Should see that the <SortableList /> component has been rendered in the dom`, () => {
    const sortableListTestId = "Properties-SortableList";
    render(<Checkboxes {...props} mode={EditorMode.EDITOR} />);
    expect(screen.queryByTestId(sortableListTestId)).toBeTruthy();
  });

  it(`Should render an descriptionStyle, containerStyle, and labelStyle when specified`, () => {
    const label = "Label Check";
    const description = "Description Check";

    render(<Checkboxes {...props} label={label} description={description} />);

    // descriptionStyle
    expect(
      styleClean(screen.getByText(description).getAttribute("style"))
    ).toContain(styleClean(reactToCSS(styleData.descriptionStyle)));

    // containerStyle
    expect(
      styleClean(screen.getByTestId(testId).getAttribute("style"))
    ).toContain(styleClean(reactToCSS(styleData.containerStyle)));

    // labelStyle
    expect(styleClean(screen.getByText(label).getAttribute("style"))).toContain(
      styleClean(reactToCSS(styleData.labelStyle))
    );
  });

  test.skip(`Should only see that the <Validation /> component has been rendered if the "hide" value is false`, () => {});
});

describe("<Checkboxes /> Event Tests", () => {
  afterEach(cleanup);

  test.skip(`Should trigger the onChange event (via handleChange()) if the AttributeContainer's onChange event is called`, () => {});

  it(`Should trigger the onChange event with appropriate payload if the toggle switch is toggled`, () => {
    const onChange = jest.fn();
    render(
      <Checkboxes {...props} onChange={onChange} mode={EditorMode.LIVE} />
    );
    fireEvent.click(
      screen
        .queryAllByTestId("FormBuilder-FormFields-Checkboxes-Checkbox")[0]
        .querySelector("input"),
      { checked: true }
    );
    expect(onChange).toBeCalled();
  });
});

describe("<Checkboxes /> Action Tests", () => {
  afterEach(cleanup);

  test.todo(
    `handleChange should convert the incoming payload to LiveFormFieldProps`
  );

  test.todo(
    `Should trigger the onChange event with appropriate payload (via handleChange()) when the list sortable items change`
  );
});
