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';

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

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.endurance:
        return "Endurance";
      case Goals.weight_loss:
        return "Loss Weight";
      case Goals.gain_muscle:
        return "Gain Muscle";
      case Goals.gain_strength:
        return "Gain Strength";
      case Goals.muscle_endurance:
        return "Muscle Endurance";
      case Goals.flexibility:
        return "Flexibility";
      case Goals.explosiveness:
        return "Explosiveness";
      case Goals.shape_forming:
        return "Shape Forming";
      default:
        return "Gain Muscle";
    }
  }

  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 is HashMap && args['personal_data'] != null) {
      fulldata = args['personal_data'];
      customerRepository = args['bloc'];
    } else {
      customerRepository = ModalRoute.of(context)!.settings.arguments as CustomerRepository;
    }

    PreferredSizeWidget _bar = AppBarMin(
      back: true,
    );
    if (!fulldata) {
      _bar = AppBarProgress(max: 50, min: 26);
    }

    return Scaffold(
      appBar: _bar,
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('asset/image/WT_light_background.jpg'),
            fit: BoxFit.cover,
            alignment: Alignment.center,
          ),
        ),
        height: double.infinity,
        width: double.infinity,
        child: BlocProvider(
          create: (context) => CustomerChangeBloc(customerRepository: customerRepository),
          child: Builder(builder: (context) {
            changeBloc = BlocProvider.of<CustomerChangeBloc>(context);

            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.orange,
                        fontSize: 30,
                        shadows: <Shadow>[
                          Shadow(
                            offset: Offset(2.0, 2.0),
                            blurRadius: 3.0,
                            color: Colors.black87,
                          ),
                        ],
                      ),
                    ),
                  ]),
                  Divider(),
                  getItem(changeBloc, Goals.gain_muscle),
                  Divider(),
                  getItem(changeBloc, Goals.weight_loss),
                  Divider(),
                  getItem(changeBloc, Goals.shape_forming),
                  Divider(),
                  getItem(changeBloc, Goals.endurance),
                  Divider(),
                  getItem(changeBloc, Goals.gain_strength),
                  Divider(),
                  getItem(changeBloc, Goals.muscle_endurance),
                  Divider(),
                  getItem(changeBloc, Goals.flexibility),
                  Divider(),
                  getItem(changeBloc, Goals.explosiveness),
                  Divider(),
                ],
              ),
            ));
          }),
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () => {
          //changingViewModel.saveCustomer(),
          changeBloc.add(CustomerSave()),
          Navigator.of(context).pop(),
          if (!fulldata) {Navigator.of(context).pushNamed("customerFitnessPage", arguments: changeBloc.customerRepository)}
        },
        backgroundColor: Colors.orange[800],
        icon: Icon(
          CustomIcon.save,
          size: 20,
        ),
        label: Text(
          fulldata ? t("Save") : t("Next"),
          style: GoogleFonts.inter(fontWeight: FontWeight.bold, fontSize: 12),
        ),
      ),
    );
  }

  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.yellow[300],
            fontSize: 28,
            shadows: <Shadow>[
              Shadow(
                offset: Offset(2.0, 2.0),
                blurRadius: 5.0,
                color: Colors.black87,
              ),
            ],
          ),
        ),
      )
    ]);
  }

  dynamic getShape(CustomerChangeBloc customerBloc, String goal) {
    if (customerBloc.customerRepository.goal == null) return null;
    String selectedGoal = customerBloc.customerRepository.goal!;
    dynamic returnCode = (selectedGoal == goal)
        ? RoundedRectangleBorder(
            side: BorderSide(width: 4, color: Colors.red),
          )
        : null;
    //return
    return returnCode;
  }
}
