import 'package:car_provider_app/classes/colors.dart';
import 'package:car_provider_app/theme/colors.dart';
import 'package:car_provider_app/utils/utils.dart';
import 'package:car_provider_app/widgets/extensions/string_extensions.dart';
import 'package:flutter/material.dart';

class DropValue {
  int id;
  String value;
  String subValue;

  DropValue(this.id, this.value, this.subValue);

  bool operator ==(o) => o is DropValue && o.value == value && o.id == id;
}

class DropdownField extends StatefulWidget {
  String? hint;
  List<DropValue>? list;
  DropValue? dropdownValue;
  Function(DropValue) onSelect;

  DropdownField(this.onSelect, {this.hint, this.list, this.dropdownValue});

  @override
  State<DropdownField> createState() => _DropdownFieldState();
}

class _DropdownFieldState extends State<DropdownField> {
  DropValue? dropdownValue;
  List<DropValue> defaultV = [
    new DropValue(1, "One", ""),
    new DropValue(2, "Two", ""),
  ];

  @override
  void initState() {
    super.initState();
    dropdownValue = widget.dropdownValue;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: containerColorRadiusBorderWidth(
        MyColors.white,
        0,
        MyColors.darkPrimaryColor,
        2,
      ),
      margin: EdgeInsets.all(0),
      padding: EdgeInsets.only(left: 8, right: 8),
      child: DropdownButton<DropValue>(
        value: dropdownValue??null,
        icon: const Icon(Icons.keyboard_arrow_down_sharp),
        elevation: 16,
        iconSize: 16,
        iconEnabledColor: borderColor,
        iconDisabledColor: borderColor,
        isExpanded: true,
        style: const TextStyle(color: Colors.black, fontWeight: FontWeight.w600),
        hint: (widget.hint ?? "").toText12(color: borderColor),
        underline: Container(
          height: 0,
        ),
        onChanged: (DropValue? newValue) {
          setState(() {
            dropdownValue = newValue!;
            widget.onSelect(newValue);
          });
        },
        items: (widget.list ?? defaultV).map<DropdownMenuItem<DropValue>>(
          (DropValue value) {
            return DropdownMenuItem<DropValue>(
              value: value,
              child: value.value.toText12(),
            );
          },
        ).toList(),
      ),
    );
  }
}
