import 'package:cims_apps/application/component/button/button_view.dart';
import 'package:cims_apps/application/component/numeric_pad/numeric_pad.dart';
import 'package:cims_apps/application/theme/color_palette.dart';
import 'package:cims_apps/core/utils/number_formatter.dart';
import 'package:cims_apps/core/utils/size_config.dart';
import 'package:flutter/material.dart';

class InputInvestmentView extends StatefulWidget {
  final String selectedPlan;
  final void Function(String value) nextMove;
  const InputInvestmentView({super.key, required this.selectedPlan, required this.nextMove});

  @override
  State<InputInvestmentView> createState() => _InputInvestmentViewState();
}

class _InputInvestmentViewState extends State<InputInvestmentView> {
  TextEditingController inputController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    inputController.text = 'Rp 0';
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    inputController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(16)
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(height: 16),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(widget.selectedPlan,
                      style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.w700,

                      ),
                    ),
                    Row(
                      children: [
                        Icon(Icons.change_circle_outlined, color: ColorPalette.primary, size: 20),
                        SizedBox(width: 4),
                        Text('Change',
                          style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w600,
                              color: ColorPalette.primary
                          ),
                        )
                      ],
                    )
                  ],
                ),
                TextField(
                  controller: inputController,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      fontSize: 28,
                      fontWeight: FontWeight.w600,
                      color: ColorPalette.slate800
                  ),
                  keyboardType: TextInputType.number,
                  onChanged: (value) {
                    value = value.replaceAll('Rp ', '').replaceAll('.', '');
                    double parseValue = double.parse(value);
                    if(value.isNotEmpty){
                      inputController.text = NumberFormatter.numberCurrency(parseValue, 'Rp ', 'id_ID', decimalDigits: 0);
                    }else{
                      inputController.text = NumberFormatter.numberCurrency(0, 'Rp ', 'id_ID', decimalDigits: 0);
                    }
                  },
                  decoration: InputDecoration(
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: ColorPalette.primary,
                            width: 2
                        ),
                      )
                  ),
                ),
                SizedBox(height: 12),
                Text('Minimum Budget Rp1,000,000',
                  style: TextStyle(
                      color: ColorPalette.slate400,
                      fontSize: 16,
                      fontWeight: FontWeight.w600
                  ),
                ),
                SizedBox(height: 16),
                NumericPad(onNumberSelected: (p0) {
                  String checkIsZeroInput = inputController.text.replaceAll('Rp ', '').replaceAll(',', '');
                  String getNumeric = p0;
                  if(p0.isNotEmpty){
                    if(checkIsZeroInput != '0'){
                      getNumeric = checkIsZeroInput + getNumeric;
                    }
                  }else{
                    getNumeric = checkIsZeroInput.substring(0, checkIsZeroInput.length - 1);
                  }
                  String formatNumeric = NumberFormatter.numberCurrency(
                      double.parse(getNumeric), 'Rp ', 'id_ID', decimalDigits: 0).replaceAll('.', ',');
                  inputController.text = formatNumeric;
                }),
                SizedBox(height: 8),
                ButtonView(
                  name: 'Next',
                  onPressed: () {
                    widget.nextMove(inputController.text);
                  },
                  width: SizeConfig.width,
                  heightWrapContent: true,
                  contentPadding: EdgeInsets.symmetric(vertical: 16),
                  marginVertical: 0,
                )
              ],
            ),
          ),
        ],
      ),
    );;
  }
}
