import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:traderscircuit/controller/contact_us_controller.dart';
import 'package:traderscircuit/controller/risk_profile_controller.dart';

import '../text.dart';

class CustomDropDownWidget extends StatefulWidget {
  const CustomDropDownWidget({
    super.key,
    required this.header,
    required this.title,
    required this.listData,
    required this.type,
  });
  final String header;
  final String title;
  final List<String> listData;
  final String type;

  @override
  State<CustomDropDownWidget> createState() => _CustomDropDownWidgetState();
}

class _CustomDropDownWidgetState extends State<CustomDropDownWidget> {
  ContactUsController contactUsController = Get.put(ContactUsController());

  RiskProfileController riskProfileController =
      Get.put(RiskProfileController());

  RxBool onDropTap = false.obs;
  RxString selectedValue = "".obs;

  void updateOrAddData(String key, String value) {
    bool keyExists = false;
    for (int i = 0; i < riskProfileController.selectedData.length; i++) {
      Map<String, String> item = riskProfileController.selectedData[i];
      if (item.containsKey(key)) {
        riskProfileController.selectedData[i][key] =
            value; // Update existing value
        keyExists = true;
        break;
      }
    }

    if (!keyExists) {
      // Add new key-value pair
      riskProfileController.selectedData.add({key: value});
    }
  }

  @override
  void initState() {
    if (widget.type == "risk") {
      for (int i = 0; i < riskProfileController.selectedData.length; i++) {
        Map<String, String> item = riskProfileController.selectedData[i];
        if (item.containsKey(widget.title)) {
          selectedValue.value =
              riskProfileController.selectedData[i][widget.title]!;

          break;
        }
      }
      super.initState();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => SizedBox(
        width: Get.width,
        // height: onDropTap.value ? 350 : 55,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            InkWell(
              onTap: () {
                onDropTap.value = !onDropTap.value;
              },
              child: SizedBox(
                width: 398,
                height: 55,
                child: Stack(
                  children: [
                    Positioned(
                      left: 398,
                      top: 55,
                      child: Opacity(
                        opacity: 0.50,
                        child: Transform(
                          transform: Matrix4.identity()
                            ..translate(0.0, 0.0)
                            ..rotateZ(-3.14),
                          child: Container(
                            width: 398,
                            height: 55,
                            decoration: BoxDecoration(
                                gradient: LinearGradient(
                                  begin: const Alignment(0.98, -0.21),
                                  end: const Alignment(-0.98, 0.21),
                                  colors: [
                                    Colors.white
                                        .withOpacity(0.03999999910593033),
                                    Colors.white
                                        .withOpacity(0.05999999865889549)
                                  ],
                                ),
                                border: Border(
                                  top: onDropTap.value
                                      ? const BorderSide(
                                          width: 0, color: Color(0xFF393939))
                                      : const BorderSide(
                                          width: 0.50,
                                          color: Color(0xFF393939)),
                                  bottom: const BorderSide(
                                      width: 0.50, color: Color(0xFF393939)),
                                  left: const BorderSide(
                                    width: 0.50,
                                    color: Color(0xFF393939),
                                  ),
                                  right: const BorderSide(
                                    width: 0.50,
                                    color: Color(0xFF393939),
                                  ),
                                )),
                          ),
                        ),
                      ),
                    ),
                    Positioned(
                      left: 14,
                      top: 16,
                      child: Text(
                        widget.type == "risk"
                            ? (selectedValue.isNotEmpty
                                ? selectedValue.value
                                : widget.header)
                            : (contactUsController.selectedValue.isNotEmpty
                                ? contactUsController.selectedValue.value
                                : widget.header),
                        style: const TextStyle(
                          color: Color(0xFFADADAD),
                          fontSize: 16,
                          fontFamily: 'hiragino',
                          fontWeight: FontWeight.w400,
                          height: 0,
                        ),
                      ),
                    ),
                    Positioned(
                        right: 14,
                        top: 16,
                        child: onDropTap.value
                            ? const Icon(
                                Icons.keyboard_arrow_up_rounded,
                                color: Colors.white,
                              )
                            : const Icon(
                                Icons.keyboard_arrow_down_rounded,
                                color: Colors.white,
                              )),
                  ],
                ),
              ),
            ),
            !onDropTap.value
                ? const SizedBox()
                : Opacity(
                    opacity: 0.50,
                    child: Container(
                      width: Get.width,
                      //  height: 216,
                      decoration: ShapeDecoration(
                        gradient: LinearGradient(
                          begin: const Alignment(0.98, -0.21),
                          end: const Alignment(-0.98, 0.21),
                          colors: [
                            Colors.white.withOpacity(0.03999999910593033),
                            Colors.white.withOpacity(0.05999999865889549)
                          ],
                        ),
                        shape: const RoundedRectangleBorder(
                          side:
                              BorderSide(width: 0.50, color: Color(0xFF393939)),
                          borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(8),
                            bottomRight: Radius.circular(8),
                          ),
                        ),
                      ),
                      child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: widget.listData.length,
                          itemBuilder: (context, index) {
                            return InkWell(
                              onTap: () {
                                widget.type == "risk"
                                    ? (selectedValue.value =
                                        widget.listData[index])
                                    : (contactUsController.selectedValue.value =
                                        widget.listData[index]);
                                onDropTap.value = !onDropTap.value;
                                updateOrAddData(
                                    widget.title, widget.listData[index]);
                              },
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  Container(
                                      margin: EdgeInsets.only(
                                          left: 14,
                                          top: index == 0 ? 16 : 0,
                                          bottom: index ==
                                                  widget.listData.length - 1
                                              ? 16
                                              : 0),
                                      child:
                                          text16W400(widget.listData[index])),
                                  index == widget.listData.length - 1
                                      ? const SizedBox()
                                      : Container(
                                          width: Get.width,
                                          margin: const EdgeInsets.only(
                                              top: 16, bottom: 16),
                                          decoration: const ShapeDecoration(
                                            shape: RoundedRectangleBorder(
                                              side: BorderSide(
                                                width: 0.60,
                                                strokeAlign: BorderSide
                                                    .strokeAlignCenter,
                                                color: Color(0xFF393939),
                                              ),
                                            ),
                                          ),
                                        ),
                                ],
                              ),
                            );
                          }),
                    ),
                  ),
          ],
        ),
      ),
    );
  }
}
