import 'dart:collection';
import 'package:aitrainer_app/model/cache.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_nav.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 {
  @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;
    print("Media: " + mediaWidth.toString() + " imageWidth: " + imageWidth.toString());

    return Scaffold(
      appBar: AppBarNav(depth: 0),
      body: Container(
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('asset/image/WT_menu_dark.png'),
              fit: BoxFit.cover,
              alignment: Alignment.center,
            ),
          ),
          child: CustomScrollView(
            scrollDirection: Axis.vertical,
            slivers:
            [
              SliverGrid(
                delegate: SliverChildListDelegate(
                  [
                    ImageButton(
                      width: imageWidth,
                      textAlignment: Alignment.topLeft,
                      text: t("Execute My Selected Training Plan"),
                      style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(fontSize: 14, color: Colors.orange, fontWeight: FontWeight.bold,
                        backgroundColor: Colors.black54.withOpacity(0.4))),
                      image: "asset/image/exercise_plan_execute.jpg",
                      top: 130,
                      left: 10,
                      onTap:() => {
                        args['customerId'] = Cache().userLoggedIn.customerId,
                        Navigator.of(context).pushNamed('exerciseExecutePlanPage',
                          arguments: args)
                      },
                      isLocked: false,
                    ),

                    ImageButton(
                      width: imageWidth,
                      textAlignment: Alignment.topLeft,
                      text: t("Edit My Custom Plan"),
                      style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(fontSize: 14, color: Colors.orange, fontWeight: FontWeight.bold,
                        backgroundColor: Colors.black54.withOpacity(0.4))),
                      image: "asset/image/exercise_plan_custom.jpg",
                      left: 10,
                      onTap:() => {
                        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("Suggested Training Plan"),
                      style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(fontSize: 14, color: Colors.orange, fontWeight: FontWeight.bold,
                        backgroundColor: Colors.black54.withOpacity(0.4))),
                      image: "asset/image/exercise_plan_suggested.jpg",
                      left: 10,
                      onTap:() => {

                      },
                      isLocked: true,
                    ),

                    ImageButton(
                      width: imageWidth,
                      textAlignment: Alignment.topLeft,
                      text: t("My Special Plan"),
                      style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(fontSize: 14, color: Colors.orange, fontWeight: FontWeight.bold,
                        backgroundColor: Colors.black54.withOpacity(0.4))),
                      image: "asset/image/exercise_plan_special.jpg",
                      left: 10,
                      onTap:() => {

                      },
                      isLocked: true,
                    ),

                    ImageButton(
                      width: imageWidth,
                      textAlignment: Alignment.topLeft,
                      text: t("My Arnold's Plan"),
                      style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(fontSize: 14, color: Colors.orange, fontWeight: FontWeight.bold,
                        backgroundColor: Colors.black54.withOpacity(0.4))),
                      image: "asset/image/exercise_plan_stars.jpg",
                      left: 10,
                      onTap:() => {

                      },
                      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 FlatButton(
        padding: EdgeInsets.all(20),
        textColor: Colors.white,
        color: Colors.black12,
        focusColor: Colors.blueAccent,
        onPressed: () =>
        {
          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 ) {
      print ("!!Trainee: " + Cache().getTrainee().firstname + " " + Cache().getTrainee().name);
      return FlatButton(
        padding: EdgeInsets.all(20),
        textColor: Colors.white,
        color: Colors.black12,
        focusColor: Colors.blueAccent,
        onPressed: () =>
        {
          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();
    }
  }

}