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

export default function AuxiliaryEdit(props) {
  const contextPopupPosition = useRecoilValue(contextPopupPositionState)
  const { id, pos = contextPopupPosition, type } = props
  const { getMessage } = useMessage()
  const { closePopup } = usePopup()
  const { move, copy } = useAuxiliaryDrawing()
  const [verticalSize, setVerticalSize] = useState('0')
  const [horizonSize, setHorizonSize] = useState('0')
  const [arrow1, setArrow1] = useState(null)
  const [arrow2, setArrow2] = useState(null)
  const currentObject = useRecoilValue(currentObjectState)
  const { swalFire } = useSwal()
  const handleSave = () => {
    if (!horizonSize || !verticalSize || !arrow1 || !arrow2) {
      swalFire({ title: '길이와 방향을 입력하세요.', type: 'alert' })
      return
    }
    if (type === 'copy') {
      if (currentObject) {
        copy(
          currentObject,
          arrow2 === '←' ? Number(horizonSize) * -1 : Number(horizonSize),
          arrow1 === '↑' ? Number(verticalSize) * -1 : Number(verticalSize),
        )
      }
    } else {
      move(
        currentObject,
        arrow2 === '←' ? Number(horizonSize) * -1 : Number(horizonSize),
        arrow1 === '↑' ? Number(verticalSize) * -1 : Number(verticalSize),
      )
    }

    closePopup(id)
  }
  return (
    <WithDraggable isShow={true} pos={pos}>
      <div className={`modal-pop-wrap xm mount`}>
        <div className="modal-head">
          <h1 className="title">{getMessage(type === 'copy' ? 'modal.auxiliary.copy' : 'modal.auxiliary.move')} </h1>
          <button className="modal-close" onClick={() => closePopup(id)}>
            닫기
          </button>
        </div>
        <div className="modal-body">
          <div className="grid-option-tit">{getMessage(type === 'copy' ? 'modal.auxiliary.copy.info' : 'modal.auxiliary.move.info')}</div>
          <div className="grid-option-wrap">
            <div className="grid-option-box">
              <div className="move-form">
                <p className="mb5">{getMessage('length')}</p>
                <div className="input-move-wrap mb5">
                  <div className="input-move">
                    <input type="text" className="input-origin" value={verticalSize} onChange={(e) => setVerticalSize(e.target.value)} />
                  </div>
                  <span>mm</span>
                  <div className="direction-move-wrap">
                    <button
                      className={`direction up ${arrow1 === '↑' ? 'act' : ''}`}
                      onClick={() => {
                        setArrow1('↑')
                        document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowUp' }))
                      }}
                    ></button>
                    <button
                      className={`direction down ${arrow1 === '↓' ? 'act' : ''}`}
                      onClick={() => {
                        setArrow1('↓')
                        document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }))
                      }}
                    ></button>
                  </div>
                </div>
                <div className="input-move-wrap">
                  <div className="input-move">
                    <input type="text" className="input-origin" value={horizonSize} onChange={(e) => setHorizonSize(e.target.value)} />
                  </div>
                  <span>mm</span>
                  <div className="direction-move-wrap">
                    <button
                      className={`direction left ${arrow2 === '←' ? 'act' : ''}`}
                      onClick={() => {
                        setArrow2('←')
                        document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft' }))
                      }}
                    ></button>
                    <button
                      className={`direction right ${arrow2 === '→' ? 'act' : ''}`}
                      onClick={() => {
                        setArrow2('→')
                        document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' }))
                      }}
                    ></button>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div className="grid-btn-wrap">
            <button className="btn-frame modal act" onClick={handleSave}>
              {getMessage('modal.common.save')}
            </button>
          </div>
        </div>
      </div>
    </WithDraggable>
  )
}
