import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import '../../../core/constants/app_colors.dart';
import '../../../core/services/notification_service.dart';
import '../../providers/notification_handler_provider.dart';
import '../share/share_screen.dart';
import '../restaurant_list/restaurant_list_screen.dart';
import '../random_selection/random_selection_screen.dart';
import '../calendar/calendar_screen.dart';
import '../settings/settings_screen.dart';

class MainScreen extends ConsumerStatefulWidget {
  final int initialTab;

  const MainScreen({super.key, this.initialTab = 2});

  @override
  ConsumerState<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends ConsumerState<MainScreen> {
  late int _selectedIndex;

  @override
  void initState() {
    super.initState();
    _selectedIndex = widget.initialTab;

    // 알림 핸들러 설정
    WidgetsBinding.instance.addPostFrameCallback((_) {
      NotificationService.onNotificationTap = (NotificationResponse response) {
        if (mounted) {
          ref
              .read(notificationHandlerProvider.notifier)
              .handleNotificationTap(context, response.payload);
        }
      };
    });
  }

  @override
  void didUpdateWidget(covariant MainScreen oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.initialTab != widget.initialTab &&
        _selectedIndex != widget.initialTab) {
      setState(() {
        _selectedIndex = widget.initialTab;
      });
    }
  }

  @override
  void dispose() {
    NotificationService.onNotificationTap = null;
    super.dispose();
  }

  final List<({IconData icon, String label})> _navItems = [
    (icon: Icons.share, label: '공유'),
    (icon: Icons.restaurant, label: '맛집'),
    (icon: Icons.casino, label: '뽑기'),
    (icon: Icons.calendar_month, label: '기록'),
    (icon: Icons.settings, label: '설정'),
  ];

  final List<Widget> _screens = [
    const ShareScreen(),
    const RestaurantListScreen(),
    const RandomSelectionScreen(),
    const CalendarScreen(),
    const SettingsScreen(),
  ];

  @override
  Widget build(BuildContext context) {
    final isDark = Theme.of(context).brightness == Brightness.dark;

    return Scaffold(
      body: IndexedStack(index: _selectedIndex, children: _screens),
      bottomNavigationBar: NavigationBar(
        selectedIndex: _selectedIndex,
        onDestinationSelected: (index) {
          setState(() => _selectedIndex = index);
        },
        backgroundColor: isDark
            ? AppColors.darkSurface
            : AppColors.lightSurface,
        destinations: _navItems
            .map(
              (item) => NavigationDestination(
                icon: Icon(item.icon),
                label: item.label,
              ),
            )
            .toList(),
        indicatorColor: AppColors.lightPrimary.withOpacity(0.2),
      ),
    );
  }
}
