import { useUndoRedo } from '@/hooks/useUndoRedo'
import WithDraggable from '@/components/common/draggable/WithDraggable'
import Image from 'next/image'
import { useState } from 'react'
import { useRecoilValue } from 'recoil'
import { contextPopupPositionState } from '@/store/popupAtom'
import { usePopup } from '@/hooks/usePopup'
import { useMessage } from '@/hooks/useMessage'
import { MODULE_INSERT_TYPE, useModule } from '@/hooks/module/useModule'

export default function RowInsert(props) {
  const contextPopupPosition = useRecoilValue(contextPopupPositionState)
  const { id, pos = contextPopupPosition, apply } = props
  const { closePopup } = usePopup()
  const [selectedType, setSelectedType] = useState(MODULE_INSERT_TYPE.TOP)
  const { getMessage } = useMessage()
  const { muduleRowInsert } = useModule()
  const { saveSnapshot } = useUndoRedo()
  const handleApply = () => {
    saveSnapshot()
    muduleRowInsert(selectedType)
    closePopup(id)
  }

  const HandleRadioChange = (e) => {
    setSelectedType(e.target.value)
  }

  return (
    <WithDraggable isShow={true} pos={pos} className="r">
      <WithDraggable.Header title={getMessage('modal.row.insert')} onClose={() => closePopup(id)} />
      <WithDraggable.Body>
        <div className="properties-setting-wrap">
          <div className="setting-tit">{getMessage('modal.row.insert.info')}</div>
          <div className="additional-wrap">
            <div className="additional-radio-wrap">
              <div className="d-check-radio pop">
                <input
                  type="radio"
                  name="radio01"
                  id="ra01"
                  onChange={HandleRadioChange}
                  value={MODULE_INSERT_TYPE.TOP}
                  checked={selectedType === MODULE_INSERT_TYPE.TOP}
                />
                <label htmlFor="ra01">{getMessage('modal.row.insert.type.up')}</label>
              </div>
              <div className="d-check-radio pop">
                <input
                  type="radio"
                  name="radio01"
                  id="ra02"
                  onChange={HandleRadioChange}
                  value={MODULE_INSERT_TYPE.BOTTOM}
                  checked={selectedType === MODULE_INSERT_TYPE.BOTTOM}
                />
                <label htmlFor="ra02">{getMessage('modal.row.insert.type.down')}</label>
              </div>
            </div>
            <div className="additional-img-wrap">
              {selectedType === MODULE_INSERT_TYPE.TOP && (
                <Image
                  src="/static/images/canvas/additional_bundle-edit01.svg"
                  alt="react"
                  width={0}
                  height={0}
                  style={{ width: 'auto', height: 'auto' }}
                />
              )}
              {selectedType === MODULE_INSERT_TYPE.BOTTOM && (
                <Image
                  src="/static/images/canvas/additional_bundle-edit02.svg"
                  alt="react"
                  width={0}
                  height={0}
                  style={{ width: 'auto', height: 'auto' }}
                />
              )}
            </div>
          </div>
        </div>
        <div className="properties-setting-wrap">
          <div className="setting-tit">{getMessage('legend')}</div>
          <div className="module-table-box">
            <div className="module-table-inner">
              <div className="additional-color-wrap">
                <div className="additional-color-box">
                  <span className="additional-color pink"></span>
                  <span className="normal-font">{getMessage('modal.panel.select.row')}</span>
                </div>
                <div className="additional-color-box">
                  <span className="additional-color white"></span>
                  <span className="normal-font">{getMessage('modal.panel.insert.row')}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div className="grid-btn-wrap">
          <button className="btn-frame modal act" onClick={handleApply}>
            {getMessage('modal.common.save')}
          </button>
        </div>
        <div className="modal-foot modal-handle"></div>
      </WithDraggable.Body>
    </WithDraggable>
  )
}
