import { useMessage } from '@/hooks/useMessage'
import WithDraggable from '@/components/common/draggable/WithDraggable'
import QSelectBox from '@/components/common/select/QSelectBox'
import { useRoofAllocationSetting } from '@/hooks/roofcover/useRoofAllocationSetting'
import { usePopup } from '@/hooks/usePopup'
import { useRecoilValue } from 'recoil'
import { contextPopupPositionState } from '@/store/popupAtom'
import { useEffect, useState } from 'react'
import { ROOF_MATERIAL_LAYOUT } from '@/components/floor-plan/modal/placementShape/PlacementShapeSetting'
import { useCanvasSetting } from '@/hooks/option/useCanvasSetting'
import { useCommonCode } from '@/hooks/common/useCommonCode'
import { globalLocaleStore } from '@/store/localeAtom'
import { useRoofShapeSetting } from '@/hooks/roofcover/useRoofShapeSetting'
import { currentAngleTypeSelector, pitchTextSelector } from '@/store/canvasAtom'
import { getDegreeByChon } from '@/util/canvas-util'
import { normalizeDecimalLimit, normalizeDigits } from '@/util/input-utils'

export default function RoofAllocationSetting(props) {
  const contextPopupPosition = useRecoilValue(contextPopupPositionState)
  const { id, pos = contextPopupPosition } = props
  const { getMessage } = useMessage()
  const { closePopup } = usePopup()
  const {
    handleSave,
    onAddRoofMaterial,
    onDeleteRoofMaterial,
    roofMaterials,
    currentRoofMaterial,
    setCurrentRoofMaterial,
    roofList,
    handleDefaultRoofMaterial,
    handleChangeRoofMaterial,
    handleChangeRaft,
    handleChangeLayout,
    currentRoofList,
    handleChangeInput,
    handleChangePitch,
  } = useRoofAllocationSetting(id)
  const pitchText = useRecoilValue(pitchTextSelector)
  const { findCommonCode } = useCommonCode()
  const [raftCodes, setRaftCodes] = useState([])
  const globalLocale = useRecoilValue(globalLocaleStore)
  const currentAngleType = useRecoilValue(currentAngleTypeSelector)
  useEffect(() => {
    const raftCodeList = findCommonCode('203800')
    setRaftCodes(raftCodeList.map((raft) => ({ ...raft, name: raft.clCodeNm })))
  }, [])

  return (
    <WithDraggable isShow={true} pos={pos} className="lr">
      <WithDraggable.Header title={getMessage('plan.menu.estimate.roof.alloc')} onClose={() => closePopup(id)} />
      <WithDraggable.Body>
        {currentRoofList && (
          <>
            <div className="properties-guide">{getMessage('modal.roof.alloc.info')}</div>
            <div className="allocation-select-wrap">
              {/* <span>{getMessage('modal.roof.alloc.select.roof.material')}</span>
                <div className="grid-select">
                  <QSelectBox
                    options={roofMaterials.map((roof) => {
                      return { ...roof, name: globalLocale === 'ko' ? roof.roofMatlNm : roof.roofMatlNmJp }
                    })}
                    value={currentRoofMaterial}
                    onChange={(e) => {
                      // const selected = roofMaterials.find((roofMaterial) => roofMaterial.roofMatlCd === e.id)
                      setCurrentRoofMaterial(e)
                    }}
                    showKey="name"
                    sourceKey={'roofMatlCd'}
                    targetKey={'roofMatlCd'}
                  />
                </div> */}
              <button
                className="allocation-edit"
                onClick={() => {
                  onAddRoofMaterial()
                }}
              >
                <i className="edit-ico"></i>
                {getMessage('modal.roof.alloc.add.roof.material')}
              </button>
            </div>
            <div className="grid-option-overflow">
              <div className="grid-option-wrap">
                {currentRoofList.map((roof, index) => {
                  return (
                    <div className="grid-option-box" key={index}>
                      <div className="d-check-radio pop no-text">
                        <input type="radio" name="radio01" checked={!!roof.selected} readOnly />
                        <label
                          htmlFor="ra01"
                          onClick={(e) => {
                            handleDefaultRoofMaterial(index)
                          }}
                        ></label>
                      </div>
                      <div className="grid-option-block-form">
                        <div className="block-box">
                          <div className="flex-ment">
                            <div className="grid-select">
                              <QSelectBox
                                //options={roofMaterials}
                                options={roofMaterials.map((roof2) => {
                                  return { ...roof2, name: globalLocale === 'ko' ? roof2.roofMatlNm : roof2.roofMatlNmJp }
                                })}
                                value={roof}
                                //showKey={'roofMatlNm'}
                                showKey="name"
                                sourceKey={'roofMatlCd'}
                                targetKey={'roofMatlCd'}
                                onChange={(e) => handleChangeRoofMaterial(e, index)}
                              />
                            </div>
                            {index === 0 && <span className="absol dec">{getMessage('modal.roof.alloc.default.roof.material')}</span>}
                            {index !== 0 && (
                              <span className="absol">
                                <button className="delete" onClick={() => onDeleteRoofMaterial(index)}></button>
                              </span>
                            )}
                          </div>
                        </div>

                        {roof.raftAuth && (
                          <div className="block-box">
                            <div className="flex-ment">
                              <span>{getMessage('modal.placement.initial.setting.rafter')}</span>
                              {raftCodes.length > 0 && (
                                <div className="grid-select">
                                  <QSelectBox
                                    options={raftCodes}
                                    value={roof}
                                    showKey={'clCodeNm'}
                                    sourceKey={'clCode'}
                                    targetKey={roof.raft ? 'raft' : 'raftBaseCd'}
                                    onChange={(e) => handleChangeRaft(e, index)}
                                  />
                                </div>
                              )}
                            </div>
                          </div>
                        )}

                        {(roof.widAuth || roof.lenAuth) && (
                          <>
                            {roof.widAuth && (
                              <div className="block-box">
                                <div className="flex-ment">
                                  <span>W</span>
                                  <div className="input-grid">
                                    <input
                                      type="text"
                                      className="input-origin block"
                                      defaultValue={roof.width}
                                      onChange={(e) => {
                                        e.target.value = normalizeDigits(e.target.value)
                                        handleChangeInput(e, 'width', index)
                                      }}
                                      readOnly={roof.widAuth === 'R'}
                                    />
                                  </div>
                                </div>
                              </div>
                            )}
                            {roof.lenAuth && (
                              <div className="block-box">
                                <div className="flex-ment">
                                  <span>L</span>
                                  <div className="input-grid">
                                    <input
                                      type="text"
                                      className="input-origin block"
                                      defaultValue={roof.length}
                                      onChange={(e) => {
                                        e.target.value = normalizeDigits(e.target.value)
                                        handleChangeInput(e, 'length', index)
                                      }}
                                      readOnly={roof.lenAuth === 'R'}
                                    />
                                  </div>
                                </div>
                              </div>
                            )}
                          </>
                        )}
                        {roof.roofPchAuth && (
                          <div className="block-box">
                            <div className="flex-ment">
                              <span>{getMessage('hajebichi')}</span>
                              <div className="input-grid">
                                <input
                                  type="text"
                                  className="input-origin block"
                                  onChange={(e) => {
                                    e.target.value = normalizeDigits(e.target.value)
                                    handleChangeInput(e, 'hajebichi', index)
                                  }}
                                  value={roof.hajebichi ?? ''}
                                  readOnly={roof.roofPchAuth === 'R'}
                                />
                              </div>
                            </div>
                          </div>
                        )}
                        <div className="block-box">
                          <div className="flex-ment">
                            <span>{getMessage('modal.object.setting.offset.slope')}</span>
                            <div className="input-grid">
                              <input
                                type="text"
                                className="input-origin block"
                                onChange={(e) => {
                                  e.target.value = normalizeDecimalLimit(e.target.value, 2)
                                  handleChangePitch(e, index)
                                }}
                                value={currentAngleType === 'slope' ? (roof.pitch ?? '') : (roof.angle ?? '')}
                              />
                            </div>
                            <span className="absol">{pitchText}</span>
                          </div>
                        </div>
                        {/* <div className="block-box">
                            <div className="icon-btn-wrap">
                              <button
                                className={roof.layout === ROOF_MATERIAL_LAYOUT.PARALLEL ? 'act' : ''}
                                onClick={() => {
                                  handleChangeLayout(ROOF_MATERIAL_LAYOUT.PARALLEL, index)
                                }}
                              >
                                {getMessage('modal.roof.alloc.select.parallel')}
                                <i className="allocation01"></i>
                              </button>
                              <button
                                className={roof.layout === ROOF_MATERIAL_LAYOUT.STAIRS ? 'act' : ''}
                                onClick={() => {
                                  handleChangeLayout(ROOF_MATERIAL_LAYOUT.STAIRS, index)
                                }}
                              >
                                {getMessage('modal.roof.alloc.select.stairs')} <i className="allocation02"></i>
                              </button>
                            </div>
                          </div> */}
                      </div>
                    </div>
                  )
                })}
              </div>
            </div>
            <div className="grid-btn-wrap">
              <button className="btn-frame modal act" onClick={handleSave}>
                {getMessage('modal.roof.alloc.apply')}
              </button>
            </div>
          </>
        )}
      </WithDraggable.Body>
    </WithDraggable>
  )
}
