import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.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/pages/remedical/vocabulary_entry/controller.dart';
import 'package:flyinsono/pages/remedical/vocabulary_entry/view.dart';
import 'package:flyinsono/pages/remedical/vocabulary_entry/widgets/entries_item.dart';

/// 词条分类
class EntriesClassificationBody extends FisView<VocabularyEntryController>
    implements FInteractiveContainer {
  @override
  final String pageName;

  EntriesClassificationBody({this.pageName = 'EntriesClassificationBody'});

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      margin: EdgeInsets.all(10),
      width: 400,
      child: FColumn(
        children: [
          _EntriesInputClassification(this),
          FContainer(
            height: 5,
          ),
          FObx(
            () => controller.state.isEnableEdit
                ? FRow(
                    children: [
                      const FSizedBox(
                        width: 20,
                      ),
                      EntriesBottomItem(
                        businessParent: this,
                        itemTitle: i18nBook.remedical.createWordClass.t,
                        onClick: () =>
                            controller.openEntryClassifcationDialog(null),
                      ),
                    ],
                  )
                : FContainer(),
          ),
          FExpanded(
            child: _Entries(),
          ),
        ],
      ),
    );
  }
}

class _EntriesInputClassification extends FisView<VocabularyEntryController> {
  const _EntriesInputClassification(this.businessParent, {Key? key})
      : super(key: key);

  ///父级节点
  final FInteractiveContainer businessParent;
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      margin: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      color: Colors.white,
      child: FRow(
        children: [
          FExpanded(
            child: FContainer(
              height: 45,
              child: FRow(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  FBorderInput(
                    hintSize: 16,
                    contentSize: 16,
                    maxLength: 20,
                    borderColor: Color.fromARGB(255, 187, 180, 180),
                    suffixIcon: FMaterial(
                      color: Colors.transparent,
                      child: FIconButton(
                        businessParent: this.businessParent,
                        name: "getEntryByKeyWords",
                        onPressed: () {
                          controller.getEntryByKeyWords();
                        },
                        icon: FIcon(
                          Icons.search,
                        ),
                      ),
                    ),
                    height: 45,
                    focusNode: controller.searchThesaurusItemInputFocusNode,
                    hintText: i18nBook.common.search.t,
                    onChanged: (value) {
                      controller.state.keyWords = value;
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class _Entries extends FisView<VocabularyEntryController> {
  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      return FContainer(
        margin: EdgeInsets.only(
          top: 15,
          left: 10,
          right: 10,
        ),
        child: controller.state.getTreeView.length == 0
            ? NoDataWidget() as FWidget
            : FTreeView(
                theme: FTreeViewTheme(
                  colorScheme: ColorScheme.dark(
                    primary: FTheme.ins.colorScheme.primary,
                  ),
                ),
                onNodeTap: (code) {
                  controller.onNodeTap(code);
                },
                onExpansionChanged: (code, isDrop) {
                  controller.onExpansionChanged(
                    code,
                    isDrop,
                  );
                },
                controller: TreeViewController(
                  children: controller.state.getTreeView,
                  selectedKey: controller.state.selectedKey,
                ),
                nodeBuilder: (buildContext, node) {
                  return EntryTreeView(node: node);
                },
              ),
      );
    });
  }
}
