import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/util/logger.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DropdownColumnWidget extends StatefulWidget {
  final List<String> listData;
  final String currentValue;
  final ValueChanged<String>? selectedValue;
  final String label;
  final Color? color;
  final String? hintText;

  const DropdownColumnWidget(
      {Key? key,
      required this.listData,
      required this.currentValue,
      required this.selectedValue,
      required this.label,
      required this.color,
      required this.hintText})
      : super(key: key);

  @override
  _DropdownColumnWidgetState createState() => _DropdownColumnWidgetState();
}

class _DropdownColumnWidgetState extends State<DropdownColumnWidget> {
  late int _selectedPos;
  late List<String> list;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initData();
  }

  void initData() {
    String firstElement = widget.hintText ?? "Chọn";
    if (!Util.isEmpty(widget.listData)) {
      try {
        if (list.last != widget.listData.last) _selectedPos ;
      } catch (e) {
        logger.e(e.toString());
      }
      list = widget.listData;
      if (list.contains(firstElement)) {
        if (_selectedPos == null) _selectedPos = list.indexOf(widget.currentValue);
        return;
      }
      list.insert(0, firstElement);

      if (_selectedPos == null) _selectedPos = list.indexOf(widget.currentValue) ;
      if (_selectedPos < 0) {
        _selectedPos = 0;
      }
      if (!Util.isEmpty(list) && widget.selectedValue != null) {
        String? selectedValue = _selectedPos == 0 ? null : list[_selectedPos];
        widget.selectedValue!(selectedValue!);
      }
    } else {
      list = []..add(firstElement);
    }
  }

  @override
  Widget build(BuildContext context) {
    initData();
    int sizeList = list.length;

    return Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
        decoration:
            BoxDecoration(borderRadius: BorderRadius.circular(5.0), border: Border.all(color: R.color.gray, width: 1)),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Visibility(
                visible: !Util.isEmpty(widget.label),
                child: Text(
                  widget.label ,
                  textAlign: TextAlign.start,
                  style: TextStyle(color: widget.color ?? R.color.grey, fontSize: 30.sp),
                )),
            Visibility(
                visible: !Util.isEmpty(widget.label),
                child: SizedBox(
                  height: 5,
                )),
            sizeList == 0
                ? Text("No data")
                : (sizeList == 1
                    ? Row(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          Expanded(
                            child: Text(list[0],
                                style: TextStyle(
                                  fontSize: 40.sp,
                                  color: R.color.grey,
                                )),
                          ),
                          Icon(
                            MdiIcons.menuDown,
                            color: R.color.grey,
                          )
                        ],
                      )
                    : DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
//                            isDense: true,
                            isExpanded: true,
                            style: TextStyle(
                              color: widget.color ?? R.color.grey,
                              fontSize: 40.sp,
                            ),
                            hint: Text(
                              widget.hintText ?? "",
                              style: TextStyle(fontSize: 40.sp, color: R.color.black),
                            ),
                            value: _selectedPos != null && _selectedPos >= 0 ? list[_selectedPos] : list[0],
                            items: list
                                .map((item) => DropdownMenuItem<String>(
                                      child: Text(
                                        item ,
                                        style: TextStyle(fontSize: 40.sp, color: widget.color ?? R.color.grey),
                                      ),
                                      value: item,
                                    ))
                                .toList(),
                            onChanged: widget.selectedValue == null
                                ? null
                                : (value) {
                                    print(value);
                                    setState(() {
                                      _selectedPos = list.indexOf(value!);
                                    });
                                    String? selectedValue = _selectedPos == 0 ? null : list[_selectedPos];
                                    widget.selectedValue!(selectedValue!);
                                  }),
                      )),
            //SizedBox(height: 10)
          ],
        ));
  }
}
