import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/screens/auth/state/state.dart';
import 'package:qadirneyriz/screens/home/screen.dart';
import 'package:qadirneyriz/screens/meeting/screen.dart';
import 'package:qadirneyriz/setting/setting.dart';

class CustomDrawerNavigation extends StatefulWidget {
  final int activeTab;

  const CustomDrawerNavigation({super.key, required this.activeTab});

  @override
  _CustomDrawerNavigationState createState() => _CustomDrawerNavigationState();
}

class _CustomDrawerNavigationState extends State<CustomDrawerNavigation> {
  late final PageController _pageController;
  int _selectedIndex = 0;
  final String language = setting.userLocalDb.getUser().language;
  String? selectedLanguage; // زبان پیش‌فرض فارسی
  late AuthState state;
  @override
  void initState() {
    super.initState();
    state = Provider.of(context, listen: false);
    selectedLanguage = language;
    _selectedIndex = widget.activeTab;
    _pageController = PageController(initialPage: _selectedIndex);
  }

  final List<Widget> _bottomBarPages = [
    const HomeScreen(),
    const MeetingsScreen()
    // Add more screens here
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
    _pageController.jumpToPage(index);
    Navigator.pop(context); // Close the drawer
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Consumer<AuthState>(
        builder: (context, value, child) {
          return Drawer(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(left: 16.0, top: 40),
                  child: Image.asset(
                    'assets/images/iconinappbar.png', // مسیر لوگوی شما
                    height: 60,
                  ),
                ),
                NewSessionButton(),
                Expanded(
                  child: ListView(
                    padding: EdgeInsets.zero,
                    children: <Widget>[
                      _buildDrawerItem(
                        icon: FontAwesomeIcons.house,
                        text: 'خانه',
                        index: 0,
                      ),
                      _buildDrawerItem(
                        icon: FontAwesomeIcons.pencil,
                        text: 'جلسات',
                        index: 1,
                      ),
                      _buildDrawerItem(
                        icon: FontAwesomeIcons.pencil,
                        text: 'ملاقات ها',
                        index: 2,
                      ),
                      _buildDrawerItem(
                        icon: FontAwesomeIcons.pencil,
                        text: 'گزارشات',
                        index: 3,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          decoration: BoxDecoration(
                            color: Colors.grey[300],
                            borderRadius: BorderRadius.circular(10),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              _buildLanguageButton('fa', 'فارسی', () {
                                value.setLocale('fa');
                              }),
                              _buildLanguageButton('en', 'English', () {
                                value.setLocale('en');
                              }),
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                const Divider(),
              ],
            ),
          );
        },
      ),
      body: PageView(
        controller: _pageController,
        physics: const NeverScrollableScrollPhysics(),
        children: _bottomBarPages,
      ),
    );
  }

  Widget _buildDrawerItem(
      {required IconData icon, required String text, required int index}) {
    bool isSelected = _selectedIndex == index;
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
      child: Material(
        color: isSelected
            ? config.ui.mainGreen.withOpacity(.2)
            : Colors.transparent,
        borderRadius: BorderRadius.circular(8),
        child: InkWell(
          borderRadius: BorderRadius.circular(8),
          onTap: () => _onItemTapped(index),
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
            ),
            child: ListTile(
              leading: FaIcon(
                icon,
                size: 19,
                color: isSelected ? config.ui.mainGreen : config.ui.mainGray,
              ),
              title: Text(
                text,
                style: TextStyle(
                    color:
                        isSelected ? config.ui.mainGreen : config.ui.mainGray,
                    fontWeight:
                        isSelected ? FontWeight.bold : FontWeight.normal,
                    fontSize: 15),
              ),
              selected: isSelected,
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildLanguageButton(
      String language, String text, void Function() onPressed) {
    bool isSelected = selectedLanguage == language;

    return ElevatedButton(
      onPressed: () {
        setState(() {
          selectedLanguage = language; // به‌روز کردن زبان انتخاب شده
        });
        onPressed(); // اجرای متد تغییر زبان
      },
      style: ElevatedButton.styleFrom(
        backgroundColor: isSelected ? Colors.green : Colors.grey[300],
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8.0),
        ),
      ),
      child: Text(
        text,
        style: TextStyle(
          color: isSelected ? Colors.white : Colors.black,
        ),
      ),
    );
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }
}

class NewSessionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        backgroundColor: Colors.green, // رنگ لبه‌ها
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8), // گرد کردن گوشه‌ها
        ),
        elevation: 0, // بدون سایه
        padding: EdgeInsets.symmetric(vertical: 20, horizontal: 16),
      ),
      onPressed: () {
        // کاری که باید انجام شود
      },
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            Icons.person_outline,
            color: Colors.white, // رنگ آیکون
            size: 40,
          ),
          SizedBox(height: 8),
          Text(
            'جلسه جدید',
            style: TextStyle(
              color: Colors.white, // رنگ متن
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }
}

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

  @override
  State<CustomScalfod> createState() => _CustomScalfodState();
}

class _CustomScalfodState extends State<CustomScalfod> {
  @override
  Widget build(BuildContext context) {
    return Scaffold();
  }
}
