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/util/app_localization.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/model/fitness_state.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/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';

import '../bloc/customer_change/customer_change_bloc.dart';

// ignore: must_be_immutable
class CustomerFitnessPage extends StatefulWidget {
  late _CustomerFitnessPageState _state;

  _CustomerFitnessPageState createState() {
    _state = _CustomerFitnessPageState();
    return _state;
  }
}

class _CustomerFitnessPageState extends State<CustomerFitnessPage> with Trans {
  String? selected;
  bool fulldata = false;
  late CustomerChangeBloc changeBloc;
  late double cWidth;

  @override
  Widget build(BuildContext context) {
    setContext(context);
    cWidth = MediaQuery.of(context).size.width * 0.75;
    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: 30, min: 15);
    }
    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("customerSexPage", 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: () => {
          if (!fulldata)
            {
              changeBloc.add(CustomerSaveFitness()),
            }
          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(
      scrollDirection: Axis.vertical,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SizedBox(
            height: h,
          ),
          Wrap(alignment: WrapAlignment.center, children: [
            Text(
              t("Your Fitness State"),
              textAlign: TextAlign.center,
              style: GoogleFonts.archivoBlack(
                color: Colors.white,
                fontSize: 30,
                fontWeight: FontWeight.w900,
              ),
            )
          ]),
          SizedBox(
            height: h,
          ),
          getButton("Beginner", "I am beginner", FitnessState.beginner),
          SizedBox(
            height: h,
          ),
          getButton("Intermediate", "I am intermediate", FitnessState.intermediate),
          SizedBox(
            height: h,
          ),
          getButton("Advanced", "I am advanced", FitnessState.advanced),
          SizedBox(
            height: h,
          ),
          getButton("Professional", "I am professional", FitnessState.professional),
        ],
      ),
    );
  }

  TextButton getButton(String title, String desc, String state) {
    return TextButton(
        style: TextButton.styleFrom(
          padding: EdgeInsets.all(10.0),
          shape: getShape(changeBloc, state),
        ),
        child: Container(
          width: cWidth,
          child: Column(
            children: [
              InkWell(
                child: Text(
                  AppLocalizations.of(context)!.translate(title),
                  style: TextStyle(color: Colors.white, fontSize: 32, fontFamily: 'Arial', fontWeight: FontWeight.w900),
                ),
                highlightColor: Colors.white,
              ),
              InkWell(
                child: Text(
                  AppLocalizations.of(context)!.translate(desc),
                  style: TextStyle(color: Colors.white, fontSize: 20, fontFamily: 'Arial', fontWeight: FontWeight.w100),
                ),
                highlightColor: Colors.white,
              ),
            ],
          ),
        ),
        onPressed: () => {
              changeBloc.add(CustomerFitnessChange(fitness: state)),
            });
  }

  dynamic getShape(CustomerChangeBloc changeBloc, String fitnessLevel) {
    String? selected = changeBloc.selectedFitnessItem;

    dynamic returnCode = (selected == fitnessLevel)
        ? RoundedRectangleBorder(
            side: BorderSide(
              width: 4,
              color: Color(0xffb4f500),
            ),
            borderRadius: BorderRadius.circular(12),
          )
        : RoundedRectangleBorder(
            side: BorderSide(width: 4, color: Colors.white24),
            borderRadius: BorderRadius.circular(12),
          );
    return returnCode;
  }
}
