import 'dart:collection';

import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:aitrainer_app/util/track.dart';
import 'package:aitrainer_app/widgets/dialog_premium.dart';
import 'package:badges/badges.dart';
import 'package:google_fonts/google_fonts.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';

class MyDevelopmentPage extends StatefulWidget {
  @override
  _MyDevelopmentPage createState() => _MyDevelopmentPage();
}

class _MyDevelopmentPage extends State<MyDevelopmentPage> with Trans {
  @override
  Widget build(BuildContext context) {
    final ExerciseRepository exerciseRepository = ExerciseRepository();
    final CustomerRepository customerRepository = CustomerRepository();
    final LinkedHashMap args = LinkedHashMap();
    setContext(context);
    double mediaWidth = MediaQuery.of(context).size.width;
    double imageWidth = (mediaWidth - 45) / 2;

    bool showExerciseLogBadge = Cache().getBadges()["exerciseLog"] != null;
    int counterExerciseLogBadge = Cache().getBadges()["exerciseLog"] != null ? Cache().getBadges()["exerciseLog"] : 0;
    bool showMuscleDevelopmentBadge = Cache().getBadges()["muscleDevelopment"] != null;
    int counterMuscleDevelopmentBadge = Cache().getBadges()["muscleDevelopment"] != null ? Cache().getBadges()["muscleDevelopment"] : 0;

    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([
                  Badge(
                      padding: EdgeInsets.all(8),
                      position: BadgePosition.topEnd(top: -5, end: -3),
                      animationDuration: Duration(milliseconds: 500),
                      animationType: BadgeAnimationType.slide,
                      badgeColor: Colors.red,
                      showBadge: showExerciseLogBadge,
                      badgeContent: Text(counterExerciseLogBadge.toString(),
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                          )),
                      child: ImageButton(
                        width: imageWidth,
                        textAlignment: Alignment.topCenter,
                        text: t("My Exercise Logs"),
                        style: GoogleFonts.robotoMono(
                            textStyle: TextStyle(
                                fontSize: 14,
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                backgroundColor: Colors.black54.withOpacity(0.4))),
                        image: "asset/image/edzesnaplom400400.jpg",
                        left: 5,
                        onTap: () => this.callBackExerciseLog(exerciseRepository, customerRepository),
                        isLocked: false,
                      )),
                  ImageButton(
                    width: imageWidth,
                    textAlignment: Alignment.topLeft,
                    text: t("My Whole Body Development"),
                    style: GoogleFonts.robotoMono(
                      textStyle: TextStyle(
                          fontSize: 14, color: Colors.white, fontWeight: FontWeight.bold, backgroundColor: Colors.black54.withOpacity(0.4)),
                    ),
                    image: "asset/image/testemfejl400x400.jpg",
                    left: 5,
                    onTap: () => {
                      if (Cache().userLoggedIn != null)
                        {
                          args['customerId'] = Cache().userLoggedIn!.customerId,
                          Navigator.of(context).pushNamed('mydevelopmentBodyPage', arguments: args)
                        }
                      else
                        {}
                    },
                    isLocked: true,
                  ),
                  /* ImageButton(
                    width: imageWidth,
                    textAlignment: Alignment.topLeft,
                    text: t("My Sizes Development"),
                    style: GoogleFonts.robotoMono(
                      textStyle: TextStyle(
                          fontSize: 14, color: Colors.white, fontWeight: FontWeight.bold, backgroundColor: Colors.black54.withOpacity(0.4)),
                    ),
                    image: "asset/image/testemfejl400x400.jpg",
                    left: 5,
                    onTap: () => {
                      if (Cache().userLoggedIn != null)
                        {
                          args['customerId'] = Cache().userLoggedIn.customerId,
                          Navigator.of(context).pushNamed('mydevelopmentSizesPage', arguments: args)
                        }
                    },
                    isLocked: true,
                  ), */
                  Badge(
                      padding: EdgeInsets.all(8),
                      position: BadgePosition.topEnd(top: -5, end: -3),
                      animationDuration: Duration(milliseconds: 500),
                      animationType: BadgeAnimationType.slide,
                      badgeColor: Colors.red,
                      showBadge: showMuscleDevelopmentBadge,
                      badgeContent: Text(counterMuscleDevelopmentBadge.toString(),
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                          )),
                      child: ImageButton(
                        width: imageWidth,
                        textAlignment: Alignment.topLeft,
                        text: t("Development Of Muscles"),
                        style: GoogleFonts.robotoMono(
                            textStyle: TextStyle(
                                fontSize: 14,
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                backgroundColor: Colors.black54.withOpacity(0.4))),
                        image: "asset/image/izomcsop400400.jpg",
                        left: 5,
                        onTap: () => {Navigator.of(context).pushNamed('mydevelopmentMusclePage', arguments: args)},
                        isLocked: true,
                      )),
                  ImageButton(
                    width: imageWidth,
                    left: 5,
                    textAlignment: Alignment.topLeft,
                    text: t("Predictions"),
                    style: GoogleFonts.robotoMono(
                        textStyle: TextStyle(
                            fontSize: 14,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            backgroundColor: Colors.black54.withOpacity(0.4))),
                    image: "asset/image/predictions.jpg",
                    onTap: () => {
                      if (Cache().userLoggedIn != null)
                        {
                          Track().track(TrackingEvent.prediction),
                          showDialog(
                              context: context,
                              builder: (BuildContext context) {
                                return DialogPremium(
                                  unlocked: Cache().hasPurchased,
                                  unlockRound: 3,
                                  function: "Predictions",
                                  unlockedText: null,
                                  onTap: () => {Navigator.of(context).pop()},
                                );
                              })
                        }
                    },
                    isLocked: true,
                  ),
                  hiddenWidget(customerRepository, exerciseRepository),
                ]),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  mainAxisSpacing: 15.0,
                  crossAxisSpacing: 15.0,
                  childAspectRatio: 1.0,
                ),
              )
            ])),
        bottomNavigationBar: BottomNavigator(bottomNavIndex: 1));
  }

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

  void callBackExerciseLog(ExerciseRepository exerciseRepository, CustomerRepository customerRepository) {
    if (Cache().userLoggedIn != null) {
      final LinkedHashMap args = LinkedHashMap();
      args['exerciseRepository'] = exerciseRepository;
      args['customerRepository'] = customerRepository;
      args['customerId'] = Cache().userLoggedIn!.customerId;
      Navigator.of(context).pushNamed('exerciseLogPage', arguments: args);
    }
  }
}
