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 Image from 'next/image'
import { currentObjectState } from '@/store/canvasAtom'
import { useAuxiliaryDrawing } from '@/hooks/roofcover/useAuxiliaryDrawing'
import { useSwal } from '@/hooks/useSwal'
import { normalizeDigits } from '@/util/input-utils'
import { CalculatorInput } from '@/components/common/input/CalcInput'

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 [useCalcPad, setUseCalcPad] = useState(false)
  const disableKeypad = !useCalcPad
  const handleSave = () => {
    if ((!arrow1 && !arrow2) || (+verticalSize === 0 && +horizonSize === 0)) {
      swalFire({ title: getMessage('length.direction.is.required'), type: 'alert' })
      return
    }

    if (verticalSize && +verticalSize !== 0 && !arrow1) {
      swalFire({ title: getMessage('length.direction.is.required'), type: 'alert' })
      return
    }

    if (horizonSize && +horizonSize !== 0 && !arrow2) {
      swalFire({ title: getMessage('length.direction.is.required'), type: 'alert' })
      return
    }

    if (type === 'copy') {
      if (currentObject) {
        copy(
          currentObject,
          arrow2 ? (arrow2 === '←' ? (Number(normalizeDigits(horizonSize)) / 10) * -1 : Number(normalizeDigits(horizonSize)) / 10) : 0,
          arrow1 ? (arrow1 === '↑' ? (Number(normalizeDigits(verticalSize)) / 10) * -1 : Number(normalizeDigits(verticalSize)) / 10) : 0,
        )
      }
    } else {
      move(
        currentObject,
        arrow2 ? (arrow2 === '←' ? (Number(normalizeDigits(horizonSize)) / 10) * -1 : Number(normalizeDigits(horizonSize)) / 10) : 0,
        arrow1 ? (arrow1 === '↑' ? (Number(normalizeDigits(verticalSize)) / 10) * -1 : Number(normalizeDigits(verticalSize)) / 10) : 0,
      )
    }

    closePopup(id)
  }
  return (
    <WithDraggable isShow={true} pos={pos} className="xm">
      <WithDraggable.Header title={getMessage(type === 'copy' ? 'modal.auxiliary.copy' : 'modal.auxiliary.move')} onClose={() => closePopup(id)} />
      <WithDraggable.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(normalizeDigits(e.target.value))} />*/}
                  <CalculatorInput
                    id=""
                    name=""
                    label=""
                    className="input-origin block"
                    value={verticalSize}
                    onChange={(value) => setVerticalSize(value)}
                    disableKeypad={disableKeypad}
                    options={{
                      allowNegative: false,
                      allowDecimal: false
                    }}
                  />
                </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(normalizeDigits(e.target.value))} />*/}
                  <CalculatorInput
                    id=""
                    name=""
                    label=""
                    className="input-origin block"
                    value={horizonSize}
                    onChange={(value) => setHorizonSize(value)}
                    disableKeypad={disableKeypad}
                    options={{
                      allowNegative: false,
                      allowDecimal: false
                    }}
                  />
                </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" style={{ display: 'flex', alignItems: 'center', justifyContent: 'flex-end', gap: '5px' }}>
          <button style={{ width: 'auto' }} onClick={() => setUseCalcPad((prev) => !prev)}>
            <Image src={useCalcPad ? '/static/images/common/Icon_ON_Black.png' : '/static/images/common/Icon_OFF_Black.png'} alt="toggle" width={34} height={34} />
          </button>
          <button className="btn-frame modal act" onClick={handleSave}>
            {getMessage('modal.common.save')}
          </button>
        </div>
      </WithDraggable.Body>
    </WithDraggable>
  )
}
