import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../managers/data_manager.dart';
import '../managers/sound_manager.dart';
import '../pages/home_page.dart';
import '../pages/favorites_page.dart';
import '../pages/messages_page.dart';
import '../pages/settings_page.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({super.key});

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

class _MainScreenState extends State<MainScreen> {
  int _currentIndex = 0;
  bool _isDataLoaded = false;

  @override
  void initState() {
    super.initState();
    _initData();
  }

  void _initData() async {
    await DataManager().loadData();
    if (mounted) {
      setState(() {
        _isDataLoaded = true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    if (!_isDataLoaded) {
      return const Scaffold(
        body: Center(
          child: CircularProgressIndicator(color: Color(0xFF6C63FF)),
        ),
      );
    }

    final List<Widget> pages = [
      const HomePage(),
      const FavoritesPage(),
      const MessagesPage(),
      const SettingsPage(),
    ];

    return Scaffold(
      extendBody: true,
      body: IndexedStack(
        index: _currentIndex,
        children: pages,
      ),
      bottomNavigationBar: SafeArea(
        child: Padding(
          padding: const EdgeInsets.fromLTRB(24, 0, 24, 20),
          child: Container(
            height: 70,
            decoration: BoxDecoration(
              color: Colors.white.withOpacity(0.9),
              borderRadius: BorderRadius.circular(35),
              boxShadow: [
                BoxShadow(
                  color: const Color(0xFF6C63FF).withOpacity(0.15),
                  blurRadius: 20,
                  offset: const Offset(0, 8),
                  spreadRadius: 2,
                ),
              ],
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(35),
              child: BackdropFilter(
                filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    _buildCustomNavItem(0, Icons.grid_view_rounded, Icons.grid_view_outlined),
                    _buildCustomNavItem(1, Icons.favorite_rounded, Icons.favorite_border_rounded),
                    _buildCustomNavItem(2, Icons.notifications_rounded, Icons.notifications_none_rounded),
                    _buildCustomNavItem(3, Icons.settings_rounded, Icons.settings_outlined),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildCustomNavItem(int index, IconData activeIcon, IconData inactiveIcon) {
    final bool isSelected = _currentIndex == index;

    return GestureDetector(
      onTap: () {
        if (_currentIndex != index) {
          SoundManager().stop();
          HapticFeedback.lightImpact();
          setState(() => _currentIndex = index);
        }
      },
      behavior: HitTestBehavior.opaque,
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 250),
        curve: Curves.easeOutBack,
        padding: isSelected
            ? const EdgeInsets.symmetric(horizontal: 20, vertical: 12)
            : const EdgeInsets.all(12),
        decoration: BoxDecoration(
          color: isSelected ? const Color(0xFF6C63FF) : Colors.transparent,
          borderRadius: BorderRadius.circular(25),
        ),
        child: Icon(
          isSelected ? activeIcon : inactiveIcon,
          color: isSelected ? Colors.white : const Color(0xFF9EA3B8),
          size: 24,
        ),
      ),
    );
  }
}
