import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:fis_theme/theme.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';

// 控制面板-切换控制器
class ControlItemTypeSelector<T> extends StatefulWidget {
  const ControlItemTypeSelector({
    super.key,
    required this.title,
    required this.values,
    required this.textMap,
    required this.currentValue,
    this.onChanged,
  });

  final List<T> values;
  final T currentValue;
  final ValueChanged? onChanged;
  final Map<T, String> textMap;
  final String title;

  @override
  State<ControlItemTypeSelector> createState() =>
      _ControlItemTypeSelectorState<T>();
}

class _ControlItemTypeSelectorState<T> extends State<ControlItemTypeSelector> {
  late T _currentValue;
  List<T> values = [];

  @override
  void initState() {
    super.initState();
    _currentValue = (widget as ControlItemTypeSelector<T>).currentValue;
    values = (widget as ControlItemTypeSelector<T>).values;
  }

  @override
  void didUpdateWidget(ControlItemTypeSelector<T> oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.currentValue != _currentValue) {
      _currentValue = widget.currentValue;
    }
    if (widget.values != values) {
      values = (widget as ControlItemTypeSelector<T>).values;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
      decoration: BoxDecoration(
        color: LabColors.base200,
        borderRadius: BorderRadius.circular(3),
        border: Border.all(
          color: LabColors.base400,
          width: 1,
        ),
      ),
      child: Column(
        children: [
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              widget.title,
              style: TextStyle(
                fontSize: 12,
                height: 1,
                color: LabColors.text700,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          SizedBox(
            height: 10,
          ),
          Container(
            height: 40,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              decoration: LabBoxDecoration.operatePanel,
              child: DropdownButtonHideUnderline(
                child: DropdownButton<T>(
                    value: _currentValue,
                    itemHeight: 50,
                    dropdownColor: LabColors.base100,
                    icon: Icon(
                      Icons.keyboard_arrow_down_rounded,
                      color: LabColors.base800,
                    ),
                    style: TextStyle(
                      fontSize: 14,
                      color: LabColors.text800,
                      fontFamily: FTheme.ins.localeSetting.fontFamily,
                    ),
                    isExpanded: true,
                    elevation: 2,
                    items: values.map((T dataSource) {
                      return DropdownMenuItem<T>(
                        value: dataSource,
                        child: Text(
                          typeToString(dataSource),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                      );
                    }).toList(),
                    onChanged: (value) {
                      if (value != null) {
                        setState(() {
                          _currentValue = value;
                        });
                        widget.onChanged?.call(value);
                      }
                    }),
              ),
            ),
          ),
        ],
      ),
    );
  }

  String typeToString(T type) {
    // 查询 map 获取对应的文本
    return widget.textMap[type] ?? "";
  }
}
