import {
    type FC
} from "react";
import {
    TouchableOpacity
} from "react-native";
import type {
    TimeInputCombinedProps
} from "./type";
import stylesheet, {
    useStyles
} from "./stylesheet";
import {
    NCoreUIKitTheme
} from "../../../../core/hooks";
import type {
    UpdateDateRangeType
} from "../../type";
import moment from "moment";
import NumericInput from "../../../numericInput";

const TimeInput: FC<TimeInputCombinedProps> = ({
    isWorkWith24HoursFormat = true,
    selectMultipleObject,
    setDateRangeValue,
    rangeValidation,
    selectObject,
    setDateValue,
    rangeType,
    timeType,
    maxDate,
    minDate,
    variant,
    ...props
}) => {
    const {
        typography,
        radiuses
    } = NCoreUIKitTheme.useContext();

    const {
        container: containerDynamicStyle,
        input: inputDynamicStyle
    } = useStyles({
        typography,
        radiuses
    });

    return <TouchableOpacity
        style={[
            stylesheet.container,
            containerDynamicStyle
        ]}
    >
        <NumericInput
            {...props}
            validation={(text, setText) => {
                let resp = true;

                if(props.validation && !props.validation(text, setText)) {
                    return false;
                }

                const num = Number(text);

                if(timeType === "hours") {
                    if(num < 0) {
                        setText("0");
                        resp = false;
                    }

                    if(isWorkWith24HoursFormat) {
                        if(num > 23) {
                            setText("23");
                            resp = false;
                        }
                    } else {
                        if(num > 11) {
                            setText("11");
                            resp = false;
                        }
                    }
                } else {
                    if(num < 0) {
                        setText("0");
                        resp = false;
                    }

                    if(num > 59) {
                        setText("59");
                        resp = false;
                    }
                }

                if(resp) {
                    if(variant === "range") {
                        const updateTime: UpdateDateRangeType = {
                            rangeType: rangeType as "start" | "end"
                        };

                        updateTime[timeType] = Number(num);

                        const nDRV = setDateRangeValue(updateTime);

                        if(nDRV) {
                            const nDRVMoment = moment(nDRV[rangeType as "start" | "end"]);

                            if(minDate && nDRVMoment.isBefore(minDate)) {
                                setText(moment(minDate).get(timeType).toString());
                                resp = false;
                            }

                            if(maxDate && nDRVMoment.isAfter(maxDate)) {
                                setText(moment(maxDate).get(timeType).toString());
                                resp = false;
                            }
                            console.log("x:", resp);
                            if(rangeValidation && !rangeValidation({
                                rangeType: rangeType as "start" | "end",
                                dateRangeValue: nDRV
                            }, setText)) {
                                resp = false;
                            }
                        }
                    } else {
                        const nDV = setDateValue({
                            [timeType]: Number(text)
                        });

                        if(minDate && nDV && moment(nDV).isBefore(minDate)) {
                            setText(moment(minDate).get(timeType).toString());
                            resp = false;
                        }

                        if(maxDate && nDV && moment(nDV).isAfter(maxDate)) {
                            setText(moment(maxDate).get(timeType).toString());
                            resp = false;
                        }
                    }
                }

                return resp;
            }}
            onChangeText={(text) => {
                if(variant === "range") {
                    const updateTime: UpdateDateRangeType = {
                        rangeType: rangeType as "start" | "end"
                    };

                    updateTime[timeType] = Number(text);

                    const nDRV = setDateRangeValue(updateTime);
                    selectMultipleObject(nDRV);
                } else {
                    const nDV = setDateValue({
                        [timeType]: Number(text)
                    });
                    selectObject(nDV);
                }
            }}
            inputStyle={[
                stylesheet.input,
                inputDynamicStyle
            ]}
            keyboardType="numeric"
            textAlign="center"
            numberOfLines={1}
            maxLength={2}
        />
    </TouchableOpacity>;
};
export default TimeInput;
