import React, { useEffect, useState } from 'react'
import { useRecoilState, useSetRecoilState } from 'recoil'
import { settingModalGridOptionsState } from '@/store/settingAtom'
import { useMessage } from '@/hooks/useMessage'
import { adsorptionPointAddModeState } from '@/store/canvasAtom'
import { useTempGrid } from '@/hooks/useTempGrid'
import { gridColorState } from '@/store/gridAtom'
import { useColor } from 'react-color-palette'
import ColorPickerModal from '@/components/common/color-picker/ColorPickerModal'
import { usePopup } from '@/hooks/usePopup'
import { v4 as uuidv4 } from 'uuid'
import DotLineGrid from '@/components/floor-plan/modal/grid/DotLineGrid'
export default function GridOption() {
const [gridOptions, setGridOptions] = useRecoilState(settingModalGridOptionsState)
const [adsorptionPointAddMode, setAdsorptionPointAddMode] = useRecoilState(adsorptionPointAddModeState)
const setSettingModalGridOptions = useSetRecoilState(settingModalGridOptionsState)
const { getMessage } = useMessage()
const { tempGridMode, setTempGridMode } = useTempGrid()
const [gridColor, setGridColor] = useRecoilState(gridColorState)
const [color, setColor] = useColor(gridColor)
const [showColorPickerModal, setShowColorPickerModal] = useState(false)
const [showDotLineGridModal, setShowDotLineGridModal] = useState(false)
const { addPopup, closePopup } = usePopup()
let [colorId, dotLineId] = ''
useEffect(() => {
setGridColor(color.hex)
}, [color])
useEffect(() => {
console.log(showColorPickerModal)
gridOptions[3].selected = showColorPickerModal
setGridOptions([...gridOptions])
}, [showColorPickerModal])
useEffect(() => {
colorId = uuidv4()
dotLineId = uuidv4()
return () => {
setSettingModalGridOptions((prev) => {
const newSettingOptions = [...prev]
newSettingOptions[3].selected = false
return [...newSettingOptions]
})
}
}, [])
const onClickOption = (option) => {
const { id, name, selected } = option
const newGridOptions = [...gridOptions]
// 초기화
setShowColorPickerModal(false)
setShowDotLineGridModal(false)
setTempGridMode(false)
setAdsorptionPointAddMode(false)
closePopup(dotLineId)
closePopup(colorId)
//
newGridOptions.forEach((item) => {
if (item.id === id) {
item.selected = !item.selected
} else {
item.selected = false
}
})
const selectedOption = newGridOptions.find((item) => item.id === option.id)
if (selectedOption.id === 1) {
// 임의 그리드 모드
setTempGridMode(selectedOption.selected)
} else if (selectedOption.id === 2) {
// 점 선 그리드 설정 모달
setShowDotLineGridModal(selectedOption.selected)
addPopup(dotLineId, 2,