import React from 'react';
import { Dumbbell, User, PlayCircle, Plus, ArrowRight } from 'lucide-react';
import { TopBar } from '../ui/TopBar';
import { Language } from '../../types';
import { t } from '../../services/i18n';
import { useTracker } from './useTracker';

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

const IdleView: React.FC<IdleViewProps> = ({ tracker, lang }) => {
    const {
        userBodyWeight,
        setUserBodyWeight,
        handleStart,
        setIsSporadicMode,
        plans,
        showPlanPrep,
        setShowPlanPrep,
        confirmPlanStart,
        lastWorkoutDate
    } = tracker;

    // Calculate days off
    const getDaysOffContent = () => {
        if (!lastWorkoutDate) {
            return {
                title: t('first_workout_prompt', lang),
                subtitle: null,
                colorClass: 'text-on-surface'
            };
        }

        const now = new Date();
        now.setHours(0, 0, 0, 0);
        const last = new Date(lastWorkoutDate);
        last.setHours(0, 0, 0, 0);

        const diffTime = Math.abs(now.getTime() - last.getTime());
        const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

        if (diffDays === 0) {
            return {
                title: t('last_workout_today', lang),
                subtitle: null,
                colorClass: 'text-on-surface'
            };
        }

        const prefix = t('days_off', lang);

        if (diffDays === 1) {
            return {
                title: `${prefix} ${diffDays}`,
                subtitle: null,
                colorClass: 'text-green-500' // 1 is green
            };
        }

        if (diffDays >= 5) {
            return {
                title: `${prefix} ${diffDays}`,
                subtitle: null,
                colorClass: 'text-red-500' // 5 and more is red
            };
        }

        // Gradient for 2-4
        return {
            title: `${prefix} ${diffDays}`,
            subtitle: null,
            colorClass: 'bg-gradient-to-r from-green-500 to-red-500 bg-clip-text text-transparent'
        };
    };

    const content = getDaysOffContent();

    return (
        <div className="flex flex-col h-full bg-surface">
            <TopBar title="Tracker" icon={Dumbbell} />
            <div className="flex-1 p-4 md:p-8 overflow-y-auto relative">
                <div className="flex-1 flex flex-col items-center justify-center space-y-12">
                    <div className="flex flex-col items-center gap-4">
                        <div className="w-24 h-24 rounded-full bg-surface-container-high flex items-center justify-center text-primary shadow-elevation-1">
                            <Dumbbell size={40} />
                        </div>
                        <div className="text-center">
                            <h1 className={`text-3xl font-normal ${content.colorClass}`}>{content.title}</h1>
                            <p className="text-on-surface-variant text-sm">{content.subtitle}</p>
                        </div>
                    </div>

                    <div className="w-full max-w-sm bg-surface-container rounded-2xl p-6 flex flex-col items-center gap-4 shadow-elevation-1">
                        <label className="text-xs text-on-surface-variant font-bold tracking-wide flex items-center gap-2">
                            <User size={14} />
                            {t('bodyweight', lang)}
                        </label>
                        <div className="flex items-center gap-4">
                            <input
                                type="number"
                                step="0.1"
                                className="text-5xl font-normal text-on-surface tabular-nums bg-transparent text-center w-full focus:outline-none"
                                value={userBodyWeight}
                                onChange={(e) => setUserBodyWeight(e.target.value)}
                            />
                        </div>
                        <p className="text-[10px] text-on-surface-variant">{t('change_in_profile', lang)}</p>
                    </div>

                    <div className="w-full max-w-xs space-y-3">
                        <button
                            onClick={() => handleStart()}
                            className="w-full h-16 rounded-full bg-primary text-on-primary font-medium text-lg shadow-elevation-2 hover:shadow-elevation-3 active:shadow-elevation-1 transition-all flex items-center justify-center gap-2"
                        >
                            <PlayCircle size={24} />
                            {t('free_workout', lang)}
                        </button>

                        <button
                            onClick={() => setIsSporadicMode(true)}
                            className="w-full h-12 rounded-full bg-surface-container-high text-on-surface font-medium text-base hover:bg-surface-container-highest transition-all flex items-center justify-center gap-2"
                        >
                            <Plus size={20} />
                            {t('quick_log', lang)}
                        </button>
                    </div>

                    {plans.length > 0 ? (
                        <div className="w-full max-w-md mt-8">
                            <h3 className="text-sm text-on-surface-variant font-medium px-4 mb-3">{t('or_choose_plan', lang)}</h3>
                            <div className="grid grid-cols-1 gap-3">
                                {plans.map(plan => (
                                    <button
                                        key={plan.id}
                                        onClick={() => handleStart(plan)}
                                        className="flex items-center justify-between p-4 bg-surface-container rounded-xl hover:bg-surface-container-high transition-colors border border-outline-variant/20"
                                    >
                                        <div className="text-left">
                                            <div className="text-base font-medium text-on-surface">{plan.name}</div>
                                            <div className="text-xs text-on-surface-variant">{plan.steps.length} {t('exercises_count', lang)}</div>
                                        </div>
                                        <div className="w-10 h-10 rounded-full bg-secondary-container text-on-secondary-container flex items-center justify-center">
                                            <ArrowRight size={20} />
                                        </div>
                                    </button>
                                ))}
                            </div>
                        </div>
                    ) : (
                        <div className="w-full max-w-md mt-8 text-center p-6 bg-surface-container rounded-2xl border border-outline-variant/20">
                            <p className="text-on-surface-variant mb-4">{t('no_plans_yet', lang)}</p>
                            <div className="flex flex-col gap-2">
                                <a
                                    href="/plans?aiPrompt=true"
                                    className="text-primary font-medium hover:underline"
                                >
                                    {t('ask_ai_to_create', lang)}
                                </a>
                                <a
                                    href="/plans?create=true"
                                    className="text-primary font-medium hover:underline"
                                >
                                    {t('create_manually', lang)}
                                </a>
                            </div>
                        </div>
                    )}
                </div>

                {showPlanPrep && (
                    <div className="fixed inset-0 bg-black/60 z-50 flex items-center justify-center p-6 backdrop-blur-sm">
                        <div className="w-full max-w-sm bg-surface-container rounded-[28px] p-6 shadow-elevation-3">
                            <h3 className="text-2xl font-normal text-on-surface mb-4">{showPlanPrep.name}</h3>
                            <div className="bg-surface-container-high p-4 rounded-xl text-on-surface-variant text-sm mb-8">
                                <div className="text-xs font-bold text-primary mb-2">{t('prep_title', lang)}</div>
                                {showPlanPrep.description || t('prep_no_instructions', lang)}
                            </div>
                            <div className="flex justify-end gap-2">
                                <button onClick={() => setShowPlanPrep(null)} className="px-6 py-2.5 rounded-full text-primary font-medium hover:bg-white/5">{t('cancel', lang)}</button>
                                <button onClick={confirmPlanStart} className="px-6 py-2.5 rounded-full bg-primary text-on-primary font-medium">{t('start', lang)}</button>
                            </div>
                        </div>
                    </div>
                )}
            </div>
        </div>
    );
};

export default IdleView;
