import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/remote_controller/widgets/remote_controller_parameters/remote_controller_parameters_styles.dart';
import 'package:flyinsono/views/desktop/remote_maintenance/widgets/apiinfo.dart';
import 'package:flyinsono/views/desktop/remote_maintenance/widgets/single_select.dart';
import 'package:fis_theme/theme.dart';

class CustomValueSelect extends FStatelessWidget {
  final String title;
  final ApiInfo data;
  final double width;
  final double height;
  final BoxDecoration? decoration;
  final Color? textColor;
  final ValueCallback onChanged;
  List<FSelectModel> selectDatas = [];

  CustomValueSelect({
    super.key,
    required this.title,
    required this.data,
    required this.onChanged,
    this.height = 25,
    this.width = 120,
    this.textColor,
    this.decoration,
  });

  ///背景色
  Color get background => TuningParametersStyles.backgroundColor;

  @override
  FWidget build(BuildContext context) {
    List<String> valuesMapString = data.valuesMapString?.split(';') ?? [];
    String displayValue = data.displayValue ?? '';
    List<String> items = [];
    for (int i = 0; i < valuesMapString.length; i++) {
      var name = valuesMapString[i].split(':')[0];
      selectDatas.add(
        FSelectModel(
          name: name,
          code: i.toString(),
        ),
      );
      items.add(name);
    }
    return FContainer(
      decoration: decoration ??
          BoxDecoration(
            color: Colors.white,
            border: Border.all(
              color: Colors.white,
              width: 0.5,
            ),
            borderRadius: BorderRadius.circular(8),
          ),
      child: FColumn(
        children: [
          FContainer(
            width: width,
            height: height,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  color: Color.fromRGBO(245, 245, 245, 1),
                ),
              ),
            ),
            child: FText(
              title,
              textAlign: TextAlign.center,
              style: TextStyle(
                color: textColor,
                fontFamily: FTheme.ins.localeSetting.fontFamily,
              ),
            ),
          ),
          FContainer(
            width: width,
            height: height,
            child: FSingleSelected(
              items: items,
              width: width,
              textColor: textColor,
              selectedValue: displayValue,
              backgroundColor: background,
              onChanged: (value) async {
                int valueIndex =
                    items.indexWhere((element) => element == value);
                onChanged.call(valueIndex);
              },
              onSelectedValoue: (value) async {},
            ),
          ),
        ],
      ),
    );
  }
}
