import 'dart:collection';

import 'package:aitrainer_app/bloc/menu/menu_bloc.dart';
import 'package:aitrainer_app/bloc/test_set_execute/test_set_execute_bloc.dart';
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
import 'package:aitrainer_app/model/workout_menu_tree.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:aitrainer_app/util/track.dart';
import 'package:aitrainer_app/util/trans.dart';
import 'package:aitrainer_app/widgets/dialog_widget.dart';
import 'package:aitrainer_app/widgets/menu_search_bar.dart';
import 'package:aitrainer_app/widgets/victory_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
import 'package:flutter/cupertino.dart';

// ignore: must_be_immutable
class BottomBarMultipleExercises extends StatefulWidget {
  bool? isSet = false;
  final List<ExercisePlanDetail>? details;
  int? exerciseTypeId;

  BottomBarMultipleExercises({this.details, this.isSet, this.exerciseTypeId});
  @override
  _BottomBarMultipleExercisesState createState() => _BottomBarMultipleExercisesState();
}

class _BottomBarMultipleExercisesState extends State<BottomBarMultipleExercises> with Trans {
  final Color bgrColor = Color(0xffb4f500);
  final Color bgrColorEnd = Colors.blue;
  final Color active = Colors.black;
  final Color inactive = Colors.black26;
  ScrollController? _controller;

  @override
  void initState() {
    super.initState();
    SchedulerBinding.instance!.addPostFrameCallback((_) {
      _controller = ScrollController();
    });
  }

  @override
  void dispose() {
    _controller!.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    setContext(context);
    // ignore: close_sinks
    final MenuBloc menuBloc = BlocProvider.of<MenuBloc>(context);
    // ignore: close_sinks
    final TestSetExecuteBloc bloc = BlocProvider.of<TestSetExecuteBloc>(context);
    bloc.menuBloc = menuBloc;
    bloc.setExerciseTypeId(widget.exerciseTypeId!);
    bloc.add(TestSetExecuteLoad());
    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topRight,
          end: Alignment.bottomLeft,
          stops: [0.1, 0.99],
          colors: [
            bgrColor,
            bgrColorEnd,
          ],
        ),
      ),
      height: 70,
      child: BlocConsumer<TestSetExecuteBloc, TestSetExecuteState>(listener: (context, state) {
        if (state is TestSetExecuteError) {
          ScaffoldMessenger.of(context)
              .showSnackBar(SnackBar(backgroundColor: Colors.orange, content: Text(state.message, style: TextStyle(color: Colors.white))));
        }
      }, builder: (context, state) {
        if (state is TestSetExecuteReady && bloc.exercisePlanDetails != null) {
          print("BottomBarMulti offset ${bloc.scrollOffset}");
          /* if (bloc.scrollOffset > 0 && _controller != null) {
            _controller.animateTo(bloc.scrollOffset, duration: Duration(milliseconds: 300), curve: Curves.easeIn);
          } */
        }
        return ModalProgressHUD(
          child: getWidget(bloc),
          inAsyncCall: state is TestSetExecuteLoading,
          opacity: 0.5,
          color: Colors.black54,
          progressIndicator: CircularProgressIndicator(),
        );
      }),
    );
  }

  Widget getWidget(TestSetExecuteBloc bloc) {
    return SingleChildScrollView(
      controller: _controller,
      padding: EdgeInsets.only(left: 10, right: 10, bottom: 5),
      scrollDirection: Axis.horizontal,
      child: Row(
        children: getChildren(bloc),
      ),
    );
  }

  List<Widget> getChildren(TestSetExecuteBloc bloc) {
    print("Isset? ${widget.isSet}");
    final List<Widget> list = [];
    if (!widget.isSet! && !bloc.hasBegun()) {
      list.add(GestureDetector(
          onTap: () => newExercise(bloc),
          child: ClipRRect(
              borderRadius: BorderRadius.circular(24.0),
              child: Container(
                padding: EdgeInsets.only(top: 5),
                height: 60,
                color: Colors.transparent,
                child: Image.asset("asset/image/add_test.png"),
              ))));
      list.add(
        SizedBox(
          width: 10,
        ),
      );
      if (bloc.isDone100) {
        Victory();
      }
    }

    //if (bloc.miniTestSet && widget.isSet || bloc.paralellTest && !widget.isSet) {
    if (bloc.exercisePlanDetails != null) {
      bloc.exercisePlanDetails!.forEach((element) {
        final bool highlighted = element.exerciseTypeId == widget.exerciseTypeId;
        list.add(getImageStack(element.exerciseType!.nameTranslation, bloc, element.exerciseTypeId,
            bloc.getActualImageName(element.exerciseType!.exerciseTypeId),
            highlighted: highlighted));
      });
      //    }
    }

    return list;
  }

  void newExercise(TestSetExecuteBloc bloc) {
    HashMap ret = bloc.canAddNewExercise();
    if (ret['canAdd'] == false) {
      showCupertinoDialog(
          useRootNavigator: true,
          context: context,
          builder: (_) => CupertinoAlertDialog(
                insetAnimationDuration: Duration(milliseconds: 500),
                insetAnimationCurve: Curves.elasticInOut,
                title: ClipRRect(
                    borderRadius: BorderRadius.circular(12.0),
                    child: Container(
                        color: Colors.transparent,
                        child: Text(
                          t("You are about to add a new parallel test"),
                          style: GoogleFonts.inter(color: Colors.grey[900]),
                        ))),
                content: ClipRRect(
                    borderRadius: BorderRadius.circular(12.0),
                    child: Container(
                        color: Colors.transparent,
                        child: Column(children: [
                          Divider(),
                          Text(
                            t(ret['message']),
                            style: GoogleFonts.inter(color: Colors.grey[800]),
                          ),
                          Divider(
                            color: Colors.transparent,
                          ),
                          Text(
                            t(ret['message2']),
                            style: GoogleFonts.inter(color: Colors.grey[800]),
                          ),
                        ]))),
                actions: [
                  TextButton(
                    child: Text(t("No")),
                    onPressed: () => Navigator.pop(context),
                  ),
                  TextButton(
                    child: Text(t("Yes")),
                    onPressed: () => {
                      Navigator.pop(context),
                      addNewExercise(bloc),
                    },
                  )
                ],
              ));
    } else {
      addNewExercise(bloc);
    }
  }

  void addNewExercise(TestSetExecuteBloc bloc) {
    WorkoutMenuTree? foundMenuItem;
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return DialogWidget(
            onTap: () => {
              if (foundMenuItem != null)
                {
                  bloc.add(TestSetExecuteDeleteActive()),
                  bloc.add(TestSetExecuteNewExercise(exerciseTypeId: widget.exerciseTypeId!)),
                  bloc.add(TestSetExecuteNewExercise(exerciseTypeId: foundMenuItem!.exerciseTypeId)),
                  Track().track(TrackingEvent.exercise_new_paralell, eventValue: foundMenuItem!.exerciseType!.name),
                },
              Navigator.pop(context),
            },
            title: t("Please select an exercise"),
            description: t("Add this exercise to execute it paralell"),
            widget: MenuSearchBar(
              showIcon: false,
              onFind: (workoutMenuTree) => foundMenuItem = workoutMenuTree,
              listItems: bloc.menuBloc.menuTreeRepository.menuAsExercise,
            ),
          );
        });
  }

  List<Widget> imageSliders(TestSetExecuteBloc bloc) {
    List<Widget> list = [];
    final Widget widget = ClipRRect(
        borderRadius: BorderRadius.circular(24.0),
        child: GestureDetector(
            onTap: () => newExercise(bloc),
            child: Container(
              color: Colors.transparent,
              child: Image.asset("asset/image/add_test.png"),
            )));
    list.add(widget);
    return list;
  }

  Widget getImageStack(String imageName, TestSetExecuteBloc bloc, int exerciseTypeId, String image, {highlighted = false}) {
    return Container(
        width: 120,
        child: Stack(alignment: Alignment.bottomLeft, fit: StackFit.loose, children: [
          Container(
              padding: EdgeInsets.only(left: 0, top: 10),
              child: Stack(alignment: Alignment.topRight, children: [
                ClipRRect(
                    borderRadius: BorderRadius.circular(12.0),
                    child: Container(
                      decoration: highlighted
                          ? BoxDecoration(
                              border: Border.all(
                                color: Colors.white,
                                width: 3, //
                              ),
                            )
                          : BoxDecoration(
                              border: Border.all(
                                color: Colors.transparent,
                                width: 1, //
                              ),
                            ),
                      height: 60,
                      //color: Colors.transparent,
                      child: Image.asset(image),
                    )),
                !widget.isSet! && !bloc.hasBegun()
                    ? Positioned(
                        top: -8,
                        child: GestureDetector(
                            onTap: () => {
                                  print("Delete: $imageName"),
                                  bloc.add(TestSetExecuteDeleteExercise(exerciseTypeId: exerciseTypeId)),
                                },
                            child: Text(
                              "X",
                              style: GoogleFonts.archivoBlack(color: Colors.orange[900], fontSize: 20),
                            )))
                    : Offstage(),
              ])),
          Container(
            padding: EdgeInsets.only(left: 10, bottom: 5, right: 5),
            child: Text(
              imageName,
              maxLines: 2,
              style: GoogleFonts.archivoBlack(color: Color(0xffb4f500), fontSize: 10),
            ),
          ),
        ]));
  }
}
