import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/core/utils/app_utils.dart';
import 'package:cargocalculaterapp/core/widgets/loading/progress_hud.dart';
import 'package:cargocalculaterapp/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';

import '../../../../core/widgets/drop_down/custom_drop_down.dart';
import '../../../../core/widgets/text_filds/custom_text_field_name.dart';
import '../bloc/calculator_bloc.dart';
import '../mixin/calculator_mixin.dart';

class CalculatorPage extends StatefulWidget {
  const CalculatorPage({super.key});

  @override
  State<CalculatorPage> createState() => _CalculatorPageState();
}

class _CalculatorPageState extends State<CalculatorPage> with CalculatorMixin {
  @override
  void initState() {
    initControllers();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<CalculatorBloc, CalculatorState>(
      listener: (context, state) {
        if ((state.weight ?? 0) > 0 && (state.size ?? 0) > 0) {
          averageWeightController.text =
              "${(state.weight ?? 0) / (state.size ?? 1)}";
        }
      },
      builder: (context, state) {
        return Scaffold(
          backgroundColor: context.color.grayBackground,
          appBar: AppBar(title: Text(AppLocalization.current.calculator)),
          body: ModalProgressHUD(
            inAsyncCall: state.isLoading,
            child: ListView(
              padding: AppUtils.kPaddingAll16,
              children: [
                CustomTextFieldName(
                  hint: AppLocalization.current.weight,
                  name: AppLocalization.current.weight_in_kg,
                  isRequired: true,
                  format: [
                    FilteringTextInputFormatter.allow(
                      RegExp(r'^\d*[.,]?\d{0,}$'), // accepts both "." and ","
                    ),
                  ],
                  controller: weightController,
                  inputType: const TextInputType.numberWithOptions(
                    decimal: true,
                  ),
                  onchange: (value) {
                    context.read<CalculatorBloc>().add(
                      WeightSizeEnterEvent(
                        weight: value,
                        size: sizeController.text,
                      ),
                    );
                  },
                ),
                AppUtils.kBoxHeight16,
                CustomTextFieldName(
                  hint: AppLocalization.current.size,
                  name: AppLocalization.current.size_in_m3,
                  isRequired: true,
                  controller: sizeController,
                  format: [
                    FilteringTextInputFormatter.allow(
                      RegExp(r'^\d*[.,]?\d{0,}$'), // accepts both "." and ","
                    ),
                  ],
                  inputType: const TextInputType.numberWithOptions(
                    decimal: true,
                  ),
                  onchange: (value) {
                    context.read<CalculatorBloc>().add(
                      WeightSizeEnterEvent(
                        weight: weightController.text,
                        size: value,
                      ),
                    );
                  },
                ),
                AppUtils.kBoxHeight16,
                CustomTextFieldName(
                  hint: AppLocalization.current.average_weight,
                  name: AppLocalization.current.average_weight_in,
                  controller: averageWeightController,
                  readOnly: true,
                ),
                AppUtils.kBoxHeight16,
                CustomTextFieldName(
                  hint: AppLocalization.current.product_name,
                  isRequired: true,
                  name: AppLocalization.current.product_name,
                  controller: productNameController,
                  inputType: TextInputType.name,
                ),
                AppUtils.kBoxHeight16,
                CustomDropDown(
                  isRequired: false,
                  value: state.selectedWarehouse,

                  onChange: (value) {
                    context.read<CalculatorBloc>().add(
                      WareHouseSelectEvent(wareHouse: value ?? ""),
                    );
                  },
                  name: AppLocalization.current.select_warehouse,
                ),
                AppUtils.kBoxHeight16,
                ElevatedButton(
                  onPressed: ((state.weight ?? 0) > 0 && (state.size ?? 0) > 0)
                      ? () {
                          context.read<CalculatorBloc>().add(
                            CalculateEvent(
                              productName: productNameController.text,
                            ),
                          );
                        }
                      : null,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      SvgPicture.asset("assets/svg/ic_calculator_1.svg"),
                      AppUtils.kBoxWith8,
                      Text(AppLocalization.current.calculate),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
