import { ChangeEvent, useState } from 'react';
import { PATHS } from '@/shared/constants/paths';
import { useLocation } from 'react-router';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { IMAGE_ROOT } from '@/shared/constants/common';
import { HeaderType } from '@/entities/common/model/types';
import { useExtensionKeyinApplyMutation } from '@/entities/additional-service/api/use-extension-keyin-apply-mutation';
import {
    useSetHeaderTitle,
    useSetHeaderType,
    useSetFooterMode,
    useSetOnBack
} from '@/widgets/sub-layout/use-sub-layout';
import { overlay } from 'overlay-kit';
import { Dialog } from '@/shared/ui/dialogs/dialog';
import { useStore } from '@/shared/model/store';
import { snackBar } from '@/shared/lib';
import { NumericFormat, PatternFormat } from 'react-number-format';

export const KeyInPaymentRequestPage = () => {
    const { navigate } = useNavigate();
    const location = useLocation();

    const userMid = useStore.getState().UserStore.mid;
    const midOptions = useStore.getState().UserStore.selectOptionsMids

    const [mid, setMid] = useState<string>(userMid || '');
    const [productName, setProductName] = useState<string>('');
    const [amount, setAmount] = useState<number>(0);
    const [customerName, setCustomerName] = useState<string>('');
    const [email, setEmail] = useState<string>('');
    const [phoneNumber, setPhoneNumber] = useState<string>('');
    const [cardNo1, setCardNo1] = useState<string>('');
    const [cardNo2, setCardNo2] = useState<string>('');
    const [cardNo3, setCardNo3] = useState<string>('');
    const [cardNo4, setCardNo4] = useState<string>('');
    const [expMon, setExpMon] = useState<string>('');
    const [expYear, setExpYear] = useState<string>('');
    const [instmnt, setInstmnt] = useState<string>('00');
    const [orderNumber, setOrderNumber] = useState<string>('');

    const { mutateAsync: keyInApply } = useExtensionKeyinApplyMutation();

    useSetHeaderTitle('KEY-IN 결제');
    useSetHeaderType(HeaderType.LeftArrow);
    useSetFooterMode(false);
    useSetOnBack(() => {
        navigate(PATHS.additionalService.keyInPayment.list);
    });

    const resetForm = () => {
        setProductName('');
        setAmount(0);
        setCustomerName('');
        setEmail('');
        setPhoneNumber('');
        setCardNo1('');
        setCardNo2('');
        setCardNo3('');
        setCardNo4('');
        setExpMon('');
        setExpYear('');
        setInstmnt('00');
        setOrderNumber('');
    };

    const callKeyInPaymentRequest = () => {
        const cardNo = `${cardNo1}${cardNo2}${cardNo3}${cardNo4}`;
        const cardExpire = `${expMon}${expYear}`
        let keyInApplyParams = {
            mid: mid,
            cardNo: cardNo,
            cardExpire: cardExpire,
            instmnt: instmnt,
            amount: amount,
            productName: productName,
            orderNumber: orderNumber,
            customerName: customerName,
            phoneNumber: phoneNumber,
            email: email,
        };

        keyInApply(keyInApplyParams).then((rs) => {
            if (rs.data?.success) {
                // 성공: 화면 유지 & 입력 내용 초기화
                snackBar("KEY-IN 결제 신청을 성공하였습니다.")
                resetForm();
            } else {
                // 실패: 화면 유지 & 입력 내용 유지
                const errorMessage = rs.data?.resultMessage || rs.error?.resultMessage || '결제 신청에 실패했습니다.';
                // HTML 태그 제거
                const cleanMessage = errorMessage.replace(/<br\s*\/?>/gi, ' ').trim();
                snackBar(`[실패] ${cleanMessage}`);
            }
        }).catch((error) => {
            console.error('결제 실패:', error);
            const errorMessage = error?.response?.data?.error?.resultMessage || error?.message || '결제 요청 중 오류가 발생했습니다';
            snackBar(`[실패] ${errorMessage}`);
        });
    };


    const isValidPhoneNumber = (phone: string) => {
        const phoneRegex = /^01[0|1|6|7|8|9][0-9]{7,8}$/;
        return phoneRegex.test(phone);
    };

    const isValidEmail = (email: string) => {
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return emailRegex.test(email);
    };

    const isValidCardNumber = () => {
        return cardNo1.length === 4 && cardNo2.length === 4 &&
            cardNo3.length === 4 && cardNo4.length === 4;
    };

    const isFormValid = () => {
        return (
            mid.trim() !== '' &&
            productName.trim() !== '' &&
            amount > 0 &&
            customerName.trim() !== '' &&
            expMon.trim() !== '' &&
            expYear.trim() !== '' &&
            orderNumber.trim() !== '' &&
            isValidEmail(email) &&
            isValidPhoneNumber(phoneNumber) &&
            isValidCardNumber()
        );
    };

    const onClickToRequest = () => {
        callKeyInPaymentRequest();
    };

    return (
        <>
            <main>
                <div className="tab-content">
                    <div className="tab-pane sub active">
                        <div className="option-list">
                            <div className="billing-form gap-16">
                                <div className="billing-row">
                                    <div className="billing-label">가맹점 <span>*</span></div>
                                    <div className="billing-field">
                                        <select
                                            value={mid}
                                            onChange={(e: ChangeEvent<HTMLSelectElement>) => setMid(e.target.value)}
                                        >
                                            {
                                                midOptions.map((value) => (
                                                    <option
                                                        key={value.value}
                                                        value={value.value}
                                                    >{value.name}</option>
                                                ))
                                            }
                                        </select>
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">상품명 <span>*</span></div>
                                    <div className="billing-field">
                                        <input
                                            type="text"
                                            value={productName}
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => setProductName(e.target.value)}
                                        />
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">상품가격 <span>*</span></div>
                                    <div className="billing-field">
                                        <NumericFormat
                                            value={amount}
                                            thousandSeparator={true}
                                            allowNegative={false}
                                            displayType="input"
                                            onValueChange={(values) => {
                                                const { floatValue} = values;
                                                setAmount( floatValue ?? 0);
                                            }}
                                        ></NumericFormat>
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">구매자명 <span>*</span></div>
                                    <div className="billing-field">
                                        <input
                                            type="text"
                                            value={customerName}
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => setCustomerName(e.target.value)}
                                        />
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">구매자 이메일 <span>*</span></div>
                                    <div className="billing-field">
                                        <input
                                            type="email"
                                            value={email}
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => setEmail(e.target.value)}
                                            className={email && !isValidEmail(email) ? 'error' : ''}
                                        />
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">구매자 전화번호 <span>*</span></div>
                                    <div className="billing-field">
                                        <input
                                            type="tel"
                                            value={phoneNumber}
                                            placeholder='- 제외하고 입력'
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => {
                                                const onlyNumbers = e.target.value.replace(/[^0-9]/g, '');
                                                setPhoneNumber(onlyNumbers);
                                            }}
                                            className={phoneNumber && !isValidPhoneNumber(phoneNumber) ? 'error' : ''}
                                            inputMode="numeric"
                                            pattern="[0-9]*"
                                            maxLength={11}
                                        />
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">카드번호 <span>*</span></div>
                                    <div className="billing-field">
                                        <PatternFormat
                                            value={cardNo1}
                                            allowEmptyFormatting
                                            valueIsNumericString
                                            format="####"
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => setCardNo1(e.target.value)}
                                        >
                                        </PatternFormat>
                                    </div>
                                    <div className="billing-field">
                                        <PatternFormat
                                            value={cardNo2}
                                            allowEmptyFormatting
                                            valueIsNumericString
                                            format="####"
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => setCardNo2(e.target.value)}
                                        >
                                        </PatternFormat>
                                    </div>
                                    <div className="billing-field">
                                        <PatternFormat
                                            value={cardNo3}
                                            allowEmptyFormatting
                                            valueIsNumericString
                                            format="####"
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => setCardNo3(e.target.value)}
                                        >
                                        </PatternFormat>
                                    </div>
                                    <div className="billing-field">
                                        <PatternFormat
                                            value={cardNo4}
                                            allowEmptyFormatting
                                            valueIsNumericString
                                            format="####"
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => setCardNo4(e.target.value)}
                                        >
                                        </PatternFormat>
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">유효기간(월/년)<span>*</span></div>
                                    <div className="billing-field" style={{ display: 'flex', gap: '8px', alignItems: 'center' }}>
                                        <PatternFormat
                                            placeholder='MM'
                                            value={expMon}
                                            valueIsNumericString
                                            format='##'
                                            isAllowed={(values) => {
                                                const { value } = values;
                                                if (!value) return true;
                                                const numValue = parseInt(value);
                                                return numValue >= 1 && numValue <= 12;
                                            }}
                                            onValueChange={(values) => {
                                                const { value } = values;
                                                setExpMon(value);
                                            }}
                                            onBlur={() => {
                                                if (expMon.length === 1 && parseInt(expMon) >= 1 && parseInt(expMon) <= 9) {
                                                    setExpMon(expMon.padStart(2, '0'));
                                                }
                                            }}
                                            style={{ flex: 1 }}
                                            inputMode="numeric"
                                        />
                                        <span>/</span>
                                        <PatternFormat
                                            placeholder='YY'
                                            value={expYear}
                                            valueIsNumericString
                                            format='##'
                                            onValueChange={(values) => {
                                                const { value } = values;
                                                setExpYear(value);
                                            }}
                                            style={{ flex: 1 }}
                                            inputMode="numeric"
                                        />
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">할부 기간<span>*</span></div>
                                    <div className="billing-field">
                                        <select
                                            disabled={amount < 50000}
                                            value={instmnt}
                                            onChange={(e: ChangeEvent<HTMLSelectElement>) => setInstmnt(e.target.value)}
                                        >
                                            <option value="00">일시불</option>
                                            {/* {amount >= 50000 && (
                                                <>
                                                    <option value="02">2개월</option>
                                                    <option value="03">3개월</option>
                                                    <option value="04">4개월</option>
                                                    <option value="05">5개월</option>
                                                    <option value="06">6개월</option>
                                                    <option value="07">7개월</option>
                                                    <option value="08">8개월</option>
                                                    <option value="09">9개월</option>
                                                    <option value="10">10개월</option>
                                                    <option value="11">11개월</option>
                                                    <option value="12">12개월</option>
                                                </>
                                            )} */}
                                        </select>
                                    </div>
                                </div>

                                <div className="billing-row">
                                    <div className="billing-label">주문번호<span>*</span></div>
                                    <div className="billing-field">
                                        <input
                                            type="text"
                                            value={orderNumber}
                                            onChange={(e: ChangeEvent<HTMLInputElement>) => setOrderNumber(e.target.value)}
                                        />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div className="apply-row">
                            <button
                                className="btn-50 btn-blue flex-1"
                                onClick={() => onClickToRequest()}
                                disabled={!isFormValid()}
                            >결제 신청</button>
                        </div>
                    </div>
                </div>
            </main>
        </>
    )
}