import 'dart:collection';

import 'package:aitrainer_app/bloc/training_plan/training_plan_bloc.dart';
import 'package:aitrainer_app/library/custom_icon_icons.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/customer_training_plan_details.dart';
import 'package:aitrainer_app/util/trans.dart';
import 'package:aitrainer_app/widgets/app_bar.dart';
import 'package:aitrainer_app/widgets/exercise_save.dart';
import 'package:aitrainer_app/widgets/menu_image.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 TrainingPlanExercise extends StatelessWidget with Trans {
  @override
  Widget build(BuildContext context) {
    final HashMap args = ModalRoute.of(context)!.settings.arguments as HashMap;
    final CustomerTrainingPlanDetails detail = args['customerTrainingPlanDetails'];
    // ignore: close_sinks
    final TrainingPlanBloc bloc = BlocProvider.of<TrainingPlanBloc>(context);

    setContext(context);
    return Scaffold(
        appBar: AppBarNav(
            depth: 1,
            onTap: () => {
                  print("back"),
                  bloc.add(
                    TrainingPlanChangeCancel(detail: detail),
                  )
                }),
        body: Container(
          height: double.infinity,
          width: double.infinity,
          //padding: EdgeInsets.all(20),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("asset/image/WT_black_background.jpg"),
              fit: BoxFit.cover,
              alignment: Alignment.center,
            ),
          ),
          child: BlocConsumer<TrainingPlanBloc, TrainingPlanState>(listener: (context, state) {
            if (state is TrainingPlanError) {
              ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(backgroundColor: Colors.orange, content: Text(state.message, style: TextStyle(color: Colors.white))));
            }
          }, builder: (context, state) {
            return ModalProgressHUD(
              child: getExercises(bloc, detail),
              inAsyncCall: state is TrainingPlanExerciseLoading,
              opacity: 0.5,
              color: Colors.black54,
              progressIndicator: CircularProgressIndicator(),
            );
          }),
        ),
        floatingActionButton: Row(mainAxisAlignment: MainAxisAlignment.end, children: [
          FloatingActionButton.extended(
            heroTag: "skipButton",
            onPressed: () => {
              Navigator.of(context).pop(),
              bloc.add(TrainingPlanSkipExercise(detail: detail)),
            },
            backgroundColor: Colors.grey[700],
            icon: Icon(Icons.skip_next),
            label: Text(
              t("Skip"),
              style: GoogleFonts.inter(fontWeight: FontWeight.normal, fontSize: 14),
            ),
          ),
          SizedBox(
            width: 20,
          ),
          FloatingActionButton.extended(
            heroTag: "saveButton",
            onPressed: () => {
              Navigator.of(context).pop(),
              if (detail.weight != -3)
                {
                  bloc.add(TrainingPlanSaveExercise(detail: detail)),
                }
              else
                {
                  bloc.add(TrainingPlanFinishDropset(detail: detail)),
                }
            },
            backgroundColor: Colors.orange[800],
            icon: Icon(CustomIcon.save),
            label: Text(
              t("Done"),
              style: GoogleFonts.inter(fontWeight: FontWeight.bold, fontSize: 16),
            ),
          ),
        ]));
  }

  Widget getExercises(TrainingPlanBloc bloc, CustomerTrainingPlanDetails detail) {
    int? originalQuantity = bloc.trainingPlanRepository.getOriginalRepeats(bloc.getMyPlan()!.trainingPlanId!, detail);
    if (detail.weight != -3) {
      return ExerciseSave(
        exerciseName: detail.exerciseType!.nameTranslation,
        exerciseDescription: detail.exerciseType!.descriptionTranslation,
        unit: detail.exerciseType!.unit,
        unitQuantityUnit: detail.exerciseType!.unitQuantityUnit,
        hasUnitQuantity: detail.exerciseType!.unitQuantityUnit != null,
        weight: detail.weight,
        repeats: detail.repeats == -1 ? 99 : detail.repeats,
        set: detail.set,
        exerciseNr: detail.exercises.length + 1,
        onUnitQuantityChanged: (value) => bloc.add(TrainingPlanWeightChangeRecalculate(weight: value, detail: detail)),
        onQuantityChanged: (value) => bloc.add(TrainingPlanRepeatsChange(repeats: value.toInt(), detail: detail)),
        exerciseTypeId: detail.exerciseType!.exerciseTypeId,
        originalQuantity: originalQuantity,
        tip: ActivityDone.exerciseSaveTrainingTip,
        menuImage: MenuImage(
          imageName: bloc.getActualImageName(detail.exerciseType!.exerciseTypeId),
          workoutTreeId: bloc.getActualWorkoutTreeId(detail.exerciseType!.exerciseTypeId)!,
          radius: 0,
          filter: false,
        ),
      );
    } else {
      return getDropSet(bloc, detail);
    }
  }

  Widget getDropSet(TrainingPlanBloc bloc, CustomerTrainingPlanDetails detail) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage("asset/image/drop_set.png"),
          //fit: BoxFit.cover,
          alignment: Alignment.center,
        ),
      ),
      padding: EdgeInsets.only(top: 20, left: 30),
      child: RichText(
          text: TextSpan(
        style: GoogleFonts.inter(
          fontSize: 16,
          fontWeight: FontWeight.normal,
          color: Colors.yellow[300],
        ),
        children: [
          TextSpan(
            text: t("Drop Set"),
            style: GoogleFonts.inter(
              fontSize: 24,
              fontWeight: FontWeight.bold,
              color: Colors.yellow[100],
            ),
          ),
          TextSpan(
            text: "\n",
          ),
          TextSpan(
            text: "\n",
          ),
          TextSpan(text: t("Execute at least 3 sets with maximum repeats, without resting time, with decreasing the weight.")),
          TextSpan(
            text: "\n",
          ),
          TextSpan(
            text: "\n",
          ),
          TextSpan(
            text: t("The goal is to completly exhaust your muscle without lifting a ridiculous weight end the end."),
            style: GoogleFonts.inter(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: Colors.yellow[100],
            ),
          ),
        ],
      )),
    );
  }
}
