import {tempMapper} from "../../config";
import {useEffect, useMemo} from "react";
import {useGetRegions} from "../../components/RegionSelect.jsx";
import {useTable} from "../../stores/useTable.js";
import useLocalStorage from "../../hooks/useLocalStorage.js";
import {useWeatherFilter} from "../../dzkh-features/WeatherSlider/useWeatherFilter.js";

export const useColumns = (key) => {
  const { data: regions } = useGetRegions();
  const {
    tableState: { fullScreen },
  } = useTable();

  const {value: temperature} = useWeatherFilter();

  const [sort, setSort] = useLocalStorage(`${key}Sort`, null);

  const defaultColumns = useMemo(() => {
    return [
      {
        title: "Address",
        dataIndex: "building_address",
        key: "building_address",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "TP number",
        dataIndex: "tp_number",
        key: "tp_number",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Dispatcher center number",
        dataIndex: "ods_number",
        key: "ods_number",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Dispatcher phone",
        dataIndex: "phone_number",
        key: "phone_number",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "No heating in the building",
        dataIndex: `prob1_${tempMapper[temperature]}`,
        key: "prob1",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Leaking pipes in the entrance",
        dataIndex: `prob2_${tempMapper[temperature]}`,
        key: "prob2",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Apartment temperature below standard",
        dataIndex: `prob3_${tempMapper[temperature]}`,
        key: "prob3",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Common area temperature below standard",
        dataIndex: `prob4_${tempMapper[temperature]}`,
        key: "prob4",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Leak in the heating system",
        dataIndex: `prob5_${tempMapper[temperature]}`,
        key: "prob5",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "TP failure",
        dataIndex: `prob6_${tempMapper[temperature]}`,
        key: "prob6",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },

      {
        title: "Cooling time, h",
        dataIndex: `cooling_time_${tempMapper[temperature]}`,
        key: "cooling_time",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Building priority",
        dataIndex: `priority_${tempMapper[temperature]}`,
        key: "priority",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Wall material",
        dataIndex: "wall_materials",
        key: "wall_materials",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
      {
        title: "Energy efficiency class",
        dataIndex: "fi_energy_class",
        key: "fi_energy_class",
        width: "120px",
        ellipsis: true,
        sorter: true,
        showSorterTooltip: false,
      },
    ].filter(Boolean);
  }, [regions?.normalized, fullScreen, temperature]);

  const [order, setOrder] = useLocalStorage(`${key}Order`, defaultColumns.map((column, index) => {
    return {
      key: column.key,
      position: index,
      show: true,
    }
  }));

  useEffect(() => {
    const newColumns = defaultColumns.filter((column) => {
      return !order.find(c => c.key === column.key);
    });
    const newOrderColumns = newColumns.map((column, index) => {
      return {
        key: column.key,
        position: defaultColumns.length - index - 1,
        show: true,
      }
    });
    setOrder([
      ...order,
      ...newOrderColumns
    ]);
  }, [defaultColumns]);

  const columns = useMemo(() => {
    return order.flatMap((item) => !item.show ? [] : defaultColumns[item.position])
      .map((column) => {
        if (sort && sort.includes(column?.key)) return {
          ...column,
          defaultSortOrder: sort.includes('-') ? 'descend' : 'ascend',
        };
        return column;
      }).filter(Boolean);
  }, [defaultColumns, order, sort]);

  return {
    columns,
    orderColumns: {
      defaultColumns,
      order,
      setOrder,
    },
    sort,
    setSort
  };
};
