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_common.dart';
import 'package:aitrainer_app/widgets/bottom_nav.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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);

    return Scaffold(
      appBar: AppBarCommonNav(),
      body: Container(
          padding: EdgeInsets.all(20),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('asset/image/WT_light_background.png'),
              fit: BoxFit.cover,
              alignment: Alignment.center,
            ),
          ),
          child: CustomScrollView(
            scrollDirection: Axis.vertical,
            slivers:
            [
              SliverGrid(
                delegate: SliverChildListDelegate(
                  [
                    FlatButton(
                      padding: EdgeInsets.all(10),
                      textColor: Colors.white,
                      color: Colors.black12,
                      focusColor: Colors.blueAccent,
                      onPressed: () =>
                      {
                        args['customerId'] = Cache().userLoggedIn.customerId,
                        Navigator.of(context).pushNamed('exerciseByPlanPage',
                          arguments: args)
                      },
                      child: Text(t("Execute My Selected Training Plan"),
                        style: TextStyle(fontSize: 18),)
                    ),

                    FlatButton(
                      padding: EdgeInsets.all(0),
                      textColor: Colors.white,
                      color: Colors.black12,
                      focusColor: Colors.blueAccent,
                      onPressed: () =>
                      {
                        args['exerciseRepository'] = exerciseRepository,
                        args['customerId'] = Cache().userLoggedIn.customerId,
                        Navigator.of(context).pushNamed('exercisePlanCustomPage',
                          arguments: args)
                      },
                      child: Text(t("Edit My Custom Plan"),
                        style: TextStyle(fontSize: 18),)
                    ),
                    FlatButton(
                      padding: EdgeInsets.all(20),
                      textColor: Colors.white,
                      color: Colors.black12,
                      focusColor: Colors.blueAccent,
                      onPressed: () =>
                      {

                      },
                      child: Text(t("Suggested Training Plan"),
                        style: TextStyle(fontSize: 18),)
                    ),
                    Stack(
                      fit: StackFit.passthrough,
                      overflow: Overflow.clip,
                      alignment: Alignment.topLeft,
                      children: [
                        Image.asset('asset/image/lock.png',
                          height: 40,
                          width: 40,
                        ),
                        FlatButton(
                          padding: EdgeInsets.all(20),
                          textColor: Colors.white,
                          color: Colors.black12,
                          focusColor: Colors.blueAccent,
                          onPressed: () =>
                          {

                          },
                          child: Text(t("My Special Plan"),
                            style: TextStyle(fontSize: 18),)
                        ),

                      ],
                    ),

                    Stack(
                      fit: StackFit.passthrough,
                      overflow: Overflow.clip,
                    children: [
                      Image.asset('asset/image/lock.png',
                        height: 40,
                        width: 40,
                      ),
                      FlatButton(
                        padding: EdgeInsets.all(20),
                        textColor: Colors.white,
                        color: Colors.black12,
                        focusColor: Colors.blueAccent,
                        onPressed: () =>
                        {

                        },
                        child: Text(t("My Arnold's Plan"),
                          style: TextStyle(fontSize: 18),)
                      ),

                      ]
                    ),

                    hiddenPlanWidget(exerciseRepository),
                    hiddenTrainingWidget(),


                  ]
              ),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  mainAxisSpacing: 20.0,
                  crossAxisSpacing: 20.0,
                  childAspectRatio: 1.2,
                ),
              )
            ]
          )
      ),
      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('exerciseByPlanPage',
            arguments: args)
        },
        child: Text(t("Execute My Trainee's Training Plan"),
          style: TextStyle(fontSize: 18),)
      );
    } else {
      return Container();
    }
  }

}