import 'dart:collection';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:aitrainer_app/service/logging.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/app_bar.dart';
import 'package:aitrainer_app/widgets/bottom_nav.dart';
import 'package:aitrainer_app/widgets/dialog_premium.dart';
import 'package:aitrainer_app/widgets/image_button.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class MyExercisePlanPage extends StatefulWidget {
  @override
  _MyExercisePlanPage createState() => _MyExercisePlanPage();
}

class _MyExercisePlanPage extends State<MyExercisePlanPage> with Trans, Logging {
  @override
  Widget build(BuildContext context) {
    final ExerciseRepository exerciseRepository = ExerciseRepository();
    final LinkedHashMap args = LinkedHashMap();
    setContext(context);

    double mediaWidth = MediaQuery.of(context).size.width;
    double imageWidth = (mediaWidth - 45) / 2;

    return Scaffold(
        appBar: AppBarNav(depth: 0),
        body: Container(
            padding: EdgeInsets.all(10),
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('asset/image/WT_menu_dark.jpg'),
                fit: BoxFit.cover,
                alignment: Alignment.center,
              ),
            ),
            child: CustomScrollView(scrollDirection: Axis.vertical, slivers: [
              SliverGrid(
                delegate: SliverChildListDelegate([
                  ImageButton(
                    width: imageWidth,
                    textAlignment: Alignment.topLeft,
                    text: t("Edit My Custom Plan"),
                    style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(
                            fontSize: 14,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            backgroundColor: Colors.black54.withOpacity(0.4))),
                    image: "asset/image/exercise_plan_custom.jpg",
                    left: 5,
                    onTap: () => {
                      if (Cache().userLoggedIn != null)
                        {
                          args['exerciseRepository'] = exerciseRepository,
                          args['customerId'] = Cache().userLoggedIn!.customerId,
                          Navigator.of(context).pushNamed('exercisePlanCustomPage', arguments: args)
                        }
                    },
                    isLocked: false,
                  ),
                  ImageButton(
                    width: imageWidth,
                    textAlignment: Alignment.topLeft,
                    text: t("Execute My Selected Training Plan"),
                    style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(
                            fontSize: 14,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            backgroundColor: Colors.black54.withOpacity(0.4))),
                    image: "asset/image/exercise_plan_execute.jpg",
                    top: 130,
                    left: 5,
                    onTap: () => {
                      if (Cache().userLoggedIn != null)
                        {
                          args['customerId'] = Cache().userLoggedIn!.customerId,
                          Navigator.of(context).pushNamed('exerciseExecutePlanPage', arguments: args)
                        }
                    },
                    isLocked: false,
                  ),
                  ImageButton(
                    width: imageWidth,
                    textAlignment: Alignment.topLeft,
                    text: t("Suggested Training Plan"),
                    style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(
                            fontSize: 14,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            backgroundColor: Colors.black54.withOpacity(0.4))),
                    image: "asset/image/exercise_plan_suggested.jpg",
                    left: 2,
                    onTap: () => {
                      if (Cache().userLoggedIn != null)
                        {
                          Track().track(TrackingEvent.my_suggested_plan),
                          showDialog(
                              context: context,
                              builder: (BuildContext context) {
                                return DialogPremium(
                                  unlocked: Cache().hasPurchased,
                                  unlockRound: 2,
                                  function: "Suggested Training Plan",
                                  unlockedText: null,
                                  onTap: () => {Navigator.of(context).pop()},
                                  onCancel: () => {Navigator.of(context).pop()},
                                );
                              })
                        }
                    },
                    isLocked: true,
                  ),
                  ImageButton(
                    width: imageWidth,
                    textAlignment: Alignment.topLeft,
                    text: t("Training Programs"),
                    style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(
                            fontSize: 14,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            backgroundColor: Colors.black54.withOpacity(0.4))),
                    image: "asset/image/exercise_plan_stars.jpg",
                    left: 5,
                    onTap: () => {
                      if (Cache().userLoggedIn != null)
                        {
                          Track().track(TrackingEvent.my_special_plan),
                          showDialog(
                              context: context,
                              builder: (BuildContext context) {
                                return DialogPremium(
                                  unlocked: Cache().hasPurchased,
                                  unlockRound: 1,
                                  function: "Training Programs",
                                  unlockedText: null,
                                  onTap: () => {Navigator.of(context).pop()},
                                  onCancel: () => {Navigator.of(context).pop()},
                                );
                              })
                        }
                    },
                    isLocked: true,
                  ),
                  hiddenPlanWidget(exerciseRepository),
                  hiddenTrainingWidget(),
                ]),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  mainAxisSpacing: 15.0,
                  crossAxisSpacing: 15.0,
                  childAspectRatio: 1.0,
                ),
              )
            ])),
        bottomNavigationBar: BottomNavigator(bottomNavIndex: 2));
  }

  Widget hiddenPlanWidget(ExerciseRepository exerciseRepository) {
    final LinkedHashMap args = LinkedHashMap();
    if (Cache().getTrainee() != null) {
      return TextButton(
          style: TextButton.styleFrom(
            padding: EdgeInsets.all(20),
            primary: Colors.white,
            onSurface: Colors.black12,
          ),
          onPressed: () => {
                if (Cache().getTrainee() != null)
                  {
                    args['exerciseRepository'] = exerciseRepository,
                    args['customerId'] = Cache().getTrainee()!.customerId,
                    Navigator.of(context).pushNamed('exercisePlanCustomPage', arguments: args)
                  }
              },
          child: Text(
            t("My Trainee's Plan"),
            style: TextStyle(fontSize: 18),
          ));
    } else {
      return Container();
    }
  }

  Widget hiddenTrainingWidget() {
    final LinkedHashMap args = LinkedHashMap();
    if (Cache().getTrainee() != null) {
      log("!!Trainee: " + Cache().getTrainee()!.firstname! + " " + Cache().getTrainee()!.name!);
      return TextButton(
          style: TextButton.styleFrom(
            padding: EdgeInsets.all(20),
            primary: Colors.white,
            onSurface: Colors.black12,
          ),
          onPressed: () => {
                if (Cache().getTrainee() != null)
                  {
                    args['customerId'] = Cache().getTrainee()!.customerId,
                    Navigator.of(context).pushNamed('exerciseExecutePlanPage', arguments: args)
                  }
              },
          child: Text(
            t("Execute My Trainee's Training Plan"),
            style: TextStyle(fontSize: 18),
          ));
    } else {
      return Container();
    }
  }
}
