import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../data/services/sync_service.dart';
import '../../data/services/sound_service.dart';
import '../../core/enums.dart';
import '../../core/sacred_tokens.dart';
import '../../providers.dart';
import 'main_screen.dart';
import 'adzan_screen.dart';
import 'iqomah_screen.dart';
import 'black_screen.dart';
import 'slideshow_screen.dart';
import 'jumat_screen.dart';
import 'khutbah_screen.dart';

/// The root view that orchestrates all screen states via AnimatedSwitcher.
class HomeView extends ConsumerStatefulWidget {
  const HomeView({super.key});

  @override
  ConsumerState<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends ConsumerState<HomeView> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _checkAutoSync();
    });
  }

  Future<void> _checkAutoSync() async {
    final schedule = ref.read(todayScheduleProvider);
    if (schedule == null) {
      debugPrint('[AutoSync] No schedule found for today! Starting auto-sync...');
      final success = await SyncService.instance.syncMonthlyData();
      if (success && mounted) {
        debugPrint('[AutoSync] Success! Invalidating todayScheduleProvider.');
        ref.invalidate(todayScheduleProvider);
      } else {
        debugPrint('[AutoSync] Failed or data remained empty.');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    // Audio trigger listener
    ref.listen<ScreenStateData>(screenStateProvider, (previous, next) {
      if (previous == null) return;

      // TRIGGER 1: Adzan Beep (Fires precisely when transitioning to Adzan)
      if (previous.state != ScreenState.adzan && next.state == ScreenState.adzan) {
        SoundService.instance.playAdzanBeep();
      }

      // TRIGGER 2: 3-Second Iqomah Countdown
      if (next.state == ScreenState.menujuIqomah && next.iqomahRemaining != null) {
        // Play precisely on the tick where it is 3 seconds.
        if (previous.iqomahRemaining?.inSeconds != 3 && next.iqomahRemaining!.inSeconds == 3) {
          SoundService.instance.playIqomahCountdown();
        }
      }
    });

    final screenData = ref.watch(screenStateProvider);
    final isMainScreen = ref.watch(isMainScreenProvider);

    // Determine which screen to display
    Widget screen;
    switch (screenData.state) {
      case ScreenState.normal:
      case ScreenState.menujuAdzan:
        if (screenData.isFriday && screenData.nextPrayer?.id == 'dzuhur') {
          screen = const JumatScreen(key: ValueKey('jumat'));
        } else {
          screen = isMainScreen 
              ? const MainScreen(key: ValueKey('main')) 
              : const SlideshowScreen(key: ValueKey('slideshow'));
        }
        break;
      case ScreenState.kembaliNormal:
        screen = const MainScreen(key: ValueKey('main'));
        break;
      case ScreenState.adzan:
        screen = const AdzanAlertScreen(key: ValueKey('adzan'));
        break;
      case ScreenState.menujuIqomah:
        if (screenData.isFriday && screenData.activePrayer?.id == 'dzuhur') {
          screen = const KhutbahScreen(key: ValueKey('khutbah'));
        } else {
          screen = const IqomahScreen(key: ValueKey('iqomah'));
        }
        break;
      case ScreenState.shalat:
        screen = const BlackScreen(key: ValueKey('black'));
        break;
    }

    final isSimulating = ref.watch(mockTimeOffsetProvider) != Duration.zero;

    return Scaffold(
      backgroundColor: SacredColors.background,
      body: Stack(
        children: [
          AnimatedSwitcher(
            duration: const Duration(milliseconds: 800),
            transitionBuilder: (child, animation) {
              return FadeTransition(opacity: animation, child: child);
            },
            child: screen,
          ),
          
          if (isSimulating)
            Positioned(
              right: 64,
              bottom: 64,
              child: ElevatedButton.icon(
                onPressed: () {
                  ref.read(mockTimeOffsetProvider.notifier).state = Duration.zero;
                },
                icon: const Icon(Icons.cancel, color: Colors.white),
                label: const Text(
                  'BATALKAN SIMULASI',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    letterSpacing: 2,
                    color: Colors.white,
                  ),
                ),
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.red.shade800,
                  padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
                  elevation: 10,
                ),
              ),
            ),
        ],
      ),
    );
  }
}
