import 'dart:collection';

import 'package:aitrainer_app/bloc/test_set_execute/test_set_execute_bloc.dart';
import 'package:aitrainer_app/bloc/test_set_new/test_set_new_bloc.dart';
import 'package:aitrainer_app/library/custom_icon_icons.dart';
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
import 'package:aitrainer_app/model/exercise_type.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:aitrainer_app/util/trans.dart';
import 'package:aitrainer_app/widgets/app_bar.dart';
import 'package:aitrainer_app/widgets/bottom_bar_multiple_exercises.dart';
import 'package:aitrainer_app/widgets/exercise_save.dart';
import 'package:flutter/material.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';

// ignore: must_be_immutable
class TestSetNew extends StatelessWidget with Trans {
  @override
  Widget build(BuildContext context) {
    final HashMap args = ModalRoute.of(context)!.settings.arguments as HashMap;
    final ExerciseType exerciseType = args['exerciseType'];
    final int exercisePlanDetailId = args['exercisePlanDetailId'];
    // ignore: close_sinks
    final TestSetExecuteBloc executeBloc = args['testSetExecuteBloc'];
    late TestSetNewBloc bloc;
    setContext(context);
    return Scaffold(
      appBar: AppBarNav(depth: 1),
      body: Container(
          padding: EdgeInsets.all(20),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('asset/image/WT_black_background.jpg'),
              fit: BoxFit.cover,
              alignment: Alignment.center,
            ),
          ),
          child: BlocProvider(
            create: (context) => TestSetNewBloc(
                exerciseRepository: ExerciseRepository(),
                exerciseType: exerciseType,
                exercisePlanDetailId: exercisePlanDetailId,
                executeBloc: executeBloc),
            child: BlocConsumer<TestSetNewBloc, TestSetNewState>(listener: (context, state) {
              if (state is TestSetNewError) {
                ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(backgroundColor: Colors.orange, content: Text(state.message, style: TextStyle(color: Colors.white))));
              } else if (state is TestSetNewReady) {
                print("Actual state: ${executeBloc.actualState(exerciseType.exerciseTypeId).toString()}");
                if (executeBloc.actualState(exerciseType.exerciseTypeId).equalsTo(ExercisePlanDetailState.inProgress)) {
                  HashMap args = HashMap();
                  final ExercisePlanDetail actualExercisePlanDetail = executeBloc.actualExercisePlanDetail(exerciseType.exerciseTypeId)!;
                  args['exerciseType'] = exerciseType;
                  args['exercisePlanDetail'] = actualExercisePlanDetail;
                  args['testSetExecuteBloc'] = executeBloc;
                  Navigator.of(context).pushNamed("testSetControl", arguments: args);
                }
              }
            }, builder: (context, state) {
              bloc = BlocProvider.of<TestSetNewBloc>(context);
              return ModalProgressHUD(
                child: getExercises(bloc),
                inAsyncCall: state is TestSetNewLoading,
                opacity: 0.5,
                color: Colors.black54,
                progressIndicator: CircularProgressIndicator(),
              );
            }),
          )),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () => {
          Navigator.of(context).pop(),
          bloc.add(TestSetNewSubmit()),
        },
        backgroundColor: Colors.orange[800],
        icon: Icon(CustomIcon.save),
        label: Text(
          t("Save"),
          style: GoogleFonts.inter(fontWeight: FontWeight.bold, fontSize: 16),
        ),
      ),
      bottomNavigationBar: BottomBarMultipleExercises(
        isSet: executeBloc.miniTestSet == true,
        exerciseTypeId: exerciseType.exerciseTypeId,
      ),
    );
  }

  Widget getExercises(TestSetNewBloc bloc) {
    return ExerciseSave(
      exerciseName: bloc.exerciseType.nameTranslation,
      exerciseDescription: bloc.exerciseType.descriptionTranslation,
      exerciseTask: bloc.exerciseType.unitQuantityUnit != null
          ? t("Please take a relative bigger weight and repeat 12-20 times and do your best! MAXIMIZE it!")
          : t("Please repeat as much times as you can! MAXIMIZE it!"),
      unit: bloc.exerciseType.unit,
      unitQuantityUnit: bloc.exerciseType.unitQuantityUnit,
      hasUnitQuantity: bloc.exerciseType.unitQuantityUnit != null,
      onQuantityChanged: (value) {
        bloc.add(TestSetNewChangeQuantity(quantity: value));
      },
      onUnitQuantityChanged: (value) => bloc.add(TestSetNewChangeQuantityUnit(quantity: value)),
      exerciseTypeId: bloc.exerciseType.exerciseTypeId,
      /* onSubmit: () {
          Navigator.of(context).pop();
          bloc.add(TestSetNewSubmit());
        } */
    );
  }
}
