import React, { useEffect, useState } from 'react'
import { Col, Row, Input, Form, Select, Switch, Flex } from 'antd'
import { connect, useDispatch } from 'react-redux'
import commonStyles from '../../../Utils/Common.less'
import { CheckOutlined, CloseSquareOutlined } from '@ant-design/icons'
import { GetCountryList } from '../../../Services/Common'
import type { ListFrontEndInterface } from '../../../Types/CommonType'
import FullWidthModal from '../../../Components/FullWidthModal'
import CusttomBackgroundButton from '../../../Components/CustomBagraoundButton'
import {
    GetCompanyOverViewData,
    SetCompanySailAddress,
} from '../../../Services/CompanyHouse/CompanyOverView'
import { type CsSailAddress } from '../../../Types/CompanyHouse/CompanyOverView'
interface AddressProps {
    countryList: ListFrontEndInterface[]
}
const AddSAILAddress = (
    props: AddressProps & {
        companyName: string
        companyNumber: string
        oncloseModel: () => void
        corporateId: number
        sailAddress: CsSailAddress
        businessId: number
    }
): React.ReactElement => {
    const {
        countryList,
        companyName,
        companyNumber,
        corporateId,
        oncloseModel,
        sailAddress,
        businessId,
    } = props
    const [form] = Form.useForm()
    const dispatch = useDispatch()
    const [swithModalStatus, setSwithModelStatus] = useState(false)
    const [isSubmitToCH, setIsSubmitToCH] = useState<number>(0)
    const [isSubmitToYes, setIsSubmitToCHYes] = useState<boolean>(false)
    useEffect(() => {
        GetCountryList(dispatch)
    }, [])

    useEffect(() => {
        form.setFieldsValue(sailAddress)
    }, [sailAddress])
    const handleSwitchChange = (checked: boolean): void => {
        setSwithModelStatus(!swithModalStatus)
        setIsSubmitToCHYes(checked)
    }

    const ChangeModalFunction = (): void => {
        setSwithModelStatus(!swithModalStatus)
    }

    const changeCompanyHouse = (): void => {
        setIsSubmitToCH(isSubmitToYes ? 1 : 0)
        ChangeModalFunction()
    }

    const handelSaveData = (): void => {
        void handelSetData()
    }
    const handelSetData = async (): Promise<void> => {
        try {
            const fotmData = form.getFieldsValue()
            const contryLable = countryList.find(
                (coun) => coun.value === fotmData.country
            )
            const dataSave = {
                sailAddressId: sailAddress.sailAddressId ?? 0,
                companyId: sailAddress.companyId ?? 0,
                corporateId,
                houseNumber: fotmData.houseNumber,
                poBoxNumber: fotmData.poBoxNumber,
                address1: fotmData.address1,
                address2: fotmData.address2,
                town: fotmData.town,
                region: fotmData.region,
                country: contryLable?.label ?? '',
                postCode: fotmData.postCode,
                isSubmitToCh: isSubmitToCH,
                submittedDate: sailAddress.submittedDate ?? '',
            }

            void SetCompanySailAddress(dispatch, dataSave)

            form.resetFields()
            setTimeout(() => {
                GetCompanyOverViewData(dispatch, businessId, corporateId, 0, 3)
                oncloseModel()
            }, 1000)
        } catch (error) {}
    }

    const customizeRequiredMark = (
        label: React.ReactNode,
        { required }: { required: boolean }
    ): React.ReactElement => (
        <>
            {label}
            &nbsp;
            {required && <span className={commonStyles.requireIcon}>*</span>}
        </>
    )

    return (
        <>
            <div className={commonStyles.formWrapper}>
                <Form
                    name="complex-form"
                    labelCol={{ span: 24 }}
                    wrapperCol={{ span: 24 }}
                    initialValues={{ remember: true }}
                    layout={'vertical'}
                    autoComplete="off"
                    form={form}
                    requiredMark={customizeRequiredMark}
                >
                    <div>
                        <Row gutter={16}>
                            <Col span={8}>Company & Reg Number</Col>
                            <Col span={2}>:</Col>
                            <Col span={14}>
                                {companyName} - {companyNumber}
                            </Col>
                        </Row>
                    </div>
                    <Row gutter={16}>
                        <Col span={12}>
                            <Form.Item
                                label="House Name or Number"
                                name="houseNumber"
                                rules={[
                                    {
                                        required: true,
                                        message:
                                            'Please input House Name or Number!',
                                    },
                                ]}
                            >
                                <Input />
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item
                                label="PostCode"
                                name="postCode"
                                rules={[
                                    {
                                        required: true,
                                        message: 'Please input your PostCode!',
                                    },
                                ]}
                            >
                                <Input />
                            </Form.Item>
                        </Col>
                    </Row>
                    <Row gutter={16}>
                        <Col span={12}>
                            <Form.Item
                                label="Address One"
                                name="address1"
                                rules={[
                                    {
                                        required: true,
                                        message:
                                            'Please input your address one!',
                                    },
                                ]}
                            >
                                <Input />
                            </Form.Item>
                        </Col>

                        <Col span={12}>
                            <Form.Item label="Address Two" name="address2">
                                <Input />
                            </Form.Item>
                        </Col>
                    </Row>
                    <Row gutter={16}>
                        <Col span={12}>
                            <Form.Item
                                label="Town"
                                name="town"
                                rules={[
                                    {
                                        required: true,
                                        message: 'Please input your town!',
                                    },
                                ]}
                            >
                                <Input />
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item label="Country / Region" name="region">
                                <Input />
                            </Form.Item>
                        </Col>
                    </Row>
                    <Row gutter={16}>
                        <Col span={12}>
                            <Form.Item
                                label="Country"
                                name="country"
                                rules={[
                                    {
                                        required: true,
                                        message: 'Please select Country !',
                                    },
                                ]}
                            >
                                <Select
                                    allowClear
                                    showSearch
                                    placeholder="Please select"
                                    onChange={() => {}}
                                    optionFilterProp="children"
                                    filterOption={(input, option) =>
                                        (option?.label ?? '')
                                            .toLowerCase()
                                            .includes(input.toLowerCase())
                                    }
                                    options={countryList}
                                />
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item label="PO Box Number" name="poBoxNumber">
                                <Input />
                            </Form.Item>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={24}>
                            <Switch
                                checked={isSubmitToCH === 1}
                                onChange={handleSwitchChange}
                            />{' '}
                            <span style={{ marginLeft: 5 }}>
                                Select this option, If you are need to apply
                                this changes to companies house.
                            </span>
                        </Col>
                    </Row>
                    <div style={{ marginTop: '20px' }}>
                        <Flex justify="flex-end" gap={5}>
                            <CusttomBackgroundButton
                                color={'#868e96'}
                                name={'Cancel'}
                                icon={<CloseSquareOutlined />}
                                clickAction={oncloseModel}
                            />
                            <CusttomBackgroundButton
                                color={'#1dc9b7'}
                                name={'Submit Changes'}
                                icon={<CheckOutlined />}
                                clickAction={handelSaveData}
                            />
                        </Flex>
                    </div>
                </Form>
            </div>
            <FullWidthModal
                modalStatus={swithModalStatus}
                closeModal={ChangeModalFunction}
                tittle="You are about to submit this changes to Company House. Click 'Yes' to continue."
                smallTittle="Warning: This action cannot be undone!"
                yesButtonFunction={changeCompanyHouse}
            />
        </>
    )
}

const mapStateToProps = (state: any): AddressProps => {
    return {
        countryList: state.common.countryList,
    }
}

export default connect(mapStateToProps)(AddSAILAddress)
