import "./styles.css";

import { useEffect, useState } from "react";

import Body from "../../../src/layouts/Body/index.js";
import Container from "../../layouts/Container/index.js";
import allData from "../../data/dataFiltered.js";
import levels from "../../data/levels.js";

const Character = () => {
  const [data, setData] = useState();
  const [selectedLevel, setSelectedLevel] = useState();
  useEffect(() => {
    const filterDataByFolder = (folderId) => {
      const filteredData = allData.filter((item) => item.folder === folderId);

      return filteredData.map((item) => {
        const subItems = filterDataByFolder(item._id);
        return {
          ...item,
          subItems,
        };
      });
    };

    const mainFolders = allData.filter((item) => item.folder === null);
    const filteredData = mainFolders.map((item) => {
      const subItems = filterDataByFolder(item._id);
      return {
        ...item,
        subItems,
      };
    });

    setData(filteredData);
    setSelectedLevel(0);
  }, [setData]);

  let levelDom = document.getElementsByClassName("level");

  for (let i = 0; i < levelDom.length; i++) {
    // eslint-disable-next-line no-loop-func
    levelDom[i].addEventListener("click", () => {
      for (let j = 0; j < levelDom.length; j++) {
        levelDom[j].classList.remove("selected");
      }
      levelDom[i].classList.add("selected");
      setSelectedLevel(i + 1);
    });
  }

  const renderSubItems = (subItems) => {
    if (subItems?.length === 0) {
      return subItems?.length !== 0 ? (
        <div>
          <h4>
            {subItems?._id} - {subItems?.name}
          </h4>
        </div>
      ) : (
        <></>
      );
    } else {
      return subItems?.map((subItem, subId) => {
        return (
          <div key={`subItem-${subId}`} style={{ color: "green" }}>
            {subItems?.length !== 0 ? (
              <div>
                <h5
                  className={
                    subItem?._key === `!folders!${subItem?._id}`
                      ? "selected"
                      : ""
                  }
                >
                  {subItem?._id} - {subItem?.name} - {subItem?.folder}
                </h5>
              </div>
            ) : (
              <></>
            )}
            {renderSubItems(subItem?.subItems)}
          </div>
        );
      });
    }
  };

  return (
    <div className="character">
      <Container>
        <div
          style={{
            display: "flex",
            justifyContent: "space-around",
            flexDirection: "row",
          }}
        >
          {levels.map((item, id) => (
            <div className={`level level-${id}`} key={`level-${id}`}>
              <h2>{item?.name}</h2>
            </div>
          ))}
        </div>
        {/* {data?.map((item, id) => (
          <div key={`top-${id}`}>
            <div>
              <h3>
                {item?._id} - {item?.name}
              </h3>
            </div>
            {renderSubItems(item?.subItems)}
          </div>
        ))} */}

        <Body level={selectedLevel} />
      </Container>
    </div>
  );
};

export default Character;
