import 'dart:convert';
import 'dart:math';

import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_lib_report/report_info/report_info.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:fis_ui/widgets/tree/flutter_treeview/lib/flutter_treeview.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/services/entity/default_entry_setting.dart';
import 'package:flyinsono/architecture/services/entity/report_editor_setting.dart';
import 'package:flyinsono/architecture/services/modules/report_editor_configuration.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/widgets/expandable_icon_text.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/index.dart';
import 'package:flyinsono/pages/user/widgets/search_input.dart';
// import 'package:flyinsono/pages/remedical/vocabulary_entry/view.dart';
import 'package:get/get.dart' hide Node;
import 'package:path/path.dart';

///报告中词条选择
class VocabularyEntryReport extends FStatefulWidget implements FPage {
  @override
  FState<VocabularyEntryReport> createState() => _VocabularyEntryReportState();

  ///关闭事件
  final void Function() onClose;

  VocabularyEntryReport({
    required this.onClose,
  });

  @override
  final String pageName = 'VocabularyEntryReport';
}

class _VocabularyEntryReportState extends FState<VocabularyEntryReport> {
  /// 词条树顶层结构
  late List<Node<dynamic>> treeViewTopData = [];
  late TextEditingController _textEditingController =
      TextEditingController(text: keyWords);
  final entrysScrollController = ScrollController();

  double _maxHeight = 0;
  double _maxWidth = 0;

  final double _scale = 0.39;

  /// 当前选中项
  String selectedKey = '';

  /// 关键字搜索
  String keyWords = '';

  /// 当前词条分类的词条卡片内容
  List<ThesaurusItemDTO> vocabularyCard = [];

  /// 序列化存储的词条列表
  List<dynamic> defaultThesaurusList = [];

  /// 树形选择器控制器
  TreeViewController _treeViewController = TreeViewController();

  /// 本地报告编辑器服务
  LocalReportEditorService localReportEditorService =
      Get.find<LocalReportEditorService>();

  ReportEditorSetting reportEditorSetting = ReportEditorSetting.ins;

  String? _focusItem;

  void _listenKeyboardEvent(RawKeyEvent event) {
    if (event.isKeyPressed(LogicalKeyboardKey.enter) ||
        event.isKeyPressed(LogicalKeyboardKey.numpadEnter)) {
      getEntryByKeyWords();
    }
  }

  /// 报告控制器
  final reportEditController = Get.put(ReportEditController());

  /// 检查是否需要重新打开上次打开的节点
  void checkReopenNode() {
    /// 上次打开的词条分类和词条项
    final lastEntryCategoryCode = reportEditorSetting.lastEntryCategoryCode;
    final lastEntryCode = reportEditorSetting.lastEntryCode;
    var entryCategoryCodeToReopen = '';
    var entryCodeToReopen = '';

    treeViewTopData.forEach((element) {
      if (element.key == lastEntryCategoryCode) {
        entryCategoryCodeToReopen = element.key;
      }
      element.children.forEach((child) {
        if (child.key == lastEntryCode) {
          entryCodeToReopen = child.key;
        }
      });
    });
    if (entryCategoryCodeToReopen == '') return;
    List<Node> newdata =
        _treeViewController.expandToNode(entryCategoryCodeToReopen);
    if (entryCodeToReopen == '') {
      _treeViewController = _treeViewController.copyWith(children: newdata);
    } else {
      _treeViewController = _treeViewController.copyWith(
          children: newdata, selectedKey: entryCodeToReopen);
      vocabularyCard = reportEditController.state.allThesaurusList
          .where((element) => element.parentItemCode == entryCodeToReopen)
          .toList();
    }
  }

  /// 下拉项点击
  void onExpansionChanged(String code, bool isDrop) async {
    reportEditorSetting.setLastEntryCategoryCode(code);
    localReportEditorService.save();
    if (isDrop) {
      List<Node> newdata = _treeViewController.expandToNode(code);
      _treeViewController = _treeViewController.copyWith(children: newdata);
    } else {
      List<Node> newdata = _treeViewController.collapseToNode(code);
      _treeViewController = _treeViewController.copyWith(children: newdata);
    }
    setState(() {});
  }

  /// 词条分类的选中
  void onNodeTap(String code) {
    reportEditorSetting.setLastEntryCode(code);
    localReportEditorService.save();
    _treeViewController = _treeViewController.copyWith(selectedKey: code);
    if (reportEditController.state.allThesaurusList.isNotEmpty) {
      vocabularyCard = reportEditController.state.allThesaurusList
          .where((element) => element.parentItemCode == code)
          .toList();
    }
    setState(() {
      selectedKey = code;
    });
  }

  /// 搜索的方法
  void getEntryByKeyWords() {
    List<ThesaurusItemDTO> thesaurusItem = [];
    var allThesaurusList = reportEditController.state.allThesaurusList;
    allThesaurusList.forEach(
      (e) {
        if (e.thesaurusItemName?.contains(keyWords) == true ||
            e.thesaurusItemConclusion?.contains(keyWords) == true ||
            e.thesaurusItemDescription?.contains(keyWords) == true) {
          if (e.parentItemCode != '') {
            allThesaurusList.forEach(
              (element) {
                if (element.thesaurusItemCode == e.parentItemCode) {
                  thesaurusItem.add(element);
                  if (element.parentItemCode != '') {
                    allThesaurusList.forEach(
                      (key) {
                        if (key.thesaurusItemCode == element.parentItemCode) {
                          thesaurusItem.add(key);
                        }
                      },
                    );
                  }
                }
              },
            );
          }
          thesaurusItem.add(e);
        }
      },
    );
    thesaurusItem = thesaurusItem.toSet().toList();
    treeViewTopData = getThesaurusItemsAsyncList(thesaurusItem);
    setState(() {
      _treeViewController = TreeViewController(
        children: treeViewTopData,
        selectedKey: selectedKey,
      );
    });
  }

  void _initThesaurusList() {
    treeViewTopData = getThesaurusItemsAsyncList(
      reportEditController.state.allThesaurusList,
    );
    if (treeViewTopData.isNotEmpty) {
      var firstThesaurus = treeViewTopData.first;
      treeViewTopData[0] = firstThesaurus.copyWith(
        expanded: true,
      );
      if (firstThesaurus.children.isNotEmpty) {
        selectedKey = firstThesaurus.children.first.key;
      } else {
        selectedKey = firstThesaurus.key;
      }
    }
    _treeViewController = TreeViewController(
      children: treeViewTopData,
      selectedKey: selectedKey,
    );

    vocabularyCard = reportEditController.state.allThesaurusList
        .where((element) => element.parentItemCode == selectedKey)
        .toList();
  }

  /// 数据处理
  List<Node> getThesaurusItemsAsyncList(
    List<ThesaurusItemDTO> allThesaurusItemsAsyncList,
  ) {
    List<Node> treeView = [];
    allThesaurusItemsAsyncList.forEach(
      (element) {
        List<Node> treeviewChild = [];
        final lista = allThesaurusItemsAsyncList.where(
          (e) => e.parentItemCode == element.thesaurusItemCode,
        );
        lista.forEach((element) {
          if (element.thesaurusItemType != ThesaurusItemTypeEnum.Contents) {
            treeviewChild.add(
              Node(
                key: element.thesaurusItemCode ?? '',
                label: element.thesaurusItemName ?? '',
              ),
            );
          }
        });
        if (element.parentItemCode == '' || element.parentItemCode == null) {
          treeView.add(
            Node(
              key: element.thesaurusItemCode!,
              label: element.thesaurusItemName!,
              children: treeviewChild,
            ),
          );
        }
      },
    );
    return treeView;
  }

  @override
  void initState() {
    super.initState();
    RawKeyboard.instance.addListener(_listenKeyboardEvent);
    entrysScrollController.addListener(_scrollListener);
    _checkIsShowRightScrol();

    /// 初始化形成所需的树结构
    _initThesaurusList();
  }

  @override
  void didUpdateWidget(VocabularyEntryReport oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    RawKeyboard.instance.removeListener(_listenKeyboardEvent);
    entrysScrollController.dispose();
    super.dispose();
  }

  /// 左边能否滑动
  bool _isLeftCanScroll = false;

  /// 右边能否滑动
  bool _isRightCanScroll = false;

  @override
  FWidget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    _maxWidth = mediaQuery.size.width * _scale;
    return FLayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
      _maxHeight = constraints.maxHeight;
      return FContainer(
        width: _maxWidth,
        decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(
              color: Colors.grey,
              blurRadius: 4,
              offset: Offset(2, 2),
            ),
          ],
          color: Colors.white,
          border: Border.all(
            color: Colors.grey,
          ),
          borderRadius: BorderRadius.circular(8),
        ),
        child: FColumn(
          children: [
            _buildHeader(),
            _buildEntrysSelected(),
            const FSizedBox(height: 5),
            FRow(
              children: [
                FExpanded(
                  child: SearchInput(
                    margin:
                        const EdgeInsets.symmetric(vertical: 5, horizontal: 20),
                    businessParent: widget,
                    onChanged: (value) {
                      keyWords = value;
                    },
                    serchOnPressed: () {
                      getEntryByKeyWords();
                    },
                    controller: _textEditingController,
                    hintText: i18nBook.remedical.searchKeyword.t,
                  ),
                ),
              ],
            ),
            _buildVocabularyTreeview(context),
          ],
        ),
      );
    });
  }

  FWidget _buildVocabularyTreeview(BuildContext context) {
    return FContainer(
      width: _maxWidth,
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(
            color: Color.fromRGBO(211, 211, 211, 1),
          ),
        ),
      ),
      margin: EdgeInsets.symmetric(
        vertical: 4,
      ),
      child: FRow(
        children: [
          FExpanded(
            flex: 4,
            child: _buildEntryTree(),
          ),
          FExpanded(
            flex: 11,
            child: FContainer(
              height: _maxHeight - 180,
              child: _EntriesItem(
                vocabularyCard: vocabularyCard,
              ),
            ),
          ),
        ],
      ),
    );
  }

  FWidget _buildEntryTree() {
    return FObx(() {
      if (reportEditController.state.allThesaurusList.isEmpty) {
        return FSizedBox();
      }
      return FContainer(
        height: _maxHeight - 180,
        child: FTreeView(
          theme: FTreeViewTheme(
            levelPadding: 15,
            horizontalSpacing: 15,
            colorScheme: ColorScheme.dark(
              primary: FTheme.ins.colorScheme.primary,
            ),
          ),
          onNodeTap: (code) {
            onNodeTap(code);
          },
          onExpansionChanged: (code, isDrop) {
            onExpansionChanged(
              code,
              isDrop,
            );
          },
          //  shrinkWrap: true,
          controller: _treeViewController,
          nodeBuilder: (buildContext, node) {
            return _EntryTreeView(
              node: node,
              selectedKey: selectedKey,
            );
          },
        ),
      );
    });
  }

  FWidget _buildHeader() {
    return FContainer(
      decoration: BoxDecoration(
        color: FTheme.ins.colorScheme.primary,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(8),
          topRight: Radius.circular(8),
        ),
      ),
      child: FRow(
        children: [
          const FSizedBox(
            width: 10,
            height: 40,
          ),
          FText(
            i18nBook.remedical.wordSelectBox.t,
            style: TextStyle(
              color: Colors.white,
              fontSize: 18,
            ),
          ),
          FExpanded(child: FSizedBox()),
          FInkWell(
            onTap: () => widget.onClose.call(),
            child: FIcon(
              Icons.close,
              color: Colors.white,
            ),
          ),
          const FSizedBox(
            width: 10,
          ),
        ],
      ),
    );
  }

  void _scrollListener() {
    if (entrysScrollController.offset <=
        entrysScrollController.position.minScrollExtent) {
      setState(() {
        _isLeftCanScroll = false;
        _isRightCanScroll = true;
      });
    } else if (entrysScrollController.offset >=
        entrysScrollController.position.maxScrollExtent) {
      setState(() {
        _isLeftCanScroll = true;
        _isRightCanScroll = false;
      });
    } else {
      setState(() {
        _isLeftCanScroll = true;
        _isRightCanScroll = true;
      });
    }
  }

  FWidget _buildEntrysSelected() {
    return FObx(() {
      var entryInfos = reportEditController.state.entryInfos;
      if (entryInfos.isEmpty) {
        return FSizedBox();
      }
      return FContainer(
          height: 40,
          alignment: Alignment.center,
          width: _maxWidth - 20,
          margin: EdgeInsets.symmetric(horizontal: 5),
          child: FRow(
            children: [
              FInkWell(
                child: FIcon(
                  Icons.chevron_left_rounded,
                  color: _isLeftCanScroll ? Colors.black : Colors.grey,
                  size: 50,
                ),
                onTap: () {
                  var target = entrysScrollController.offset - 60;
                  entrysScrollController.jumpTo(target > 0 ? target : 0);
                },
              ),
              const FSizedBox(
                width: 10,
              ),
              FExpanded(
                child: FListView(
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  controller: entrysScrollController,
                  children: entryInfos
                      .map(
                        (e) => ExpandableIconText(
                          icon: Icons.book,
                          isDistinguishChinese: false,
                          isExpandChild: false,
                          horizontalMargin: 10,
                          isSelected:
                              reportEditController.state.selectedEntryCode ==
                                  e.thesaurusCode,
                          onPressed: () async {
                            reportEditController.state.selectedEntryCode =
                                e.thesaurusCode ?? '';
                            await reportEditController.thesaurusController
                                .getDefaultThesaurus(e.thesaurusCode ?? '');
                            _initThesaurusList();
                            setState(() {});
                          },
                          onHoverChange: (v) {
                            if (v) {
                              setState(() {
                                _focusItem = e.thesaurusCode;
                              });
                            } else {
                              setState(() {
                                _focusItem = reportEditController
                                    .state.selectedEntryCode;
                              });
                            }
                          },
                          text: e.thesaurusName ?? '',
                          isExpand: _focusItem == e.thesaurusCode,
                        ),
                      )
                      .toList(),
                ),
              ),
              const FSizedBox(width: 10),
              FInkWell(
                child: FIcon(
                  Icons.chevron_right_rounded,
                  size: 50,
                  color: _isRightCanScroll ? Colors.black : Colors.grey,
                ),
                onTap: () {
                  var target = entrysScrollController.offset + 60;
                  var allLength =
                      entrysScrollController.position.maxScrollExtent;
                  entrysScrollController.jumpTo(
                      target > allLength ? allLength.toDouble() : target);
                },
              ),
            ],
          ));
    });
  }

  void _checkIsShowRightScrol() {
    Future.delayed(Duration(milliseconds: 500), () {
      var target = entrysScrollController.offset + 1;
      var allLength = entrysScrollController.position.maxScrollExtent;
      entrysScrollController
          .jumpTo(target > allLength ? allLength.toDouble() : target);
      entrysScrollController.jumpTo(0);
    });
  }
}

class _EntryTreeView extends FStatefulWidget implements FInteractiveContainer {
  _EntryTreeView({Key? key, required this.node, this.selectedKey = ''})
      : super(key: key);

  /// 当前下拉节点
  final Node<dynamic> node;
  @override
  final String pageName = 'EntryTreeView';

  /// 当前选中项
  final String? selectedKey;
  @override
  FState<_EntryTreeView> createState() => _EntryTreeViewState();
}

class _EntryTreeViewState extends FState<_EntryTreeView> {
  final EdgeInsets _padding = const EdgeInsets.symmetric(vertical: 4);
  bool hovering = false;
  @override
  FWidget build(BuildContext context) {
    var isNormalOption = widget.selectedKey != widget.node.key;
    return FContainer(
      margin: EdgeInsets.only(right: 15),
      child: FMouseRegion(
        cursor: SystemMouseCursors.click,
        onEnter: (e) {
          setState(() {
            hovering = true;
          });
        },
        onExit: (e) {
          setState(() {
            hovering = false;
          });
        },
        child: FGestureDetector(
          name: "${widget.node.label}",
          businessParent: this.widget,
          child: FColumn(
            children: [
              hovering
                  ? FRow(
                      children: [
                        FExpanded(
                          child: FContainer(
                            child: _SelectText(
                              label: widget.node.label,
                              isSelected: isNormalOption,
                              padding: this._padding,
                            ),
                          ),
                        ),
                      ],
                    )
                  : FRow(
                      children: [
                        FExpanded(
                          child: FContainer(
                            padding: _padding,
                            child: FText(
                              widget.node.label,
                              style: widget.selectedKey == widget.node.key
                                  ? TextStyle(
                                      color: Colors.white,
                                    )
                                  : TextStyle(
                                      color: Colors.black,
                                    ),
                            ),
                          ),
                        ),
                      ],
                    ),
            ],
          ),
        ),
      ),
    );
  }
}

/// 词条分类选中样式
class _SelectText extends FStatelessWidget {
  _SelectText({
    required this.isSelected,
    required this.label,
    required this.padding,
  });
  final bool isSelected;
  final String label;
  final EdgeInsetsGeometry padding;

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      padding: padding,
      child: FText(
        label,
        style: TextStyle(
          color: isSelected ? Colors.black : Colors.white,
        ),
      ),
    );
  }
}

class _EntriesItem extends FStatelessWidget {
  _EntriesItem({
    this.vocabularyCard,
  });

  /// 词条卡片内容
  final List<ThesaurusItemDTO>? vocabularyCard;
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      child: FReorderableListView(
        shrinkWrap: true,
        buildDefaultDragHandles: false,
        children: vocabularyCard!
            .map(
              (item) => FContainer(
                key: Key(
                  item.thesaurusItemCode.toString(),
                ),
                child: SwipeCard(
                  isForbiddenAnimation: true,
                  forbiddenAnimationMargin: EdgeInsets.all(0),
                  forbiddenAnimationPadding:
                      EdgeInsets.only(left: 5, top: 5, right: 5),
                  child: FColumn(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      FContainer(
                        child: FText(
                          i18nBook.remedical.description.t,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      FRow(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          FExpanded(
                            child: FContainer(
                              child: FText(
                                item.thesaurusItemDescription!,
                                textAlign: TextAlign.left,
                              ),
                            ),
                          )
                        ],
                      ),
                      FSizedBox(
                        height: 5,
                      ),
                      FContainer(
                        child: FText(
                          i18nBook.remedical.conclusion.t,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      FRow(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          FExpanded(
                            child: FContainer(
                              child: FText(
                                item.thesaurusItemConclusion!,
                                textAlign: TextAlign.left,
                              ),
                            ),
                          )
                        ],
                      ),
                      FSizedBox(
                        height: 10,
                      ),
                    ],
                  ),
                  onTap: () {
                    FReportInfo.instance.insertDiagnostic(
                      item.thesaurusItemDescription ?? '',
                      item.thesaurusItemConclusion ?? '',
                    );
                  },
                  isActive: false,
                ),
              ),
            )
            .toList(),
        onReorder: (int oldIndex, int newIndex) {
          /// 拖拽的方法
        },
      ),
    );
  }
}
