import React from 'react';
import { Dumbbell, Scale, Activity, Timer as TimerIcon, ArrowRight, ArrowUp, Plus, CheckCircle } from 'lucide-react';
import { ExerciseType, Language } from '../../types';
import { t } from '../../services/i18n';
import FilledInput from '../FilledInput';
import ExerciseModal from '../ExerciseModal';
import { useTracker } from './useTracker';

interface SporadicViewProps {
    tracker: ReturnType<typeof useTracker>;
    lang: Language;
}

const SporadicView: React.FC<SporadicViewProps> = ({ tracker, lang }) => {
    const {
        searchQuery,
        setSearchQuery,
        setShowSuggestions,
        showSuggestions,
        filteredExercises,
        setSelectedExercise,
        selectedExercise,
        weight,
        setWeight,
        reps,
        setReps,
        duration,
        setDuration,
        distance,
        setDistance,
        height,
        setHeight,
        handleLogSporadicSet,
        sporadicSuccess,
        setIsSporadicMode,
        isCreating,
        setIsCreating,
        handleCreateExercise,
        exercises,
        resetForm
    } = tracker;

    return (
        <div className="flex flex-col h-full max-h-full overflow-hidden relative bg-surface">
            <div className="px-4 py-3 bg-surface-container shadow-elevation-1 z-20 flex justify-between items-center">
                <button
                    onClick={() => {
                        resetForm();
                        setIsSporadicMode(false);
                    }}
                    className="text-error font-medium text-sm hover:opacity-80 transition-opacity"
                >
                    {t('cancel', lang)}
                </button>
                <div className="flex flex-col items-center">
                    <h2 className="text-title-medium text-on-surface flex items-center gap-2 font-medium">
                        <span className="w-2 h-2 rounded-full bg-primary animate-pulse" />
                        {t('quick_log', lang)}
                    </h2>
                </div>
                <button
                    onClick={handleLogSporadicSet}
                    className={`px-5 py-2 rounded-full text-sm font-medium transition-all ${selectedExercise
                        ? 'bg-primary-container text-on-primary-container hover:opacity-90 shadow-elevation-1'
                        : 'bg-surface-container-high text-on-surface-variant opacity-50 cursor-not-allowed'
                        }`}
                    disabled={!selectedExercise}
                >
                    {t('log_set', lang)}
                </button>
            </div>

            <div className="flex-1 overflow-y-auto p-4 pb-32 space-y-6">
                {/* Exercise Selection */}
                <div className="relative">
                    <FilledInput
                        label={t('select_exercise', lang)}
                        value={searchQuery}
                        onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
                            setSearchQuery(e.target.value);
                            setShowSuggestions(true);
                        }}
                        onFocus={() => setShowSuggestions(true)}
                        onBlur={() => setTimeout(() => setShowSuggestions(false), 100)}
                        icon={<Dumbbell size={10} />}
                        autoComplete="off"
                        type="text"
                    />
                    <button
                        onClick={() => setIsCreating(true)}
                        className="absolute right-2 top-1/2 -translate-y-1/2 p-2 text-primary hover:bg-primary-container/20 rounded-full z-10"
                    >
                        <Plus size={24} />
                    </button>
                    {showSuggestions && (
                        <div className="absolute top-full left-0 w-full bg-surface-container rounded-xl shadow-elevation-3 overflow-hidden z-20 mt-1 max-h-60 overflow-y-auto animate-in fade-in slide-in-from-top-2">
                            {filteredExercises.length > 0 ? (
                                filteredExercises.map(ex => (
                                    <button
                                        key={ex.id}
                                        onMouseDown={(e) => {
                                            e.preventDefault(); // Prevent input blur
                                            setSelectedExercise(ex);
                                            setSearchQuery(ex.name);
                                            setShowSuggestions(false);
                                        }}
                                        className="w-full text-left px-4 py-3 text-on-surface hover:bg-surface-container-high transition-colors text-lg"
                                    >
                                        {ex.name}
                                    </button>
                                ))
                            ) : (
                                <div className="px-4 py-3 text-on-surface-variant text-lg">{t('no_exercises_found', lang)}</div>
                            )}
                        </div>
                    )}
                </div>

                {selectedExercise && (
                    <div className="animate-in fade-in slide-in-from-bottom-4 duration-300 space-y-6">
                        <div className="grid grid-cols-2 gap-4">
                            {(selectedExercise.type === ExerciseType.STRENGTH || selectedExercise.type === ExerciseType.BODYWEIGHT || selectedExercise.type === ExerciseType.STATIC) && (
                                <FilledInput
                                    label={selectedExercise.type === ExerciseType.BODYWEIGHT ? t('add_weight', lang) : t('weight_kg', lang)}
                                    value={weight}
                                    step="0.1"
                                    onChange={(e: any) => setWeight(e.target.value)}
                                    icon={<Scale size={10} />}
                                />
                            )}
                            {(selectedExercise.type === ExerciseType.STRENGTH || selectedExercise.type === ExerciseType.BODYWEIGHT || selectedExercise.type === ExerciseType.PLYOMETRIC) && (
                                <FilledInput
                                    label={t('reps', lang)}
                                    value={reps}
                                    onChange={(e: any) => setReps(e.target.value)}
                                    icon={<Activity size={10} />}
                                    type="number"
                                />
                            )}
                            {(selectedExercise.type === ExerciseType.CARDIO || selectedExercise.type === ExerciseType.STATIC) && (
                                <FilledInput
                                    label={t('time_sec', lang)}
                                    value={duration}
                                    onChange={(e: any) => setDuration(e.target.value)}
                                    icon={<TimerIcon size={10} />}
                                />
                            )}
                            {(selectedExercise.type === ExerciseType.CARDIO || selectedExercise.type === ExerciseType.LONG_JUMP) && (
                                <FilledInput
                                    label={t('dist_m', lang)}
                                    value={distance}
                                    onChange={(e: any) => setDistance(e.target.value)}
                                    icon={<ArrowRight size={10} />}
                                />
                            )}
                            {(selectedExercise.type === ExerciseType.HIGH_JUMP) && (
                                <FilledInput
                                    label={t('height_cm', lang)}
                                    value={height}
                                    onChange={(e: any) => setHeight(e.target.value)}
                                    icon={<ArrowUp size={10} />}
                                />
                            )}
                        </div>

                        <button
                            onClick={handleLogSporadicSet}
                            className={`w-full h-14 font-medium text-lg rounded-full shadow-elevation-2 hover:shadow-elevation-3 active:scale-[0.98] transition-all flex items-center justify-center gap-2 ${sporadicSuccess
                                ? 'bg-green-500 text-white'
                                : 'bg-primary-container text-on-primary-container'
                                }`}
                        >
                            {sporadicSuccess ? <CheckCircle size={24} /> : <Plus size={24} />}
                            <span>{sporadicSuccess ? t('saved', lang) : t('log_set', lang)}</span>
                        </button>
                    </div>
                )}
            </div>

            {isCreating && (
                <ExerciseModal
                    isOpen={isCreating}
                    onClose={() => setIsCreating(false)}
                    onSave={handleCreateExercise}
                    lang={lang}
                    existingExercises={exercises}
                />
            )}
        </div>
    );
};

export default SporadicView;
