import React, { useEffect, useState } from 'react';
import {
    TextInput,
    TouchableOpacity,
    StyleSheet,
} from 'react-native';
import Modal from "react-native-modal";
import { useTranslation } from 'react-i18next';

import { Colors } from '@/src/constants/Colors';
import { ThemedView } from '@/src/components/themed/ThemedView';
import { ThemedText } from '@/src/components/themed/ThemedText';
import { useColorScheme } from '@/src/hooks/useColorScheme';
import ThemedInput from '@/src/components/themed/ThemedInput';

interface Field {
    name: string;
    title?: string;
    placeholder: string;
    defaultValue?: string;
}

interface EditModalProps {
    title?: string;
    visible: boolean;
    onClose?: () => void;
    onSave: (inputValues: Record<string, string>) => void;
    fields: Field[];
}

const CustomModal: React.FC<EditModalProps> = ({ title, visible, onClose, onSave, fields }) => {
    const colorScheme = useColorScheme() ?? 'light';
    const [ inputValues, setInputValues ] = useState<Record<string, string>>( {} );
    const { t } = useTranslation();

    useEffect( () => {
        const initialValues: Record<string, string> = {};
        fields.forEach( field => {
            if (field.defaultValue) {
                initialValues[ field.name ] = field.defaultValue;
            }
        } );
        setInputValues( initialValues );
    }, [ fields ] );

    const handleInputChange = (name: string, value: string) => {
        setInputValues( { ...inputValues, [ name ]: value } );
    };

    const handleSave = () => {
        onSave( inputValues );
        if (onClose) {
            onClose();
        }
    };

    return (
        <Modal isVisible={visible} useNativeDriverForBackdrop={true}>
            <ThemedView style={{ ...styles.view, backgroundColor: Colors[ colorScheme ].background }}>
                <ThemedText style={styles.text}>{title ? title : t( "modal.default.title" )}</ThemedText>

                {fields.map( (field, index) => (
                    <ThemedInput
                        key={index}
                        label={field.title}
                        onChangeText={(text) => handleInputChange( field.name, text )}
                        value={inputValues[ field.name ] || ''}
                        placeholder={field.placeholder}
                    />
                ) )}

                <ThemedView style={styles.buttonContainer}>
                    <TouchableOpacity onPress={onClose || ( () => {
                    } )}>
                        <ThemedText style={[ styles.textStyle, styles.buttonClose ]}>{t( "modal.default.cancel" )}</ThemedText>
                    </TouchableOpacity>

                    <TouchableOpacity style={styles.buttonSave} onPress={handleSave}>
                        <ThemedText style={styles.textStyle}>{t( "modal.default.save" )}</ThemedText>
                    </TouchableOpacity>
                </ThemedView>
            </ThemedView>
        </Modal>
    );
};

const styles = StyleSheet.create( {
    view: {
        margin: 25,
        borderRadius: 10,
        paddingTop: 30,
        paddingBottom: 30,
        paddingLeft: 35,
        paddingRight: 35,
        alignItems: 'center',
        shadowColor: '#000',
        shadowOffset: {
            width: 0,
            height: 2,
        },
        shadowOpacity: 0.25,
        shadowRadius: 4,
        elevation: 5,
    },
    text: {
        marginBottom: 15,
        textAlign: 'center',
    },
    inputContainer: {
        width: '100%',
        marginBottom: 15,
    },
    inputTitle: {
        fontSize: 16,
        marginBottom: 5,
    },
    input: {
        height: 40,
        borderColor: 'gray',
        borderWidth: 1,
        width: '100%',
        paddingHorizontal: 10,
        borderRadius: 5,
    },
    buttonContainer: {
        marginTop: 20,
        flexDirection: 'row',
        justifyContent: 'space-between',
        width: '100%',
    },
    buttonClose: {
        color: Colors.red,
        paddingTop: 8,
    },
    buttonSave: {
        backgroundColor: Colors.tint,
        borderRadius: 5,
        paddingTop: 10,
        paddingBottom: 10,
        paddingLeft: 40,
        paddingRight: 40,
    },
    textStyle: {
        color: 'white',
        fontWeight: 'bold',
        textAlign: 'center',
    },
} );

export default CustomModal;