mirrored_repositories/lifeclass/lib/pages/content/lessons
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lifeclass/classes/clsApp.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../../classes/clsRichTextFormatter.dart';
import '../../../../classes/sql/clsSqlLesson.dart';
import 'package:lifeclass/pages/content/lessons/classes/clsLanguageSelector.dart';
import 'package:lifeclass/classes/sql/sqlHelper.dart';
import 'package:lifeclass/classes/Themes.dart';

import '../../../../models/modelDailyVerse.dart';
import '../../../../widgets/ads/BannerAds.dart';
import '../components/ParagraphCenterCenterFormatter.dart';
import '../components/ParagraphFormatter.dart';
import '../controlBox/MultiInputField.dart';

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

class _LessonQuestionnaire04State extends State<LessonQuestionnaire04> {
  late String colorTheme=clsApp.defaultColorThemes;
  late double fontSize=16;
  late String language = clsApp.defaultLanguage;
  clsSqlLesson? lessonData;
  modelDailyVerse? dailyVerseData;

  final int lessonID = 4;
  final  answer_1 = TextEditingController();

  late String createdDt = '';
  late String content = '';
  late int maxAnswers=0;
  late int completionRate=0;
  late int _currentFontSize = 1;
  List<double> availableFontSize = [10,12,16,18,20,24,28,32];

  @override
  void initState() {
    super.initState();
    initTheme();
    loadData();
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
  void initTheme() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      colorTheme = prefs.getString('colorThemes') ?? clsApp.defaultColorThemes;
      _currentFontSize = prefs.getInt('fontSize') ?? 1;
      language = prefs.getString('language') ?? clsApp.defaultLanguage;
      fontSize = availableFontSize[_currentFontSize];
    });
  }
  void loadData() async{
    lessonData = await sqlHelper.getLessonByID(lessonID);
    dailyVerseData = await sqlHelper.getDailyVerseByID(lessonID);

    maxAnswers = lessonData!.maxAnswers;
    completionRate = lessonData!.completionRate;
    answer_1.text = lessonData!.answer_1;

    createdDt = lessonData!.createdDt;
  }

  void updateAnswers() async{
    String ans1 = answer_1.text;
    String ans2 = '';
    String ans3 = '';
    String ans4 = '';
    String ans5 = '';

    int completionCounter = 0;
    if(answer_1.text.trim()!='')
      completionCounter+=1;

    double totalPercentage = (completionCounter/1) * 100;
    completionRate = int.parse(totalPercentage.toStringAsFixed(0));

    await sqlHelper.updateLesson(
        lessonID,
        ans1,
        ans2,
        ans3,
        ans4,
        ans5,
        maxAnswers,
        completionRate,
        createdDt);

    setState(() {
      var snackBar = SnackBar(content: Text(clsLanguagePreset.label_update_notify(language)));
      ScaffoldMessenger.of(context).showSnackBar(snackBar);

      if (Navigator.canPop(context)) {
        Navigator.pop(context);
      } else {
        SystemNavigator.pop();
      }

    });
  }
  void clearFields(){
    Navigator.pop(context,false);
    answer_1.text='';
    setState(() {});
  }

  void openDailyVerse(int dailyVerseID, String content, String title){
    Navigator.of(context).pushNamed('/DailyVerseDetails', arguments: {
      'dailyVerseID': dailyVerseID.toString(),
      'content': content,
      'title' : title
    });
  }

  @override
  Widget build(BuildContext context) {
    var brightness = MediaQuery
        .of(context)
        .platformBrightness;
    bool isDarkMode = brightness == Brightness.dark;

    List<clsRichTextFormatter> question_01 = [
      clsRichTextFormatter(content: clsWeek01Lesson4.question_01(language),
          fontStyle: FontStyle.normal,
          fontWeight: FontWeight.normal,
          bColor: '',
          fontSize: fontSize,
          isDarkMode: isDarkMode),
      clsRichTextFormatter(content: '',
          fontStyle: FontStyle.italic,
          fontWeight: FontWeight.normal,
          bColor: colorTheme,
          fontSize: fontSize,
          isDarkMode: isDarkMode),
    ];

    List<clsRichTextFormatter> remember_the_lesson = [
      clsRichTextFormatter(
          content: clsWeek01Lesson4.remember_the_lesson(language),
          fontStyle: FontStyle.italic,
          fontWeight: FontWeight.bold,
          bColor: '',
          fontSize: fontSize,
          isDarkMode: isDarkMode),
      clsRichTextFormatter(content: '',
          fontStyle: FontStyle.italic,
          fontWeight: FontWeight.normal,
          bColor: colorTheme,
          fontSize: fontSize,
          isDarkMode: isDarkMode),
    ];
    List<clsRichTextFormatter> study_the_word = [
      clsRichTextFormatter(content: clsWeek01Lesson4.study_the_word(language),
          fontStyle: FontStyle.normal,
          fontWeight: FontWeight.normal,
          bColor: '',
          fontSize: fontSize,
          isDarkMode: isDarkMode),
      clsRichTextFormatter(content: '',
          fontStyle: FontStyle.italic,
          fontWeight: FontWeight.normal,
          bColor: colorTheme,
          fontSize: fontSize,
          isDarkMode: isDarkMode),
    ];


    return Scaffold(
      body:
      Scaffold(
        appBar: AppBar(
          actions: [
            IconButton(onPressed: () {
              showDialog(
                  context: context,
                  builder: (context) =>
                      AlertDialog(
                        title: Text(
                          clsLanguagePreset.label_clear_fields(language),
                          style: TextStyle(fontSize: 16.0),),
                        actions: <Widget>[
                          TextButton(
                              onPressed: () => Navigator.pop(context, false),
                              child: Text(clsLanguagePreset.label_no(language),
                                style: TextStyle(
                                    color: myThemes.getColor(colorTheme)),)),
                          TextButton(
                              onPressed: () => clearFields(),
                              child: Text(clsLanguagePreset.label_yes(language),
                                style: TextStyle(
                                    color: myThemes.getColor(colorTheme)),)),
                        ],
                      )
              );
            },
              icon: Icon(Icons.delete),
              tooltip: clsLanguagePreset.icon_label_clear_all_fields(language),)
          ],
          title: Text(
              clsLanguagePreset.app_bar_label_questionnaire(language) + ' ' +
                  lessonID.toString()),
          backgroundColor: myThemes.getColor(colorTheme),
        ),
        floatingActionButton: FloatingActionButton(
            onPressed: updateAnswers,
            backgroundColor: myThemes.getColor(colorTheme),
            child: Icon(Icons.save),
            tooltip: clsLanguagePreset.float_button_label_save_answers(
                language)),

        body: SafeArea(
          child: Column(
            children: [Expanded(
              child: RawScrollbar(
                child: SingleChildScrollView(
                  child: SafeArea(
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Center(child: Image.asset(
                            'assets/images/live_the_lessons.png')),
                        Divider(),
                        ParagraphFormatter(richTextData: question_01),
                        MultiInputField(controller: answer_1,
                            hint: clsLanguagePreset.hint_enter_your_answer(
                                language),
                            maxLines: 10,
                            colorTheme: colorTheme,
                            fontSize: fontSize),
                        Center(child: Image.asset(
                            'assets/images/remember_the_lesson.png')),
                        Divider(),
                        ParagraphCenterCenterFormatter(
                            richTextData: remember_the_lesson, marginGap: 50),
                        Center(child: Image.asset(
                            'assets/images/study_the_word.png')),
                        Divider(),
                        ParagraphCenterCenterFormatter(
                            richTextData: study_the_word, marginGap: 50),
                        SizedBox(width: 0, height: 30,),
                        Center(child: Padding(padding: EdgeInsets.all(10.0),
                            child: ElevatedButton.icon(
                              icon: Icon(Icons.open_in_new),
                              onPressed: () {
                                openDailyVerse(lessonID,
                                    dailyVerseData!.content.toString(),
                                    'DAY ' + lessonID.toString());
                              },
                              style: ButtonStyle(
                                  backgroundColor: MaterialStateProperty.all(
                                      myThemes.getColor(colorTheme))),
                              label: Padding(padding: EdgeInsets.all(15),
                                  child: Text(clsLanguagePreset
                                      .button_open_daily_readings(
                                      language))),))),
                        SizedBox(width: 0, height: 100,),
                      ],
                    ),
                  ),
                ),
              ),
            ),
              BannerAds()
            ],
          ),
        ),
      ),
    );
  }

}
