import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/widgets/mobile/record_refer_mobile_select_popup.dart';

/// 手机端选择器
class RecordReferSelectSheet<T, TValue> extends FStatelessWidget {
  RecordReferSelectSheet({
    Key? key,
    this.icon,
    this.onClick,
    this.hintText,
    this.value,
    this.borderText = '',
    this.suffixIcon,
    required this.optionValueExtractor,
    required this.optionLabelExtractor,
    required this.source,
    this.onChange,
    this.isCustomMade = false,
    this.tipTitle = '',
    this.inkWellTitle = '',
    this.specialWidget,
  }) : super(key: key);

  /// 数据源
  final List<T> source;

  ///图标
  final FWidget? icon;

  /// 选项值提取器
  ///
  /// [data] 选项数据源
  final TValue Function(T data) optionValueExtractor;

  /// 选项文本提取器
  ///
  /// [data] 选项数据源
  final String Function(T data) optionLabelExtractor;

  final FWidget Function(T data)? specialWidget;

  ///尾部图标
  final FWidget? suffixIcon;

  ///提示文字
  final String? hintText;

  ///点击事件
  final Function? onClick;

  ///值
  final TValue? value;

  ///边框文字
  final String? borderText;

  /// 下拉选中的值
  final ValueCallback? onChange;

  /// 是否定制
  final bool? isCustomMade;

  /// 未选择的时候提示
  final String? tipTitle;

  /// 当前数据为空的提示
  final String? inkWellTitle;

  ///区域文本内容
  FWidget _buildTextContent() {
    String valueName = '';
    if (value != null) {
      valueName = optionLabelExtractor(source
          .firstWhere((element) => optionValueExtractor(element) == value));
    }
    if (value == null)
      return FText(
        hintText!,
        style: TextStyle(
          fontSize: 14,
          color: Color.fromRGBO(155, 155, 155, 1),
        ),
      );
    else
      return FText(
        valueName,
        style: TextStyle(
          fontSize: 14,
          color: Colors.black,
        ),
        textAlign: TextAlign.right,
        overflow: TextOverflow.ellipsis,
        maxLines: 1,
      );
  }

  @override
  FWidget build(BuildContext context) {
    int index = -1;
    return FMaterial(
      child: FInk(
        child: FInkWell(
          onTap: () {
            if (source.length == 0) {
              PromptBox.toast(
                inkWellTitle ?? '',
              );
              return;
            } else
              showModalBottomSheet(
                isDismissible: false,
                builder: (BuildContext context) {
                  return FContainer(
                    child: FColumn(
                      children: [
                        FContainer(
                          decoration: BoxDecoration(
                            border: Border(
                              bottom: BorderSide(
                                color: Color.fromRGBO(211, 211, 211, 0.3),
                              ),
                            ),
                          ),
                          child: FRow(
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              _buildConfirmOrCancelInkWell(
                                title: i18nBook.common.cancel.t,
                                onTap: () {
                                  Navigator.of(context).pop();
                                },
                              ),
                              _buildConfirmOrCancelInkWell(
                                title: i18nBook.common.confirm.t,
                                onTap: () {
                                  if (index < 0) {
                                    PromptBox.toast(tipTitle ?? '');
                                  } else {
                                    onChange?.call(
                                        optionValueExtractor(source[index]));
                                    Navigator.of(context).pop();
                                  }
                                },
                                textColor: FTheme.ins.colorScheme.primary,
                              ),
                            ],
                          ),
                        ),
                        FExpanded(
                          child: RecordReferMobileSelectPopup<T, TValue>(
                            source: source,
                            value: value,
                            optionValueExtractor: optionValueExtractor,
                            optionLabelExtractor: optionLabelExtractor,
                            isCustomMade: isCustomMade,
                            specialWidget: (data) => specialWidget != null
                                ? specialWidget!(data)
                                : null,
                            onChangeIndex: (value) => {index = value},
                          ),
                        ),
                      ],
                    ),
                  );
                },
                context: context,
              );
          },
          child: FColumn(
            children: [
              FContainer(
                height: 50,
                child: FContainer(
                  child: FRow(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      FContainer(
                        alignment: Alignment.centerRight,
                        child: _buildTextContent(),
                      ),
                      suffixIcon ??
                          FIcon(
                            Icons.chevron_right,
                            color: Color.fromRGBO(164, 164, 164, 1),
                          ),
                    ],
                  ),
                ),
              ),
              FContainer(
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: Color.fromRGBO(245, 245, 245, 1),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  FWidget _buildConfirmOrCancelInkWell({
    Color? textColor,
    required String title,
    required Function onTap,
  }) {
    return FInkWell(
      child: FContainer(
        padding: EdgeInsets.all(15),
        child: FText(
          title,
          style: TextStyle(
            color: textColor,
          ),
        ),
      ),
      onTap: () => onTap.call(),
    );
  }
}
