import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers.dart';
import '../widgets/weather_info_overlay.dart';
import '../widgets/weather_details_sheet.dart';

class HomeScreen extends ConsumerWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final weatherAsync = ref.watch(weatherProvider);
    final wallpaperPath = ref.watch(dynamicWallpaperProvider);

    return Stack(
      children: [
        AnimatedSwitcher(
          duration: const Duration(milliseconds: 1000),
          child: Container(
            key: ValueKey<String>(wallpaperPath),
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage(wallpaperPath),
                fit: BoxFit.cover,
              ),
            ),
          ),
        ),
        Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                Colors.black.withOpacity(0.3),
                Colors.black.withOpacity(0.0),
                Colors.black.withOpacity(0.4),
                Colors.black.withOpacity(0.7),
              ],
              stops: const [0.0, 0.4, 0.7, 1.0],
            ),
          ),
        ),
        weatherAsync.when(
          data: (weather) => WeatherInfoOverlay(weather: weather),
          loading: () => const Center(child: CircularProgressIndicator()),
          error: (e, s) => Center(
            child: Text(
              'Failed to load weather:\n$e',
              textAlign: TextAlign.center,
              style: const TextStyle(color: Colors.white),
            ),
          ),
        ),
        weatherAsync.maybeWhen(
          data: (weather) => WeatherDetailsSheet(
            forecast: weather.forecast,
            current: weather.current,
          ),
          orElse: () => const SizedBox.shrink(),
        ),
      ],
    );
  }
}
