import React, { useEffect, useRef, useState } from 'react'
import { connect, useDispatch } from 'react-redux'
import { Card, Select } from 'antd'
import commonStyles from '../../../Utils/Common.less'
import CommonButton from '../../../Components/Commonbutton'
import CoporateList from './OnboardingCoporate/OnboardingList'
import IndividualList from './OnBoardingIndividual/OnBoardingIndividualList'
import SoleTraderList from './OnBoardingSoletrader/OnboardingSoltraderList'
import PartnerList from './OnBoardingPatrnership/OnBoardingPartnerList'

import { GetCategoryDropDownList } from '../../../Services/GroupAndPartners'
import { GetServiceDropDownList } from '../../../Services/OnbordingCoporate'

import type { RegisterClientInterface } from '../../../Types/Client/OnboardingClients/OnbordingClient'

const OnboardingClient = (
    props: RegisterClientInterface
): React.ReactElement => {
    const dispatch = useDispatch()
    const { categoryDropDownList } = props
    const [activeButton, setActiveButton] = useState(1)
    const [showCoporateList, setShowCoporateList] = useState(true)
    const [showpartnerList, setShowpartner] = useState(true)
    const [showSoleTraderList, setShowSoleTrader] = useState(true)
    const [showIndividualList, setShowIndividualList] = useState(true)
    const [selectedId, setSelectedId] = useState(0)
    // const [selectedServiceId, setSelectedServiceId] = useState(0)
    const hasFetchedOnce1 = useRef(false)
    useEffect(() => {
        setShowSoleTrader(true)
        const buttonId = localStorage.getItem('activeButtonON')
        console.log('buttonId', buttonId)
        if (buttonId !== null && buttonId !== undefined) {
            handleButtonClick(Number(buttonId))
            setActiveButton(Number(buttonId))
        }
    }, [])

    useEffect(() => {
        if (!hasFetchedOnce1.current) {
            hasFetchedOnce1.current = true
            return
        }
        GetCategoryDropDownList(dispatch)
        GetServiceDropDownList(dispatch)
    }, [dispatch])

    const handleSelectChange = (value: string | number): void => {
        setSelectedId(value === 'all' ? 0 : Number(value))
    }

    // const handleSelectChange1 = (value: string | number): void => {
    //     setSelectedServiceId(value === 'all' ? 0 : Number(value))
    // }

    const handleButtonClick = (buttonName: number): void => {
        localStorage.setItem('activeButtonON', buttonName.toString())
        setActiveButton(buttonName)

        if (buttonName === 1) {
            setShowCoporateList(true)
            setShowSoleTrader(false)
        } else if (buttonName === 2) {
            setShowSoleTrader(true)
            setShowCoporateList(false)
        } else if (buttonName === 3) {
            setShowIndividualList(true)
            setShowCoporateList(false)
            setShowSoleTrader(false)
        } else if (buttonName === 4) {
            setShowpartner(true)
            setShowIndividualList(false)
            setShowCoporateList(false)
            setShowSoleTrader(false)
        }
    }

    const handleOtherFunction = (): React.ReactElement => {
        if (activeButton === 1) {
            return (
                <CoporateList
                    selectedCategoryId={selectedId}
                    selectedServiceId={0}
                />
            )
        }
        if (activeButton === 3) {
            return (
                <IndividualList
                    selectedCategoryId={selectedId}
                    selectedServiceId={0}
                />
            )
        }
        if (activeButton === 4) {
            return (
                <PartnerList
                    selectedCategoryId={selectedId}
                    selectedServiceId={0}
                />
            )
        }
        return <></>
    }

    return (
        <>
            <div>
                <h2>Onboarding Clients</h2>
            </div>
            <Card title="Onboarding Clients" className={commonStyles.card}>
                <Select
                    className={commonStyles.topdropdown2}
                    placeholder="Please select"
                    onChange={handleSelectChange}
                    value={selectedId === 0 ? 'all' : selectedId}
                    options={[
                        { value: 'all', label: 'All' },
                        ...categoryDropDownList,
                    ]}
                />
                {/* <Select
                    className={commonStyles.topdropdown}
                    placeholder="Please select"
                    onChange={handleSelectChange1}
                    value={selectedServiceId === 0 ? 'all' : selectedServiceId}
                    options={[
                        { value: 'all', label: 'All' },
                        ...serviceDropDownList,
                    ]}
                /> */}
                <CommonButton
                    name="Corporate"
                    isActive={activeButton === 1}
                    clickAction={() => handleOtherFunction()}
                    additionalOnClick={() => {
                        handleButtonClick(1)
                    }}
                />
                <CommonButton
                    name="Sole Trader"
                    isActive={activeButton === 2}
                    clickAction={() => handleOtherFunction()}
                    additionalOnClick={() => {
                        handleButtonClick(2)
                    }}
                />
                <CommonButton
                    name="Individual"
                    isActive={activeButton === 3}
                    clickAction={() => handleOtherFunction()}
                    additionalOnClick={() => {
                        handleButtonClick(3)
                    }}
                />
                <CommonButton
                    name="Partnership"
                    isActive={activeButton === 4}
                    clickAction={() => handleOtherFunction()}
                    additionalOnClick={() => {
                        handleButtonClick(4)
                    }}
                />
                {showCoporateList && activeButton === 1 && (
                    <CoporateList
                        selectedCategoryId={selectedId}
                        selectedServiceId={0}
                    />
                )}
                {showIndividualList && activeButton === 3 && (
                    <IndividualList
                        selectedCategoryId={selectedId}
                        selectedServiceId={0}
                    />
                )}
                {showSoleTraderList && activeButton === 2 && (
                    <SoleTraderList
                        selectedCategoryId={selectedId}
                        selectedServiceId={0}
                    />
                )}
                {showpartnerList && activeButton === 4 && (
                    <PartnerList
                        selectedCategoryId={selectedId}
                        selectedServiceId={0}
                    />
                )}
            </Card>
        </>
    )
}

const mapStateToProps = (state: any): RegisterClientInterface => {
    return {
        parentCategoryList: state.initial.parentCategoryList,
        categoryDropDownList: state.initial.categoryDropDownList,
        serviceDropDownList: state.initial.serviceDropDownList,
    }
}

export default connect(mapStateToProps)(OnboardingClient)
