import { Checkbox, NumberInput, TextInput } from "@mantine/core";
import { DatePickerInput, DateTimePicker } from "@mantine/dates";
import { UseFormReturnType } from "@mantine/form";
import DefaultAttrOptionSelect from "@/app/module-editor/[moduleId]/components/shared/DefaultAttrOptionSelect/DefaultAttrOptionSelect";
import { AttributeSchema } from "@/lib/client";
import { naiveDateTimeStringToUtc } from "@/utils/datetime";

type Props = {
    form: UseFormReturnType<Partial<AttributeSchema>>;
};

const DefaultAttributeValueInput = ({ form }: Props) => {
    const type = form.values.type?.type;
    const label = "Значение по умолчанию";
    const inputName = "defaultValue";

    const value = form.getValues().defaultValue;

    if (type === "bool") {
        return (
            <Checkbox
                label={label}
                {...form.getInputProps(inputName, { type: "checkbox" })}
                checked={value as boolean}
                onChange={e =>
                    form.setFieldValue("defaultValue", e.currentTarget.checked)
                }
            />
        );
    } else if (type === "date") {
        return (
            <DatePickerInput
                label={label}
                {...form.getInputProps(inputName)}
                value={
                    form.values.defaultValue
                        ? new Date(String(form.values.defaultValue))
                        : null
                }
                onChange={value => form.setFieldValue("defaultValue", value)}
                clearable
                locale={"ru"}
                valueFormat={"DD.MM.YYYY"}
            />
        );
    } else if (type === "datetime") {
        return (
            <DateTimePicker
                label={label}
                {...form.getInputProps(inputName)}
                value={
                    form.values.defaultValue
                        ? naiveDateTimeStringToUtc(
                              form.values.defaultValue as string
                          )
                        : null
                }
                onChange={val => {
                    if (!val) return;
                    const localDate = new Date(val.replace(" ", "T"));
                    const utcString = localDate
                        .toISOString()
                        .substring(0, 19)
                        .replace("T", " ");
                    form.setFieldValue("defaultValue", utcString);
                }}
                clearable
                locale={"ru"}
                valueFormat={"DD.MM.YYYY HH:mm"}
            />
        );
    } else if (type === "str") {
        return (
            <TextInput
                label={label}
                {...form.getInputProps(inputName)}
            />
        );
    } else if (type === "int" || type === "float") {
        return (
            <NumberInput
                allowDecimal={type === "float"}
                label={label}
                {...form.getInputProps(inputName)}
                value={Number(form.values.defaultValue)}
                onChange={value =>
                    form.setFieldValue("defaultValue", Number(value))
                }
            />
        );
    } else if (type === "select") {
        if (!form.values.select?.id) return <></>;
        return (
            <DefaultAttrOptionSelect
                label={"Значение по умолчанию"}
                {...form.getInputProps("defaultOption")}
                value={form.values.defaultOption}
                onChange={value => {
                    form.setFieldValue("defaultOption", value);
                    form.setFieldValue("defaultOptionId", value?.id);
                }}
                selectId={form.values.select?.id}
            />
        );
    }
    return <></>;
};

export default DefaultAttributeValueInput;
