'use client'
import { Fragment, useCallback, useContext, useEffect, useState } from 'react'

import Link from 'next/link'
import { usePathname, useRouter } from 'next/navigation'

import { useRecoilState, useRecoilValue, useResetRecoilState } from 'recoil'
import { dimmedStore, sessionStore } from '@/store/commonAtom'

import { useMessage } from '@/hooks/useMessage'
import { checkSession, logout } from '@/lib/authActions'

import QSelectBox from '@/components/common/select/QSelectBox'

import UserInfoModal from '@/components/myInfo/UserInfoModal'
import { useAxios } from '@/hooks/useAxios'
import { globalLocaleStore } from '@/store/localeAtom'

import { stuffSearchState } from '@/store/stuffAtom'
import { QcastContext } from '@/app/QcastProvider'

import { usePopup } from '@/hooks/usePopup'
import { commonCodeState } from '@/store/commonCodeAtom'
import { isObjectNotEmpty } from '@/util/common-utils'

export const ToggleonMouse = (e, act, target) => {
  const listWrap = e.target.closest(target)
  const ListItem = Array.from(listWrap.childNodes)
  ListItem.forEach((el) => {
    if (act === 'add') {
      el.classList.add('mouse')
    } else {
      el.classList.remove('mouse')
    }
  })
  if (act === 'add') {
    e.target.parentElement.classList.remove('mouse')
  }
}

export default function Header(props) {
  const [userInfoModal, setUserInfoModal] = useState(false)
  const router = useRouter()

  const [stuffSearch, setStuffSearch] = useRecoilState(stuffSearchState)

  const { closeAll } = usePopup()

  const { userSession } = props
  const [sessionState, setSessionState] = useRecoilState(sessionStore)
  const { getMessage } = useMessage()
  const pathName = usePathname()
  // if (pathName.includes('login') || pathName.includes('join')) {
  //   return null
  // }
  const [selected, setSelected] = useState('')

  const { isGlobalLoading } = useContext(QcastContext)

  const dimmedState = useRecoilValue(dimmedStore)
  const isDimmed = dimmedState ? 'opacity-50 bg-black' : ''

  // Link 이동 자동 로그인
  const [globalLocaleState, setGlbalLocaleState] = useRecoilState(globalLocaleStore)
  const { promisePost } = useAxios(globalLocaleState)

  const qOrderUrl = process.env.NEXT_PUBLIC_Q_ORDER_AUTO_LOGIN_URL
  const qMusubiUrl = process.env.NEXT_PUBLIC_Q_MUSUBI_AUTO_LOGIN_URL

  const [SelectOptions, setSelectOptions] = useState([])

  const [commonCode, setCommonCode] = useRecoilState(commonCodeState)
  const { promiseGet } = useAxios()

  useEffect(() => {
    const getCommonCode = async () => {
      await promiseGet({ url: '/api/commcode/qc-comm-code' }).then((res) => {
        setCommonCode(Object.groupBy(res.data, ({ clHeadCd }) => clHeadCd))
      })
    }

    if (!isObjectNotEmpty(commonCode)) {
      getCommonCode()
    }
  }, [])

  const getAutoLoginParam = async () => {
    await promisePost({ url: '/api/login/v1.0/user/login/autoLoginEncryptData', data: { loginId: userSession.userId } })
      .then((res) => {
        if (res) {
          setSelectOptions(
            userSession.storeId === 'T01'
              ? [
                  { id: 0, name: getMessage('site.header.link1'), target: '_blank' },
                  { id: 1, name: 'HANASYS ORDER', link: `${qOrderUrl}?autoLoginParam1=${encodeURIComponent(res.data)}`, target: '_blank' },
                  { id: 2, name: 'HANASYS Musubi', link: `${qMusubiUrl}?autoLoginParam1=${encodeURIComponent(res.data)}`, target: '_blank' },
                  { id: 3, name: getMessage('site.header.link2'), link: `https://q-warranty.q-cells.jp/seller_login`, target: '_blank' },
                ]
              : userSession.groupId === '60000'
                ? [
                    { id: 0, name: getMessage('site.header.link1'), target: '_blank' },
                    { id: 1, name: 'HANASYS ORDER', link: `${qOrderUrl}?autoLoginParam1=${encodeURIComponent(res.data)}`, target: '_blank' },
                    { id: 2, name: getMessage('site.header.link2'), link: `https://q-warranty.q-cells.jp/seller_login`, target: '_blank' },
                  ]
                : [
                    { id: 0, name: getMessage('site.header.link1'), target: '_blank' },
                    { id: 1, name: 'HANASYS Musubi', link: `${qMusubiUrl}?autoLoginParam1=${encodeURIComponent(res.data)}`, target: '_blank' },
                    { id: 2, name: getMessage('site.header.link2'), link: `https://q-warranty.q-cells.jp/seller_login`, target: '_blank' },
                  ],
          )
          onChangeSelect({ id: 0, name: getMessage('site.header.link1') })
        }
      })
      .catch((error) => {
        alert(error.response.data.message)
      })
  }

  useEffect(() => {
    getAutoLoginParam()
  }, [userSession])

  const menus = [
    { id: 0, name: 'header.menus.home', url: '/', children: [] },
    {
      id: 1,
      name: 'header.menus.management',
      url: '',
      children: [
        // { id: 3, name: 'header.menus.management.newStuff', url: '/management/stuff/tempdetail', children: [] },
        { id: 3, name: 'header.menus.management.newStuff', url: '/management/stuff/tempReg', children: [] },
        { id: 4, name: 'header.menus.management.stuffList', url: '/management/stuff', children: [] },
      ],
    },
    {
      id: 2,
      name: 'header.menus.community',
      url: '',
      children: [
        { id: 5, name: 'header.menus.community.notice', url: '/community/notice', children: [] },
        { id: 6, name: 'header.menus.community.faq', url: '/community/faq', children: [] },
        { id: 7, name: 'header.menus.community.archive', url: '/community/archive', children: [] },
      ],
    },
  ]

  const syncSession = useCallback(() => {
    setSessionState({ ...userSession })
  })

  useEffect(() => {
    syncSession()
  }, [userSession])

  const onChangeSelect = (option) => {
    setSelected(option)
  }

  const navPage = () => {
    if (selected.link) {
      if (selected.target === '_blank') {
        window.open(selected.link)
      } else {
        location.href = selected.link
      }
    }
  }

  // 물건메뉴 이외 클릭시 물건 리코일 비우기
  const removeStuffRecoil = (menu) => {
    //menu.id 0 HOME
    //menu.id 3 신규물건등록
    //menu.id 4 물건리스트
    //menu.id 5 공지사항
    //menu.id 6 FAQ
    //menu.id 7 자료실
    if (menu.id === 0 || menu.id === 5 || menu.id === 6 || menu.id === 7) {
      setStuffSearch({
        ...stuffSearch,
        code: 'DELETE',
      })
    }
  }

  const getMenuTemplate = (menus) => {
    return menus.map((menu) => {
      return (
        <li
          key={`${menu.id}`}
          className={'nav-item'}
          onMouseEnter={(e) => ToggleonMouse(e, 'add', 'nav > ul')}
          onMouseLeave={(e) => ToggleonMouse(e, 'remove', 'nav > ul')}
        >
          {menu.children.length === 0 ? (
            <Link
              key={`${menu.id}`}
              href={menu.url}
              replace={true}
              onClick={() => {
                // moveHome()
                removeStuffRecoil(menu)
                if (pathName === '/') {
                  window.location.reload()
                }
              }}
            >
              {getMessage(menu.name)}
            </Link>
          ) : (
            <Fragment key={`${menu.id}`}>
              <button>{getMessage(menu.name)}</button>
              <ul className="nav-depth2">
                {menu.children.map((m) => {
                  return (
                    <li
                      key={`${m.id}`}
                      className={'nav-depth2-item'}
                      onMouseEnter={(e) => ToggleonMouse(e, 'add', 'li > ul')}
                      onMouseLeave={(e) => ToggleonMouse(e, 'remove', 'li > ul')}
                    >
                      <Link
                        scroll={false}
                        href={m.url}
                        replace={true}
                        onClick={() => {
                          removeStuffRecoil(m)
                        }}
                      >
                        {getMessage(m.name)}
                      </Link>
                    </li>
                  )
                })}
              </ul>
            </Fragment>
          )}
        </li>
      )
    })
  }

  return (
    <>
      {!(pathName.includes('login') || pathName.includes('join') || sessionState.pwdInitYn === 'N') && (
        <header className={isDimmed}>
          <div className="header-inner">
            <div className="header-right">
              <h1 className="logo">
                <Link
                  href={'/'}
                  onClick={() => {
                    setStuffSearch({
                      ...stuffSearch,
                      code: 'DELETE',
                    })
                    if (pathName === '/') {
                      window.location.reload()
                    }
                  }}
                ></Link>
              </h1>
              <nav>
                <ul className="nav-list ">{getMenuTemplate(menus)}</ul>
              </nav>
            </div>
            <div className="header-left">
              <div className="profile-box">
                <Link
                  href="#"
                  onClick={() => {
                    setUserInfoModal(true)
                    closeAll()
                  }}
                >
                  <button className="profile">{userSession.userNm}</button>
                </Link>
                {userInfoModal && <UserInfoModal userId={sessionState.userId} userInfoModal={userInfoModal} setUserInfoModal={setUserInfoModal} />}
              </div>
              <div className="sign-out-box">
                <button
                  className="sign-out"
                  onClick={() => {
                    setStuffSearch({
                      ...stuffSearch,
                      code: 'DELETE',
                    })
                    logout()
                    router.replace('/login', undefined, { shallow: true })
                  }}
                >
                  {getMessage('header.logout')}
                </button>
              </div>
              <div className="select-box">
                <QSelectBox options={SelectOptions} onChange={onChangeSelect} title={getMessage('site.header.link1')} />
              </div>
              <div className="btn-wrap">
                <button className="btn-frame small dark" onClick={() => navPage()}>
                  {getMessage('header.go')}
                </button>
              </div>
            </div>
          </div>
        </header>
      )}
    </>
  )
}
