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 { useEffect, useState } from 'react'
import { currentObjectState } from '@/store/canvasAtom'
import { useRoofAllocationSetting } from '@/hooks/roofcover/useRoofAllocationSetting'
import { useSwal } from '@/hooks/useSwal'

export default function ActualSizeSetting(props) {
  const contextPopupPosition = useRecoilValue(contextPopupPositionState)
  const { id, pos = contextPopupPosition } = props
  const currentObject = useRecoilValue(currentObjectState)
  const [planeSize, setPlaneSize] = useState(0)
  const [actualSize, setActualSize] = useState(0)
  const { setLineSize, handleAlloc } = useRoofAllocationSetting()
  const { closePopup } = usePopup()
  const { getMessage } = useMessage()
  const { swalFire } = useSwal()
  useEffect(() => {
    if (currentObject && currentObject.attributes) {
      setPlaneSize(currentObject.attributes.planeSize ?? 0)
      setActualSize(currentObject.attributes.actualSize ?? 0)
    }
  }, [currentObject])

  const handleFinish = () => {
    swalFire({
      text: '완료 하시겠습니까?',
      type: 'confirm',
      confirmFn: () => {
        handleAlloc()
      },
    })
  }

  const handleClose = () => {
    swalFire({
      text: '완료 하시겠습니까?',
      type: 'confirm',
      confirmFn: () => {
        handleAlloc()
      },
      // denyFn: () => {
      //   swalFire({ text: '취소되었습니다.', icon: 'error' })
      // },
    })
  }

  const handleApply = () => {
    if (!currentObject) {
      swalFire({ type: 'alert', icon: 'error', text: getMessage('modal.actual.size.setting.not.exist.auxiliary.line') })
      return
    }
    if (actualSize !== 0) {
      setLineSize(currentObject?.id, actualSize)
    } else {
      swalFire({ type: 'alert', icon: 'error', text: getMessage('modal.actual.size.setting.not.exist.size') })
    }
  }

  return (
    <WithDraggable isShow={true} pos={pos} className="ssm">
      <WithDraggable.Header title={getMessage('modal.actual.size.setting')} onClose={() => closePopup(id)} />
      <WithDraggable.Body>
        <div className="guide">
          <span>{getMessage('modal.actual.size.setting.info')}</span>
        </div>
        <div className="properties-setting-wrap outer">
          <div className="setting-tit">{getMessage('setting')}</div>
          <div className="outline-wrap">
            <div className="eaves-keraba-table">
              <div className="eaves-keraba-item">
                <div className="eaves-keraba-th">{getMessage('modal.actual.size.setting.plane.size.length')}</div>
                <div className="eaves-keraba-td">
                  <div className="outline-form">
                    <div className="input-grid mr5">
                      <input type="text" className="input-origin block" value={planeSize} readOnly={true} />
                    </div>
                    <span className="thin">mm</span>
                  </div>
                </div>
              </div>
              <div className="eaves-keraba-item">
                <div className="eaves-keraba-th">{getMessage('modal.actual.size.setting.actual.size.length')}</div>
                <div className="eaves-keraba-td">
                  <div className="outline-form">
                    <div className="input-grid mr5">
                      <input type="text" className="input-origin block" value={actualSize} onChange={(e) => setActualSize(Number(e.target.value))} />
                    </div>
                    <span className="thin">mm</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div className="grid-btn-wrap">
          <button className="btn-frame modal mr5" onClick={handleFinish}>
            {getMessage('common.setting.finish')}
          </button>
          <button className="btn-frame modal act" onClick={handleApply}>
            {getMessage('apply')}
          </button>
        </div>
      </WithDraggable.Body>
    </WithDraggable>
  )
}
