import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/remedical/vocabulary_entry/controller.dart';

/// 编辑词条词典主页面
class EditVocabularyEntryPage extends FStatefulWidget {
  const EditVocabularyEntryPage({Key? key}) : super(key: key);

  @override
  FState<EditVocabularyEntryPage> createState() =>
      _EditVocabularyEntryPageState();
}

/// 编辑词条词典主页面状态组件
class _EditVocabularyEntryPageState extends FState<EditVocabularyEntryPage> {
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.remedical.createThesaurus.t,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      isUseColumnLayout: true,
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      insetPadding: const EdgeInsets.symmetric(vertical: 130),
      children: [
        FContainer(
          width: 600,
          height: 600,
          padding: const EdgeInsets.only(
            top: 25,
            left: 25,
          ),
          child: _EditVocabulary(),
        ),
      ],
    );
  }
}

///编辑词条页面
class _EditVocabulary extends FisView<VocabularyEntryController> {
  static BoxDecoration createBorder() {
    final border = BoxDecoration(
      borderRadius: BorderRadius.all(Radius.circular(8)),
      border: Border.all(color: Color.fromRGBO(141, 141, 141, 1), width: 0.5),
    );
    return border;
  }

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FText(
          i18nBook.remedical.selfCreatedVoc.t,
          style: const TextStyle(
            fontWeight: FontWeight.bold,
          ),
        ),
        FContainer(
          height: 50,
          width: 200,
          margin: const EdgeInsets.only(
            top: 15,
            bottom: 30,
          ),
          decoration: _EditVocabulary.createBorder(),
          // color: Colors.redAccent,
          alignment: Alignment.centerLeft,
          child: FInk(
            child: FInkWell(
              onTap: () => controller.openEditThesaurusDialog(null),
              borderRadius: BorderRadius.all(Radius.circular(8.0)),
              highlightColor: FTheme.ins.colorScheme.primary,
              child: FContainer(
                child: FCenter(
                  child: FText(
                    i18nBook.remedical.selfCreatedVoc.t,
                  ),
                ),
              ),
            ),
          ),
        ),
        FText(
          i18nBook.remedical.useFisDefaultThesaurus.t,
          style: const TextStyle(
            fontWeight: FontWeight.bold,
          ),
        ),
        FExpanded(
          child: FContainer(
            margin: const EdgeInsets.only(
              top: 15,
              bottom: 30,
            ),
            child: FColumn(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                FExpanded(
                  child: FSingleChildScrollView(
                    child: FWrap(
                      spacing: 8.0, // 主轴(水平)方向间距
                      runSpacing: 16.0, // 纵轴（垂直）方向间距
                      direction: Axis.horizontal,
                      alignment: WrapAlignment.start,
                      children: List<FWidget>.generate(
                        controller.state.createEntryInfos.length,
                        (index) {
                          return _DefaultVocabulary(
                            controller.state.createEntryInfos[index],
                          );
                        },
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        )
      ],
    );
  }
}

/// 词条库模板
///
/// [thesaurusDTO] 词条库
class _DefaultVocabulary extends FisView<VocabularyEntryController> {
  _DefaultVocabulary(
    this.thesaurusDTO,
  );
  ThesaurusDTO thesaurusDTO = ThesaurusDTO();
  static BoxDecoration createBorder() {
    final border = BoxDecoration(
      borderRadius: BorderRadius.all(Radius.circular(8)),
      border: Border.all(color: Color.fromRGBO(141, 141, 141, 1), width: 0.5),
    );
    return border;
  }

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: i18nBook.isCurrentRussian ? 104 : 88,
      width: 250,
      margin: EdgeInsets.only(
        right: 25,
      ),
      decoration: _DefaultVocabulary.createBorder(),
      child: FInk(
        child: FInkWell(
          onTap: () {
            controller.isOrg
                ? controller.organizationEntryController
                    .copyOrgThesaurusAsync(thesaurusDTO.thesaurusCode!)
                : controller.personalEntryController
                    .copyPersonalThesaurusAsync(thesaurusDTO.thesaurusCode!);
          },
          borderRadius: BorderRadius.all(Radius.circular(8.0)),
          highlightColor: FTheme.ins.colorScheme.primary,
          child: FStack(
            alignment: AlignmentDirectional.topEnd, //设置该组件的绘图位置是从母组件的顶部，右侧开始绘画。
            children: [
              FContainer(
                padding: EdgeInsets.only(
                  top: i18nBook.isCurrentRussian ? 23 : 7,
                  left: 20,
                  bottom: 12,
                ),
                child: FColumn(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    FText(thesaurusDTO.thesaurusName!),
                    FSizedBox(
                      height: 3,
                    ),
                    FRow(
                      children: [
                        FText(
                          i18nBook.organization.serveObj.t,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        FSizedBox(
                          width: 5,
                        ),
                        FText(
                          thesaurusDTO.thesaurusUsageType ==
                                  ThesaurusUsageTypeEnum.People
                              ? i18nBook.remedical.humanAlong.t
                              : i18nBook.remedical.beastAlong.t,
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              _DefaultVocabularyCard(thesaurusDTO),
            ],
          ),
        ),
      ),
    );
  }
}

/// 词条库模板卡片
/// [thesaurusDTO] 词条库
class _DefaultVocabularyCard extends FisView<VocabularyEntryController> {
  _DefaultVocabularyCard(
    this.thesaurusDTO,
  );
  final ThesaurusDTO thesaurusDTO;
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      margin: const EdgeInsets.only(
        top: 5,
      ),
      child: FContainer(
          padding: EdgeInsets.symmetric(horizontal: 10),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(11.0),
              bottomLeft: Radius.circular(11.0),
            ),
            color: FTheme.ins.colorScheme.primary,
          ),
          child: FText(
            _getTagText(),
            style: TextStyle(
              color: Colors.white,
            ),
          )),
    );
  }

  String _getTagText() {
    switch (thesaurusDTO.thesaurusType) {
      case ThesaurusTypeEnum.Default:
        return i18nBook.remedical.defualt.t;
      case ThesaurusTypeEnum.Organization:
        return i18nBook.remedical.organization.t;
      case ThesaurusTypeEnum.Personal:
        return i18nBook.remedical.selfCreated.t;
      default:
        return i18nBook.remedical.defualt.t;
    }
  }
}
