import 'dart:math';

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? currentPlan;
  final void Function()? changePlan;
  final int? currentPrice;
  final int? minimumPrice;
  final int? maximumPrice;
  final void Function(String value) nextMove;
  const InputInvestmentView({super.key, required this.nextMove, this.currentPlan, this.minimumPrice, this.maximumPrice, this.currentPrice, this.changePlan});

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

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

  void validationInputValue(String currentValue) {
    currentValue = currentValue.replaceAll('Rp ', '').replaceAll('.', '');
    if(currentValue.isEmpty){
      currentValue = '0';
    }
    double parseValue = double.parse(currentValue);
    if(widget.minimumPrice != null){
      if(parseValue <= widget.minimumPrice!){
        parseValue = widget.minimumPrice!.toDouble();
      }
    }

    if(widget.maximumPrice != null){
      if(parseValue >= widget.maximumPrice!){
        parseValue = widget.maximumPrice!.toDouble();
      }
    }

    inputController.text = NumberFormatter.numberCurrency(parseValue, 'Rp ', 'id_ID', decimalDigits: 0);
  }

  @override
  void initState() {
    // TODO: implement initState
    if(widget.currentPrice != null){
      inputController.text = NumberFormatter.numberCurrency(widget.currentPrice, 'Rp ', 'id_ID', decimalDigits: 0);
    }else{
      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: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                if(widget.currentPlan != null)
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(widget.currentPlan ?? '',
                        style: const TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.w700,
                        ),
                      ),
                      InkWell(
                        borderRadius: BorderRadius.circular(16),
                        onTap: widget.changePlan,
                        child: const 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: const TextStyle(
                      fontSize: 28,
                      fontWeight: FontWeight.w600,
                      color: ColorPalette.slate800
                  ),
                  keyboardType: TextInputType.number,
                  onChanged: (value) {
                    validationInputValue(value);
                  },
                  decoration: const InputDecoration(
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: ColorPalette.primary,
                            width: 2
                        ),
                      )
                  ),
                ),
                const SizedBox(height: 12),
                if(widget.minimumPrice != null)
                  Text('Minimum ${NumberFormatter.numberCurrency(widget.minimumPrice, 'Rp ', 'id_ID')}',
                    style: const TextStyle(
                        color: ColorPalette.slate400,
                        fontSize: 16,
                        fontWeight: FontWeight.w600
                    ),
                  ),
                if(widget.maximumPrice != null)
                  Text('Maximum ${NumberFormatter.numberCurrency(widget.maximumPrice, 'Rp ', 'id_ID')}',
                    style: const TextStyle(
                        color: ColorPalette.slate400,
                        fontSize: 16,
                        fontWeight: FontWeight.w600
                    ),
                  ),
                const SizedBox(height: 16),
                NumericPad(onNumberSelected: (p0) {
                  String currentValue = inputController.text;
                  if(p0.isNotEmpty){
                    if(currentValue != '0'){
                      currentValue = currentValue + p0;
                    }
                  }else{
                    currentValue = currentValue.substring(0, currentValue.length - 1);
                  }
                  validationInputValue(currentValue);
                }),
                const SizedBox(height: 24),
                ButtonView(
                  name: 'Next',
                  onPressed: () {
                    widget.nextMove(inputController.text);
                  },
                  width: SizeConfig.width,
                  heightWrapContent: true,
                  contentPadding: const EdgeInsets.symmetric(vertical: 16),
                  marginVertical: 0,
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}
