import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/page/kcal_parameter/kcal_parameter.dart';
import 'package:e_commerce_training/src/page/kcal_parameter/kcal_parameter_cubit.dart';
import 'package:e_commerce_training/src/util/navigation_util.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';

class KcalParameter extends StatefulWidget {
  @override
  _KcalParameterState createState() => _KcalParameterState();
}

class _KcalParameterState extends State<KcalParameter> {
  TextEditingController controller = TextEditingController();
  bool _value = false;
  late KcalParameterCubit cubit;

  @override
  void initState() {
    // TODO: implement initState
    AppRepository repository = AppRepository();
    cubit = KcalParameterCubit(repository);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: R.color.white.withOpacity(0.5),
      body: BlocProvider(
        create: (context) => cubit,
        child: BlocConsumer<KcalParameterCubit, KcalParameterState>(
          listener: (context, state) {
            if (state is KcalParameterFailure) Util.showErrorSnackBar(context, state.error);
            // TODO: implement listener
          },
          builder: (context, state) {
            return Center(
              child: Container(
                margin: EdgeInsets.symmetric(horizontal: 20),
                padding: EdgeInsets.symmetric(vertical: 20, horizontal: 20),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: R.color.white,
                ),
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        R.string.parameter.tr(),
                        style: TextStyle(
                          fontSize: 18.sp,
                          fontWeight: FontWeight.w600,
                          color: R.color.black,
                        ),
                      ),
                      SizedBox(height: 20),
                      Text(
                        R.string.content_parameter.tr(),
                        style: TextStyle(
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w500,
                          color: R.color.black,
                        ),
                        maxLines: 3,
                        textAlign: TextAlign.center,
                      ),
                      SizedBox(height: 20),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Column(children: [
                            SizedBox(
                              width: 140,
                              child: CupertinoTextField(
                                controller: controller,
                                decoration: BoxDecoration(color: R.color.white),
                                textAlign: TextAlign.center,
                                enableInteractiveSelection: false,
                                keyboardType: TextInputType.number,
                                inputFormatters: [FilteringTextInputFormatter.deny(RegExp(r'[-.]'))],
                                style: TextStyle(color: R.color.black, fontSize: 34, fontWeight: FontWeight.w700),
                                placeholder: '--',
                                placeholderStyle:
                                    TextStyle(color: R.color.black, fontSize: 34, fontWeight: FontWeight.w500),
                                onChanged: (value) {
                                  // setState(() {
                                  //   selectedCalo = (value == null ||
                                  //       value.isEmpty)
                                  //       ? 0
                                  //       : int.parse(value);
                                  // });
                                },
                              ),
                            ),
                            Container(height: 1, width: 72, color: R.color.gray)
                          ]),
                          Text(R.string.kcal.tr(),
                              style: TextStyle(
                                fontSize: 16,
                              )),
                          SizedBox(
                            width: 16,
                          )
                        ],
                      ),
                      SizedBox(height: 20),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            R.string.recipe.tr(),
                            style: TextStyle(
                              fontSize: 18.sp,
                              fontWeight: FontWeight.w600,
                              color: Colors.teal,
                            ),
                          ),
                          Icon(
                            MdiIcons.arrowRight,
                            size: 22.h,
                            color: Colors.teal,
                          )
                        ],
                      ),
                      SizedBox(height: 20),
                      Center(
                        child: Text(
                          R.string.meal_confirmation.tr(),
                          style: TextStyle(
                            fontSize: 18.sp,
                            fontWeight: FontWeight.w600,
                            color: R.color.black,
                          ),
                        ),
                      ),
                      SizedBox(height: 20),
                      Padding(
                        padding: EdgeInsets.only(left: 40),
                        child: buildCheck(),
                      ),
                      SizedBox(height: 20),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Expanded(
                            child: InkWell(
                              onTap: () => NavigationUtil.pop(context),
                              child: Container(
                                padding: EdgeInsets.symmetric(vertical: 10),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(20),
                                  color: R.color.grey100,
                                ),
                                child: Center(
                                  child: Text(
                                    R.string.cancel.tr(),
                                    style: TextStyle(
                                      fontSize: 18.sp,
                                      fontWeight: FontWeight.w600,
                                      color: R.color.black,
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            flex: 1,
                          ),
                          SizedBox(width: 20),
                          Expanded(
                            child: InkWell(
                              child: Container(
                                padding: EdgeInsets.symmetric(vertical: 10),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(20),
                                  color: Colors.teal,
                                ),
                                child: Center(
                                  child: Text(
                                    R.string.agree.tr(),
                                    style: TextStyle(
                                      fontSize: 18.sp,
                                      fontWeight: FontWeight.w600,
                                      color: R.color.white,
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            flex: 1,
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Widget buildCheck() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        buildRowCheck(R.string.no_snacks.tr()),
        SizedBox(height: 5),
        Container(
          margin: EdgeInsets.only(left: 15),
          height: 1,
          width: 150.w,
          color: R.color.gray,
        ),
        SizedBox(height: 5),
        buildRowCheck(R.string.breakfast_snack.tr()),
        buildRowCheck(R.string.afternoon_snack.tr()),
        buildRowCheck(R.string.supper.tr()),
      ],
    );
  }

  Widget buildRowCheck(String title) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Checkbox(
          value: _value,
          checkColor: Colors.white,
          activeColor: Colors.teal,
          onChanged: (bool? newValue) {
            setState(() {
              _value = newValue!;
            });
          },
        ),
        SizedBox(width: 5),
        Text(
          title,
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w500,
            color: R.color.black,
          ),
        ),
      ],
    );
  }
}
