'use client'

import Image from 'next/image'
import { useCallback, useEffect, useState } from 'react'
import { usePopupController } from '@/store/popupController'
import { useSuitableStore } from '@/store/useSuitableStore'
import SuitableDetailPopupButton from './SuitableDetailPopupButton'
import { useSuitable } from '@/hooks/useSuitable'
import { Suitable } from '@/types/Suitable'

export default function SuitableDetailPopup() {
  const popupController = usePopupController()
  const { getSuitableDetails, serializeSelectedItems } = useSuitable()
  const { selectedItems } = useSuitableStore()

  const [openItems, setOpenItems] = useState<Set<number>>(new Set())
  const [suitableDetails, setSuitableDetails] = useState<Suitable[]>([])

  // 아이템 열기/닫기
  const toggleItemOpen = useCallback((itemId: number) => {
    setOpenItems((prev) => {
      const newOpenItems = new Set(prev)
      newOpenItems.has(itemId) ? newOpenItems.delete(itemId) : newOpenItems.add(itemId)
      return newOpenItems
    })
  }, [])

  // 선택된 아이템 상세 데이터 가져오기
  const getSelectedItemsData = async () => {
    const serialized: Map<string, string> = serializeSelectedItems()
    setSuitableDetails(await getSuitableDetails(serialized.get('ids') ?? '', serialized.get('detailIds') ?? ''))
  }

  useEffect(() => {
    getSelectedItemsData()
  }, [])

  return (
    <div className="modal-popup">
      <div className="modal-dialog">
        <div className="modal-content">
          <div className="modal-header">
            <div className="modal-header-inner">
              <div className="modal-name-wrap">
                <div className="modal-img">
                  <Image src="/assets/images/layout/modal_header_icon03.svg" width={22} height={22} alt="" />
                </div>
                <div className="modal-name">屋根材適合性詳細 </div>
              </div>
              <button className="modal-close" onClick={() => popupController.setSuitableDetailPopup(false)}></button>
            </div>
          </div>
          <div className="modal-body">
            <div className="compliance-check-pop-wrap">
              <div className={`compliance-check-bx ${openItems.has(1) ? 'act' : ''}`}>
                <div className="check-name-wrap">
                  <div className="check-name">アースティ４０</div>
                  <div className="check-name-btn">
                    <button className="bx-btn" onClick={() => toggleItemOpen(1)}></button>
                  </div>
                </div>
                <div className="compliance-check-pop-contents">
                  <div className="check-pop-data-wrap">
                    <div className="check-pop-data-tit">屋根技研 支持瓦</div>
                    <div className="check-pop-data-txt">㈱ダイトー</div>
                  </div>
                  <div className="check-pop-data-wrap">
                    <div className="check-pop-data-tit">屋根材</div>
                    <div className="check-pop-data-txt">瓦</div>
                  </div>
                  <div className="check-pop-data-wrap">
                    <div className="check-pop-data-tit">金具タイプ</div>
                    <div className="check-pop-data-txt">木ねじ打ち込み式</div>
                  </div>
                  <div className="check-pop-data-table-wrap">
                    <div className="check-pop-data-table">
                      <div className="pop-data-table-head">
                        <div className="pop-data-table-head-name">屋根技研 支持瓦</div>
                        <div className="pop-data-table-head-icon">
                          <div className="compliance-icon">
                            <Image src={'/assets/images/sub/compliance_check_icon.svg'} width={22} height={22} alt=""></Image>
                          </div>
                          <div className="compliance-icon">
                            <Image src={'/assets/images/sub/compliance_tip_icon.svg'} width={22} height={22} alt=""></Image>
                          </div>
                        </div>
                      </div>
                      <div className="pop-data-table-body">Ｄで設置可</div>
                      <div className="pop-data-table-footer">
                        <div className="pop-data-table-footer-unit">備考</div>
                        <div className="pop-data-table-footer-data">
                          桟木なしの場合は支持金具平ー１で設置可能。その場合水返しが高い為、レベルプレート使用。桟木ありの場合は支持金具平ー２で設置可能
                        </div>
                      </div>
                    </div>
                    <div className="check-pop-data-table">
                      <div className="pop-data-table-head">
                        <div className="pop-data-table-head-name">屋根技研支持金具</div>
                        <div className="pop-data-table-head-icon">
                          <div className="compliance-icon">
                            <Image src={'/assets/images/sub/compliance_x_icon.svg'} width={22} height={22} alt=""></Image>
                          </div>
                          <div className="compliance-icon">
                            <Image src={'/assets/images/sub/compliance_tip_icon.svg'} width={22} height={22} alt=""></Image>
                          </div>
                        </div>
                      </div>
                      <div className="pop-data-table-body">設置不可</div>
                      <div className="pop-data-table-footer">
                        <div className="pop-data-table-footer-unit">備考</div>
                        <div className="pop-data-table-footer-data">入手困難</div>
                      </div>
                    </div>
                    <div className="check-pop-data-table">
                      <div className="pop-data-table-head">
                        <div className="pop-data-table-head-name">屋根技研YGアンカー</div>
                        <div className="pop-data-table-head-icon">
                          <div className="compliance-icon">
                            <Image src={'/assets/images/sub/compliance_quest_icon.svg'} width={22} height={22} alt=""></Image>
                          </div>
                        </div>
                      </div>
                      <div className="pop-data-table-body">お問い合わせください</div>
                      <div className="pop-data-table-footer">
                        <div className="pop-data-table-footer-unit">備考</div>
                        <div className="pop-data-table-footer-data">入手困難</div>
                      </div>
                    </div>
                    <div className="check-pop-data-table">
                      <div className="pop-data-table-head">
                        <div className="pop-data-table-head-name">ダイドーハント支持瓦Ⅱ</div>
                        <div className="pop-data-table-head-icon">
                          <div className="compliance-icon">
                            <Image src={'/assets/images/sub/compliance_check_icon.svg'} width={22} height={22} alt=""></Image>
                          </div>
                        </div>
                      </div>
                      <div className="pop-data-table-body">Ⅳ (Ｄ) で設置可</div>
                      <div className="pop-data-table-footer">
                        <div className="pop-data-table-footer-unit">備考</div>
                        <div className="pop-data-table-footer-data">入手困難</div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <SuitableDetailPopupButton />
          </div>
        </div>
      </div>
    </div>
  )
}
