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 { useRecoilState, useRecoilValue } from 'recoil'
import { contextPopupPositionState } from '@/store/popupAtom'
import { useEffect, useState } from 'react'
import { basicSettingState } from '@/store/settingAtom'
import { ROOF_MATERIAL_LAYOUT } from '@/components/floor-plan/modal/placementShape/PlacementShapeSetting'
import { useCanvasSetting } from '@/hooks/option/useCanvasSetting'
import { useCommonCode } from '@/hooks/common/useCommonCode'
import { currentAngleTypeSelector, pitchTextSelector } from '@/store/canvasAtom'

export default function ContextRoofAllocationSetting(props) {
  const contextPopupPosition = useRecoilValue(contextPopupPositionState)
  const { id, pos = contextPopupPosition } = props
  const { getMessage } = useMessage()
  const { closePopup } = usePopup()
  const {
    handleSave,
    onAddRoofMaterial,
    onDeleteRoofMaterial,
    roofMaterials,
    setCurrentRoofMaterial,
    roofList,
    handleDefaultRoofMaterial,
    handleChangeRoofMaterial,
    handleChangeRaft,
    handleChangeLayout,
    handleSaveContext,
    currentRoofList,
    handleChangeInput,
  } = useRoofAllocationSetting(id)

  const { findCommonCode } = useCommonCode()
  const pitchText = useRecoilValue(pitchTextSelector)
  const currentAngleType = useRecoilValue(currentAngleTypeSelector)
  const [raftCodes, setRaftCodes] = useState([])
  useEffect(() => {
    const raftCodeList = findCommonCode('203800')
    setRaftCodes(raftCodeList.map((raft) => ({ ...raft, value: raft.clCode, name: raft.clCodeNm })))
  }, [])

  return (
    <WithDraggable isShow={true} pos={pos}>
      <div className={`modal-pop-wrap lr mount`}>
        <div className="modal-head modal-handle">
          <h1 className="title">{getMessage('plan.menu.estimate.roof.alloc')}</h1>
          <button className="modal-close" onClick={() => closePopup(id)}>
            닫기
          </button>
        </div>
        <div className="modal-body">
          <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}
                value={roofMaterials[0]}
                onChange={(e) => {
                  // const selected = roofMaterials.find((roofMaterial) => roofMaterial.roofMatlCd === e.id)
                  setCurrentRoofMaterial(e)
                }}
                showKey={'roofMatlNm'}
                sourceKey={'roofMatlCd'}
                targetKey={'roofMatlCd'}
              />
            </div>
            <button
              className="allocation-edit"
              onClick={() => {
                onAddRoofMaterial()
              }}
            >
              <i className="edit-ico"></i>
              {getMessage('modal.common.add')}
            </button>
          </div>
          <div className="grid-option-wrap">
            {currentRoofList.length > 0 &&
              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 && 'checked'} readOnly={true} />
                      <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" style={{ width: '248px' }}>
                            <QSelectBox
                              options={roofMaterials}
                              value={roof}
                              showKey={'roofMatlNm'}
                              sourceKey={'roofMatlCd'}
                              targetKey={'roofMatlCd'}
                              onChange={(e) => handleChangeRoofMaterial(e, index)}
                            />
                          </div>
                          {index === 0 && <span className="dec">{getMessage('modal.roof.alloc.default.roof.material')}</span>}
                          {index !== 0 && <button className="delete" onClick={() => onDeleteRoofMaterial(index)}></button>}
                        </div>
                      </div>
                      <div className="block-box">
                        <div className="flex-ment">
                          <span>{getMessage('slope')}</span>
                          <div className="input-grid" style={{ width: '214px' }}>
                            <input
                              type="text"
                              className="input-origin block"
                              onChange={(e) => {
                                handleChangeInput(e, currentAngleType === 'slope' ? 'pitch' : 'angle', index)
                              }}
                              defaultValue={currentAngleType === 'slope' ? roof.pitch : roof.angle}
                            />
                          </div>
                          <span>{pitchText}</span>
                        </div>
                      </div>
                      {(roof.widAuth || roof.lenAuth) && (
                        <div className="block-box">
                          {roof.widAuth && (
                            <div className="flex-ment">
                              <span>W</span>
                              <div className="input-grid" style={{ width: '100px' }}>
                                <input type="text" className="input-origin block" defaultValue={roof.width} readOnly={roof.widAuth === 'R'} />
                              </div>
                            </div>
                          )}
                          {roof.lenAuth && (
                            <div className="flex-ment">
                              <span>L</span>
                              <div className="input-grid" style={{ width: '100px' }}>
                                <input type="text" className="input-origin block" defaultValue={roof.length} readOnly={roof.lenAuth === 'R'} />
                              </div>
                            </div>
                          )}
                        </div>
                      )}
                      {(roof.raftAuth || roof.roofPchAuth) && (
                        <div className="block-box">
                          {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" style={{ width: '160px' }}>
                                    <QSelectBox
                                      options={raftCodes}
                                      value={roof}
                                      showKey={'clCodeNm'}
                                      sourceKey={'clCode'}
                                      targetKey={roof.raft ? 'raft' : 'raftBaseCd'}
                                    />
                                  </div>
                                )}
                              </div>
                            </div>
                          )}
                          {roof.roofPchAuth && (
                            <div className="block-box">
                              <div className="flex-ment">
                                <span>{getMessage('hajebichi')}</span>
                                <div className="input-grid" style={{ width: '84px' }}>
                                  <input
                                    type="text"
                                    className="input-origin block"
                                    value={parseInt(roof.hajebichi)}
                                    readOnly={roof.roofPchAuth === 'R'}
                                  />
                                </div>
                              </div>
                            </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 className="grid-btn-wrap">
            <button className="btn-frame modal act" onClick={handleSaveContext}>
              {getMessage('modal.roof.alloc.apply')}
            </button>
          </div>
        </div>
        <div className="modal-foot modal-handle"></div>
      </div>
    </WithDraggable>
  )
}
