mirrored_repositories/lifeclass/lib
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:lifeclass/classes/clsApp.dart';
import 'package:lifeclass/classes/clsNetwork.dart';
import 'package:lifeclass/pages/settings/Settings.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:lifeclass/classes/Themes.dart';
import 'content/Content.dart';
import 'package:lifeclass/classes/clsNavBarPanel.dart';

var indexClicked = 0;
var appBarTitle = 'Student Book: Lifeclass';

class MainPage extends StatefulWidget {
  const MainPage({super.key});
  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  late List<Widget> pages = [];
  late String colorTheme = clsApp.defaultColorThemes;
  late int currentTabIndex = 0;
  late String bibleVersion = 'NIV';
  late bool isOnline=false;
  late String language = clsApp.defaultLanguage;

  @override
  void initState() {
    initTheme();
    pages = [
      Content(),
      Container(child: Settings(colorTheme: colorTheme, bibleVersion: bibleVersion,selectedBibleVersion: loadBibleVersionData,selectedLanguage: loadLanguageData, selectedColor: loadThemeData,appMode: 'full',hasNetwork: isOnline,language: language,),
      ),
    ];
    super.initState();
  }
  @override
  void dispose() {
    super.dispose();
  }

  void loadThemeData(String selectedColor) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('colorThemes', selectedColor);
    colorTheme = selectedColor;
    isOnline = await clsNetwork.hasNetwork();
    pages = [
      Content(),
      Container(child: Settings(colorTheme: colorTheme,bibleVersion: bibleVersion,selectedBibleVersion: loadBibleVersionData,selectedLanguage: loadLanguageData,  selectedColor: loadThemeData,appMode:'full',hasNetwork: isOnline,language: language,),
      ),
    ];
    setState(() {
    });
  }
  void initTheme() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    isOnline = await clsNetwork.hasNetwork();
    setState(() {
      colorTheme = prefs.getString('colorThemes') ?? clsApp.defaultColorThemes;
      bibleVersion = prefs.getString('bibleVersion') ?? clsApp.defaultBibleVersion;
      language = prefs.getString('language') ?? clsApp.defaultLanguage;
    });
  }
  Future<bool> hasNetwork() async {
    try {
      final result = await InternetAddress.lookup('example.com');
      return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
    } on SocketException catch (_) {
      return false;
    }
  }


  void loadBibleVersionData(String _bibleVersion) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('bibleVersion', _bibleVersion);
    bibleVersion = _bibleVersion;
    isOnline = await clsNetwork.hasNetwork();
    print('->changed'+ bibleVersion);
    pages = [
      Content(),
      Container(child: Settings(colorTheme: colorTheme,bibleVersion: bibleVersion,selectedBibleVersion: loadBibleVersionData,selectedLanguage: loadLanguageData,  selectedColor: loadThemeData,appMode:'full',hasNetwork: isOnline,language:language),
      ),
    ];
    setState(() {
    });
  }
  void loadLanguageData(String value) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('language', value);
    language = value;
    isOnline = await hasNetwork();
    print('->languagechanged'+ language);
    pages = [
      Content(),
      Container(child: Settings(colorTheme: colorTheme,bibleVersion: bibleVersion,selectedBibleVersion: loadBibleVersionData,selectedLanguage: loadLanguageData,  selectedColor: loadThemeData,appMode:'full',hasNetwork: isOnline,language:language),
      ),
    ];
    setState(() {
    });
  }

  @override
  Widget build(BuildContext context) {
    VoidCallback clickDrawer(int index) {
      return () {
        setState(() {
          initTheme();
          indexClicked = index;
          pages = [
            Content(),
            Container(child: Settings(colorTheme: colorTheme,
              bibleVersion: bibleVersion,
              selectedBibleVersion: loadBibleVersionData,
              selectedColor: loadThemeData,
              selectedLanguage: loadLanguageData,
              appMode: 'full',
              hasNetwork: isOnline,language:language),
            )
          ];
          Navigator.pop(context);
          if (index == 0)
            appBarTitle = 'LifeClass: Student Book';
          else
            appBarTitle = 'Settings';
        });
      };
    }
    Future<bool> _onBackPressed() async{
      return (await showDialog(
          context: context,
          builder: (context)=> AlertDialog(
            title: const Text('Do you really want to exit the app?',style: TextStyle(fontSize: 16.0),),
            actions: <Widget>[
              TextButton(
                  onPressed: ()=>Navigator.pop(context,false),
                  child:  Text('No',style: TextStyle(color: myThemes.getColor(colorTheme)),)),
              TextButton(
                  onPressed: ()=>Navigator.pop(context,true),
                  child:  Text('Yes',style: TextStyle(color: myThemes.getColor(colorTheme)),)),
            ],
          )
      )
      );
    }


    return WillPopScope(
      onWillPop: _onBackPressed,
      child: Scaffold(
        appBar: AppBar(
          title: Text(appBarTitle),
          backgroundColor: myThemes.getColor(colorTheme),
          actions: indexClicked==0 ?  [
            IconButton(icon: Icon(Icons.info), onPressed: () {
              Navigator.pushNamed(context, '/copyright');
            },),
          ] : null,
        ),
        body: pages[indexClicked],
        drawer: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: [
              UserAccountsDrawerHeader(
                accountName: const Text('LIFECLASS',style: (TextStyle(fontWeight: FontWeight.bold)),),
                accountEmail: const Text('S t u d e n t  B o o k',overflow: TextOverflow.ellipsis, style: TextStyle(fontStyle: FontStyle.italic,fontSize: 12),),
                currentAccountPicture: CircleAvatar(
                  child: ClipOval(
                    child: Image.asset('assets/images/app_logo.png'),
                  ),
                ),
                decoration: BoxDecoration(
                    color: myThemes.getColor(colorTheme),
                    image: DecorationImage(
                        image: AssetImage(''),
                        fit: BoxFit.cover)
                ),
              ),
              ListTile(
                  leading: Icon(
                      clsNavBarPanel.drawerItemIcon[0],
                      color: indexClicked ==0 ? myThemes.getColor(colorTheme) : clsNavBarPanel.drawerItemColor),
                  title: Text(
                    clsNavBarPanel.drawerItemText[0],
                    style: TextStyle(color: indexClicked ==0 ? myThemes.getColor(colorTheme) : clsNavBarPanel.drawerItemColor),),
                  onTap: clickDrawer(0)
              ),
              ListTile(
                  leading: Icon(
                      clsNavBarPanel.drawerItemIcon[1],
                      color: indexClicked ==1 ? myThemes.getColor(colorTheme) : clsNavBarPanel.drawerItemColor),
                  title: Text(
                    clsNavBarPanel.drawerItemText[1],
                    style: TextStyle(color: indexClicked ==1 ? myThemes.getColor(colorTheme) : clsNavBarPanel.drawerItemColor),),
                  onTap: clickDrawer(1)
              ),
              const Divider(),
              ListTile(
                leading: Icon(
                    clsNavBarPanel.drawerItemIcon[2],
                    color: indexClicked ==2 ? myThemes.getColor(colorTheme) : clsNavBarPanel.drawerItemColor),
                title: Text(
                  clsNavBarPanel.drawerItemText[2],
                  style: TextStyle(color: indexClicked ==2 ? myThemes.getColor(colorTheme) : clsNavBarPanel.drawerItemColor),),
                onTap: (){
                  setState(() {
                    Navigator.pop(context);
                    showDialog(
                        context: context,
                        builder: (context)=> AlertDialog(
                          title: const Text('Do you really want to exit the app?',style: TextStyle(fontSize: 16.0),),
                          actions: <Widget>[
                            TextButton(
                                onPressed: ()=>Navigator.pop(context,false),
                                child:  Text('No',style: TextStyle(color: myThemes.getColor(colorTheme)),)),
                            TextButton(
                                onPressed: ()=>Navigator.pop(context,true),
                                child:  Text('Yes',style: TextStyle(color: myThemes.getColor(colorTheme)),)),
                          ],
                        )
                    );
                  });
                },
              )
            ],
          ),
        ),
      ),
    );




  }
}
