import 'dart:math';

import 'package:cims_apps/application/assets/path_assets.dart';
import 'package:cims_apps/application/component/button/button_view.dart';
import 'package:cims_apps/application/component/image/image_view.dart';
import 'package:cims_apps/application/component/numeric_pad/numeric_pad.dart';
import 'package:cims_apps/application/component/subscribe/input_investment_view.dart';
import 'package:cims_apps/application/component/text_title/text_title.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:cims_apps/features/dashboard/dashboard_account/view/portfolio/redeem_product/view/redeem_product.dart';
import 'package:cims_apps/features/dashboard/dashboard_account/view/portfolio/redeem_product/view_model/redeem_product_view_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ChangeAmount extends StatefulWidget {
  final int totalAmount;
  const ChangeAmount({super.key, required this.totalAmount});

  @override
  State<ChangeAmount> createState() => _ChangeAmountState();
}

class _ChangeAmountState extends State<ChangeAmount> {
  TextEditingController amountController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    amountController.text = NumberFormatter.numberCurrency(widget.totalAmount, 'Rp ', 'id_ID', decimalDigits: 0);
  }

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

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => RedeemProductViewModel(),
      child: Consumer<RedeemProductViewModel>(
        builder: (context, provider, child) {
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                          showModalBottomSheet(
                            context: context,
                            isScrollControlled: true,
                            builder: (context) {
                              return const RedeemProduct();
                            },
                          );
                        },
                        child: const Icon(Icons.arrow_back_rounded)
                    ),
                    const Text('Type amount',
                      style: TextStyle(
                          fontWeight: FontWeight.w600,
                          fontSize: 16,
                          color: ColorPalette.slate800
                      ),
                    ),
                    GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: const Icon(Icons.close_rounded)
                    )
                  ],
                ),
              ),
              const Divider(height: 1, color: ColorPalette.slate200,),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: EdgeInsets.all(24),
                    child: cardProduct()
                  ),
                  InputInvestmentView(
                    minimumPrice: (provider.getCurrentProduct.priceUnit! * 1).toInt(),
                    maximumPrice: (provider.getCurrentProduct.priceUnit! * provider.getCurrentProduct.totalUnit!).toInt(),
                    currentPrice: provider.getAmount!.toInt(),
                    nextMove: (value) {
                      String formatValueInput = value.replaceAll('Rp ', '').replaceAll('.', '');
                      provider.setAmount(double.parse(formatValueInput));
                      Navigator.pop(context);
                      showModalBottomSheet(
                        context: context,
                        isScrollControlled: true,
                        builder: (context) {
                          return const RedeemProduct();
                        },
                      );
                    },
                  )
                ],
              ),
              SizedBox(height: 16,)
            ],
          );
        }
      ),
    );
  }

  Widget cardProduct() {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          border: Border.all(color: ColorPalette.slate200),
          boxShadow: [
            BoxShadow(
                color: const Color(0xff1E293B).withOpacity(0.04),
                blurRadius: 8,
                spreadRadius: 2
            )
          ]
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ImageView(
            image: PathAssets.imgProduct,
            width: SizeConfig.width * .13,
          ),
          const SizedBox(width: 8),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const TextTitle(title: 'Gemilang Dana Kas Maxima', fontSize: 16,),
                const SizedBox(height: 4),
                Container(
                  padding: const EdgeInsets.all(6),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(40),
                      color: ColorPalette.investTypeBgColor['Money Market']?.withOpacity(0.5) ?? Colors.white,
                      border: Border.all(width: 2, color: ColorPalette.investTypeColor['Money Market']?.withOpacity(0.4) ?? Colors.white)
                  ),
                  child: Text(
                    'Money Market' ?? '',
                    style: TextStyle(
                        color: ColorPalette.investTypeColor['Money Market'],
                        fontWeight: FontWeight.w600
                    ),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
