import 'dart:collection';

import 'package:aitrainer_app/bloc/customer_change/customer_change_bloc.dart';
import 'package:aitrainer_app/library/custom_icon_icons.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/util/trans.dart';
import 'package:aitrainer_app/widgets/app_bar_min.dart';
import 'package:aitrainer_app/widgets/app_bar_progress.dart';
import 'package:flutter/cupertino.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';

enum Goals { gain_muscle, muscle_endurance, gain_strength, shape_forming } //weight_loss, endurance, explosiveness,flexibility,

extension GoalsExt on Goals {
  String toStr() => this.toString().split(".").last;
  bool equalsTo(Goals goal) => this.toString() == goal.toString();
  bool equalsStringTo(String goal) => this.toStr() == goal;

  String description(Goals goal) {
    switch (goal) {
      case Goals.gain_muscle:
        return "Gain Muscle";
      case Goals.gain_strength:
        return "Gain Strength";
      case Goals.muscle_endurance:
        return "Muscle Endurance";

      case Goals.shape_forming:
        return "Shape Forming";
      default:
        return "Gain Muscle";
    }
  }

  /*
  case Goals.endurance:
        return "Endurance";
      case Goals.weight_loss:
        return "Loss Weight";
        case Goals.explosiveness:
        return "Explosiveness";
         case Goals.flexibility:
        return "Flexibility";

  */

  Goals getGoal(Goals goal) {
    Goals selected = Goals.gain_muscle;
    Goals.values.forEach((element) {
      if (goal.equalsTo(element)) {
        selected = element;
      }
    });
    return selected;
  }
}

// ignore: must_be_immutable
class CustomerGoalPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _CustomerGoalPage();
}

class _CustomerGoalPage extends State<CustomerGoalPage> with Trans {
  String? selected;
  bool fulldata = false;
  late CustomerChangeBloc changeBloc;

  @override
  Widget build(BuildContext context) {
    setContext(context);

    CustomerRepository customerRepository;
    dynamic args = ModalRoute.of(context)!.settings.arguments;
    if (args != null) {
      if (args is HashMap && args['personal_data'] != null) {
        fulldata = args['personal_data'];
        customerRepository = args['bloc'];
      } else {
        customerRepository = ModalRoute.of(context)!.settings.arguments as CustomerRepository;
      }
    } else {
      customerRepository = CustomerRepository();
    }
    PreferredSizeWidget _bar = AppBarMin(
      back: true,
    );
    if (!fulldata) {
      _bar = AppBarProgress(max: 14, min: 0);
    }

    return Scaffold(
      appBar: _bar,
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('asset/image/WT_plainblack_background.jpg'),
            fit: BoxFit.cover,
            alignment: Alignment.center,
          ),
        ),
        height: double.infinity,
        width: double.infinity,
        child: BlocProvider(
          create: (context) => CustomerChangeBloc(customerRepository: customerRepository),
          child: BlocConsumer<CustomerChangeBloc, CustomerChangeState>(
            listener: (context, state) {
              if (state is CustomerSaveError) {
                ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(backgroundColor: Colors.orange, content: Text(state.message, style: TextStyle(color: Colors.white))));
              } else if (state is CustomerSaveSuccess) {
                if (fulldata) {
                  Navigator.of(context).pop();
                } else {
                  Navigator.of(context).popAndPushNamed("customerFitnessPage", arguments: changeBloc.customerRepository);
                }
              }
            },
            builder: (context, state) {
              changeBloc = BlocProvider.of<CustomerChangeBloc>(context);
              return ModalProgressHUD(
                child: getPage(),
                inAsyncCall: state is CustomerChangeLoading,
                opacity: 0.5,
                color: Colors.black54,
                progressIndicator: CircularProgressIndicator(),
              );
            },
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () => {
          print("Fulldata: $fulldata bloc $changeBloc"),
          if (!fulldata)
            {
              print("Savegoal"),
              changeBloc.add(CustomerSaveGoal()),
            }
          else
            {
              changeBloc.add(CustomerSave()),
            }
        },
        backgroundColor: Colors.orange[600],
        icon: Icon(
          CustomIcon.save,
          color: Colors.white,
          size: 26,
        ),
        label: Text(
          fulldata ? t("Save") : t("Next"),
          style: GoogleFonts.inter(fontWeight: FontWeight.bold, fontSize: 18, color: Colors.white),
        ),
      ),
    );
  }

  Widget getPage() {
    final double h = 27;
    return SingleChildScrollView(
        child: Center(
      child: Column(
        children: [
          Divider(),
          Wrap(alignment: WrapAlignment.center, children: [
            Text(
              t("Set Your Primary Goal"),
              maxLines: 2,
              textAlign: TextAlign.center,
              style: GoogleFonts.archivoBlack(
                color: Colors.white,
                fontSize: 30,
                shadows: <Shadow>[
                  Shadow(
                    offset: Offset(2.0, 2.0),
                    blurRadius: 3.0,
                    color: Colors.black87,
                  ),
                ],
              ),
            ),
          ]),
          SizedBox(
            height: h,
          ),
          getItem(changeBloc, Goals.gain_muscle),
          SizedBox(
            height: h,
          ),
          /*  getItem(changeBloc, Goals.weight_loss),
          SizedBox(
            height: h,
          ), */
          getItem(changeBloc, Goals.shape_forming),
          SizedBox(
            height: h,
          ),
          /* getItem(changeBloc, Goals.endurance),
          SizedBox(
            height: h,
          ), */
          getItem(changeBloc, Goals.gain_strength),
          SizedBox(
            height: h,
          ),
          getItem(changeBloc, Goals.muscle_endurance),
          SizedBox(
            height: h,
          ),
          /*   getItem(changeBloc, Goals.flexibility),
          SizedBox(
            height: h,
          ),
          getItem(changeBloc, Goals.explosiveness),
          SizedBox(
            height: h,
          ), */
        ],
      ),
    ));
  }

  Widget getItem(CustomerChangeBloc changeBloc, Goals goal) {
    return Stack(alignment: Alignment.bottomLeft, children: [
      TextButton(
          style: TextButton.styleFrom(
            padding: EdgeInsets.all(0.0),
            shape: getShape(changeBloc, goal.toStr()),
          ),
          child: Image.asset(
            "asset/image/" + goal.toStr() + ".jpg",
            height: 180,
          ),
          onPressed: () => {
                setState(() {
                  selected = goal.toStr();
                  changeBloc.add(CustomerGoalChange(goal: goal.toStr()));
                }),
              }),
      Container(
        padding: EdgeInsets.only(bottom: 5, left: 10),
        child: Text(
          t(goal.description(goal)),
          style: GoogleFonts.archivoBlack(
            color: Colors.white,
            fontSize: 28,
            shadows: <Shadow>[
              Shadow(
                offset: Offset(2.0, 2.0),
                blurRadius: 5.0,
                color: Colors.black87,
              ),
            ],
          ),
        ),
      )
    ]);
  }

  dynamic getShape(CustomerChangeBloc customerBloc, String goal) {
    dynamic baseCode = RoundedRectangleBorder(
      side: BorderSide(width: 2, color: Colors.white24),
      borderRadius: BorderRadius.circular(12),
    );
    if (customerBloc.customerRepository.goal == null) return baseCode;
    String selectedGoal = customerBloc.customerRepository.goal!;
    dynamic returnCode = (selectedGoal == goal)
        ? RoundedRectangleBorder(
            side: BorderSide(width: 6, color: Color(0xffb4f500)),
            borderRadius: BorderRadius.circular(12),
          )
        : baseCode;
    //return
    return returnCode;
  }
}