import { FC } from 'react'
import LogoImage from '../assets/images/logo.svg'
import { Brush2, Element3, Global, Logout, PenClose, People, Setting3 } from 'iconsax-react'
import SideBarItem from './SideBarItem'
import { useLocation } from 'react-router-dom'
import { useSharedStore } from './store/sharedStore'
import { clx } from '../helpers/utils'
import { useTranslation } from 'react-i18next'
import { Paths } from '@/utils/Paths'
import Storage from './components/Storage'
import { useGetDomains } from '@/pages/setting/domain/hooks/useDomainData'
import { toast } from '@/components/Toast'
import PlanRemaining from './components/PlanRemaining'

const SideBar: FC = () => {

    const { t } = useTranslation()
    const { openSidebar, setOpenSidebar } = useSharedStore()
    const location = useLocation()
    const { data: domains } = useGetDomains()

    const isActive = (path: string) => location.pathname === path
    const isDomainActive = !!domains?.data?.domain?.isVerified
    const iconSizeSideBar = 20

    const handleItemClick = (link: string, isLogout?: boolean) => (e: React.MouseEvent<HTMLAnchorElement>) => {
        if ((!isDomainActive && link !== Paths.settingDomain && !isLogout) && (link !== Paths.home)) {
            e.preventDefault()
            toast('ابتدا باید یک دامنه تایید شده داشته باشید', 'error')
            return
        }
        setOpenSidebar(false)
    }


    return (
        <>
            {
                openSidebar && <div className='fixed top-0 left-0 right-0 bottom-0  bg-black/50 z-20' onClick={() => setOpenSidebar(false)} />
            }
            <div
                className={clx(
                    'fixed xl:flex flex translate-x-[400px] xl:translate-x-0 transition-all ease-in-out opacity-0 invisible xl:visible xl:opacity-100 xl:right-2 xl:md:right-4 right-0 xl:top-2 xl:md:top-4 top-0 xl:bottom-2 xl:md:bottom-4 bottom-0 xl:rounded-[20px] xl:md:rounded-[32px] w-[250px] sm:w-[280px] xl:w-[250px] bg-white flex-col py-8 md:py-10 xl:py-12',
                    openSidebar && 'opacity-100 visible -translate-x-[0px] flex z-40'
                )}
            >
                <div className='flex justify-center px-4'>
                    <img src={LogoImage} className='w-[120px] md:w-[140px]' />
                </div>

                <div className='flex-1 flex flex-col h-full overflow-y-auto no-scrollbar min-h-0'>
                    <div className='mt-[57px] px-8 md:px-12 text-[#C3C7DD] text-sm font-normal'>
                        {t('app.menu')}
                    </div>

                    <div className='text-xs text-[#8C90A3]'>
                        <SideBarItem
                            icon={<Element3 variant={isActive(Paths.home) ? 'Bold' : 'Outline'} color={isActive(Paths.home) ? 'black' : '#8C90A3'} size={iconSizeSideBar} />}
                            title={t('setting.dashboard')}
                            isActive={isActive(Paths.home)}
                            link={Paths.home}
                            onClick={handleItemClick(Paths.home)}
                        />
                        <SideBarItem
                            icon={<Setting3 variant={isActive(Paths.settingMailServer) ? 'Bold' : 'Outline'} color={isActive(Paths.settingMailServer) ? 'black' : '#8C90A3'} size={iconSizeSideBar} />}
                            title={t('setting.mail_server')}
                            isActive={isActive(Paths.settingMailServer)}
                            link={Paths.settingMailServer}
                            onClick={handleItemClick(Paths.settingMailServer)}
                        />

                        <SideBarItem
                            icon={<Global variant={isActive(Paths.settingDomain) ? 'Bold' : 'Outline'} color={isActive(Paths.settingDomain) ? 'black' : '#8C90A3'} size={iconSizeSideBar} />}
                            title={t('setting.domain')}
                            isActive={isActive(Paths.settingDomain)}
                            link={Paths.settingDomain}
                            onClick={handleItemClick(Paths.settingDomain)}
                        />

                        <SideBarItem
                            icon={<People variant={isActive(Paths.settingAddress) ? 'Bold' : 'Outline'} color={isActive(Paths.settingAddress) ? 'black' : '#8C90A3'} size={iconSizeSideBar} />}
                            title={t('setting.address')}
                            isActive={isActive(Paths.settingAddress)}
                            link={Paths.settingAddress}
                            onClick={handleItemClick(Paths.settingAddress)}
                        />

                        <SideBarItem
                            icon={<Brush2 variant={isActive(Paths.settingPersonalityList) ? 'Bold' : 'Outline'} color={isActive(Paths.settingPersonalityList) ? 'black' : '#8C90A3'} size={iconSizeSideBar} />}
                            title={t('setting.personality')}
                            isActive={isActive(Paths.settingPersonalityList)}
                            link={Paths.settingPersonalityList}
                            onClick={handleItemClick(Paths.settingPersonalityList)}
                        />

                        {/* <SideBarItem
                            icon={<Mobile variant={isActive(Paths.emailBuilder) ? 'Bold' : 'Outline'} color={isActive(Paths.emailBuilder) ? 'black' : '#8C90A3'} size={iconSizeSideBar} />}
                            title="سازنده قالب ایمیل"
                            isActive={isActive(Paths.emailBuilder)}
                            link={Paths.emailBuilder}
                        /> */}

                        <SideBarItem
                            icon={<PenClose variant={isActive(Paths.settingSignature) ? 'Bold' : 'Outline'} color={isActive(Paths.settingSignature) ? 'black' : '#8C90A3'} size={iconSizeSideBar} />}
                            title={t('setting.signature')}
                            isActive={isActive(Paths.settingSignature)}
                            link={Paths.settingSignature}
                            onClick={handleItemClick(Paths.settingSignature)}
                        />
                    </div>

                    <div className='flex-1 flex flex-col justify-end mt-10 md:mt-14 pb-6 md:pb-8'>
                        <PlanRemaining />
                        <Storage />
                        <div className='text-xs text-[#8C90A3]'>
                            <SideBarItem
                                icon={<Logout variant={isActive('logout') ? 'Bold' : 'Outline'} color={isActive('logout') ? 'black' : '#8C90A3'} size={iconSizeSideBar} />}
                                title={t('sidebar.logout')}
                                isActive={isActive('logout')}
                                link={`#`}
                                isLogout
                                onClick={handleItemClick('#', true)}
                            />
                        </div>
                    </div>
                </div>
            </div>
        </>
    )
}

export default SideBar