import { router, useLocalSearchParams } from "expo-router";
import { useState } from "react";
import { SafeAreaView, StyleSheet, Text, TextInput, View } from "react-native";
import { AutoDecimalInput, CustomColorPicker, NavigationButton, TypeSelectorSwitch } from "../../../components";
import { addCategory } from "../../../services/database";
import { useTheme } from "../../contexts/ThemeContext";

export default function Page() {
    const {colors} = useTheme();

    const parameters = useLocalSearchParams();

    const [categoryName, setCategoryName] = useState<string>("Enter Category Name...");
    const [categoryColor, setCategoryColor] = useState<string>('#' + Math.floor(Math.random()*16777215).toString(16));
    const [selectedType, setSelectedType] = useState<string>("expense");
    const [amount, setAmount] = useState(0);

    return (
        <SafeAreaView style={[styles.safeAreaViewStyle, {backgroundColor: colors.backgroundColor}]}>
            <Text style={[styles.headingTextStyle, {color: colors.primaryText}]}>Add Category</Text>
            
            <View style={[styles.containerStyle, {backgroundColor: colors.containerColor}]}>
                <View style={[styles.textInputViewStyle, {backgroundColor: colors.elementDefaultColor}]}>
                    <TextInput placeholder={categoryName} placeholderTextColor={colors.secondaryText} style={[styles.textInputStyle, {color: colors.primaryText}]} onChangeText={(newName: string) => {
                        setCategoryName(newName);
                    }}/>
                </View>

                <View style={styles.budgetInput}>
                    <AutoDecimalInput label={"Allocated:"} onValueChange={(value) => {
                        setAmount(!Number.isNaN(Number.parseFloat(value)) ? Number.parseFloat(value) : 0);
                    }}/>  
                </View>

                <TypeSelectorSwitch 
                currentSelected={selectedType}
                handleButtonPress={(type) => {
                    setSelectedType(type);
                }}
                />

                <View>
                   <CustomColorPicker color={categoryColor} handleColorChange={(color) => {
                        setCategoryColor(color);
                    }}/> 
                </View>

                <View style={styles.navigationButtonViewStyle}>
                    <NavigationButton text="Back" onPress={() => {
                        router.back();
                    }}/>
                    <NavigationButton text="Save" onPress={() => {
                        addCategory(categoryName, categoryColor, selectedType, amount);
                        router.back();
                    }}/> 
                </View>
            </View>
        </SafeAreaView>
    );
}

const styles = StyleSheet.create({
    containerStyle: {
        flex: 1,
        margin: 10,
        borderRadius: 10,
    },
    safeAreaViewStyle: {
        flex: 1,
        flexDirection: "column" 
    },
    headingTextStyle: {
        fontSize: 40,
        fontWeight: "bold",
        alignSelf: "center",
        marginVertical: 10,
    },
    navigationButtonViewStyle: {
        flexDirection: "row",
        justifyContent: "center",
        marginBottom: 10,
    },
    textInputViewStyle: {
        borderRadius: 10,
        paddingVertical: 10,
        margin: 10,
    },
    textInputStyle: {
        paddingHorizontal: 10,
        fontSize: 25,
    },
    budgetInput: {
        marginBottom: 10,
        marginHorizontal: 10,
    }
});