import WithDraggable from '@/components/common/draggable/WithDraggable'
import { useMessage } from '@/hooks/useMessage'
import { usePopup } from '@/hooks/usePopup'
import { useRecoilValue } from 'recoil'
import { contextPopupPositionState } from '@/store/popupAtom'
import { useState } from 'react'
import { currentObjectState } from '@/store/canvasAtom'
import { useGrid } from '@/hooks/common/useGrid'

export default function GridCopy(props) {
  const contextPopupPosition = useRecoilValue(contextPopupPositionState)
  const { id, pos = contextPopupPosition } = props
  const { getMessage } = useMessage()
  const { closePopup } = usePopup()
  const [length, setLength] = useState('0')
  const [arrow, setArrow] = useState(null)
  const currentObject = useRecoilValue(currentObjectState)
  const { copy } = useGrid()
  const handleApply = () => {
    copy(currentObject, ['↑', '←'].includes(arrow) ? +length * -1 : +length)
  }
  return (
    <WithDraggable isShow={true} pos={pos}>
      <div className={`modal-pop-wrap xm mount`}>
        <div className="modal-head modal-handle">
          <h1 className="title">{getMessage('modal.grid.copy')} </h1>
          <button className="modal-close" onClick={() => closePopup(id)}>
            닫기
          </button>
        </div>
        <div className="modal-body">
          <div className="left-bar modal-handle"></div>
          <div className="right-bar modal-handle"></div>
          <div className="grid-option-tit">{getMessage('modal.grid.copy.info')}</div>
          <div className="grid-option-wrap">
            <div className="grid-option-box">
              <div className="grid-input-form">
                <span className="mr10">{getMessage('modal.grid.copy.length')}</span>
                <div className="input-grid mr5">
                  <input type="text" className="input-origin" value={length} onChange={(e) => setLength(e.target.value)} />
                </div>
                <span>mm</span>
              </div>
              <div className="grid-direction">
                <button
                  className={`direction up ${arrow === '↑' ? 'act' : ''} ${currentObject?.direction === 'vertical' ? 'no-click' : ''}`}
                  onClick={() => {
                    if (currentObject?.direction === 'vertical') return
                    setArrow('↑')
                  }}
                ></button>
                <button
                  className={`direction down ${arrow === '↓' ? 'act' : ''} ${currentObject?.direction === 'vertical' ? 'no-click' : ''}`}
                  onClick={() => {
                    if (currentObject?.direction === 'vertical') return
                    setArrow('↓')
                  }}
                ></button>
                <button
                  className={`direction left ${arrow === '←' ? 'act' : ''} ${currentObject?.direction === 'horizontal' ? 'no-click' : ''}`}
                  onClick={() => {
                    if (currentObject?.direction === 'horizontal') return
                    setArrow('←')
                  }}
                ></button>
                <button
                  className={`direction right ${arrow === '→' ? 'act' : ''} ${currentObject?.direction === 'horizontal' ? 'no-click' : ''}`}
                  onClick={() => {
                    if (currentObject?.direction === 'horizontal') return
                    setArrow('→')
                  }}
                ></button>
              </div>
            </div>
          </div>
          <div className="grid-btn-wrap">
            <button className="btn-frame modal act" onClick={handleApply}>
              {getMessage('modal.grid.copy.save')}
            </button>
          </div>
        </div>
        <div className="modal-foot modal-handle"></div>
      </div>
    </WithDraggable>
  )
}
