import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/managers/interfaces/appointment_consultation.dart';
import 'package:get/get.dart';

class FMultiSelectPopup<T, TValue> extends FStatefulWidget {
  /// 数据源
  final List<T> source;

  /// 选中值列表
  final List<int>? value;

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

  /// 是否定制下拉UI
  final bool? isCustomMade;

  /// 弹出框的高度
  final double? height;

  FMultiSelectPopup({
    Key? key,
    required this.source,
    this.onChangeIndex,
    this.value,
    this.isCustomMade = false,
    this.height,
  }) : super(key: key);

  @override
  FState<FMultiSelectPopup> createState() => _FMultiSelectPopupState();
}

class _FMultiSelectPopupState extends FState<FMultiSelectPopup> {
  final ScrollController _scrollController = ScrollController();
  List<int> _selectIndexList = [];

  /// 这边是组件外部点击取消重置状态
  List<int> _oldSelectIndexList = [];

  final fontStyle = TextStyle(
    fontSize: 14,
  );

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: widget.height ?? 500,
      child: FAlwaysScrollListView(
        scrollController: _scrollController,
        child: QuickFWidget(ScrollConfiguration(
          behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
          child: FListView.builder(
            shrinkWrap: true,
            controller: _scrollController,
            itemCount: widget.source.length,
            itemBuilder: (BuildContext context, int index) {
              return FMaterial(
                color: Colors.white,
                child: FInkWell(
                  onTap: () {
                    setState(() {
                      if (_selectIndexList.contains(index) && index != 0) {
                        _selectIndexList.remove(index);
                      } else {
                        _selectIndexList.add(index);
                      }
                      widget.onChangeIndex!(_selectIndexList.toSet().toList());
                    });
                  },
                  child: FContainer(
                    padding: EdgeInsets.symmetric(
                      vertical: 15,
                    ),
                    margin: EdgeInsets.symmetric(
                      horizontal: 15,
                    ),
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          color: Color.fromRGBO(231, 231, 231, 1),
                          width: 0.5,
                        ),
                      ),
                    ),
                    child: _buildChoiceRow(index),
                  ),
                ),
              );
            },
          ),
        )),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _selectIndexList = widget.value ?? [];
    _oldSelectIndexList.addAll(widget.value ?? []);
    Get.find<IAppointmentConsultation>()
        .onCancelAddedParticipant
        .addListener(_onCancelAddedParticipant);
  }

  ///编辑所选的行
  FWidget _buildChoiceRow(int index) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        FExpanded(
          child: FRow(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              FRow(
                children: [
                  FColumn(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      FText(
                        widget.source[index].title,
                      ),
                    ],
                  )
                ],
              ),
              FContainer(
                width: 25,
                height: 25,
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Color.fromRGBO(141, 141, 141, 1),
                    width: 0.5,
                  ),
                ),
                child: _selectIndexList.contains(index)
                    ? FContainer(
                        decoration: BoxDecoration(
                          color: Theme.of(context).primaryColor,
                        ),
                        child: FCenter(
                          child: FContainer(
                            child: FIcon(
                              Icons.check,
                              size: 20,
                              color: Colors.white,
                            ),
                          ),
                        ),
                      )
                    : FContainer(),
              ),
            ],
          ),
        ),
        FSizedBox(
          width: 15,
        ),
      ],
    );
  }

  /// 取消事件的监听
  void _onCancelAddedParticipant(sender, e) {
    if (e) {
      widget.onChangeIndex!(
        _oldSelectIndexList.toSet().toList(),
      );
    }
  }
}
