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

///词条字典编辑对话框
class EditThesaurusDialog extends FStatefulWidget {
  EditThesaurusDialog({
    Key? key,
    this.onOkTap,
    this.thesaurusInfo,
    this.onCancelTap,
  }) : super(key: key);
  final ValueCallback<bool>? onOkTap;
  final VoidCallback? onCancelTap;
  final ThesaurusDTO? thesaurusInfo;

  @override
  FState<EditThesaurusDialog> createState() => _EditThesaurusDialogState();
}

///词条字典编辑对话框状态
class _EditThesaurusDialogState extends FState<EditThesaurusDialog> {
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        widget.thesaurusInfo != null
            ? i18nBook.remedical.editThesaurus.t
            : i18nBook.remedical.selfCreatedThesaurus.t,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      isDefault: true,
      cancelString: i18nBook.common.cancel.t,
      okString: i18nBook.common.save4Btn.t,
      onOk: () => widget.onOkTap?.call(widget.thesaurusInfo == null),
      onCancel: () => widget.onCancelTap?.call(),
      insetPadding: const EdgeInsets.symmetric(vertical: 100),
      children: [
        FContainer(
          width: 520,
          height: 150,
          padding: const EdgeInsets.only(
            left: 25,
            right: 25,
          ),
          child: _EntryClassifcation(),
        ),
      ],
    );
  }
}

///循环字典列表
class _EntryClassifcation extends FisView<VocabularyEntryController> {
  @override
  FWidget build(BuildContext context) {
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        _EntryClassifcationItem(
          isRequire: true,
          inputWidget: FRow(
            children: [
              FExpanded(
                child: FContainer(
                  height: 45,
                  child: FRow(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      const FSizedBox(
                        width: 25,
                      ),
                      FBorderInput(
                        hintSize: 16,
                        contentSize: 16,
                        maxLength: PageHelper.maxInputLength_general(),
                        borderColor: Color(0xffdcdfe6),
                        height: 40,
                        hintText: i18nBook.remedical.thesaurusNameInputTip.t,
                        controller: TextEditingController(
                          text:
                              controller.state.createThesaurus['thesaurusName'],
                        ),
                        onChanged: (value) {
                          controller.state.createThesaurus['thesaurusName'] =
                              value;
                        },
                      ),
                      const FSizedBox(
                        width: 10,
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          title: i18nBook.remedical.vocBaseName.t,
        ),
      ],
    );
  }
}

///循环字典条目
class _EntryClassifcationItem extends FisView<VocabularyEntryController> {
  _EntryClassifcationItem({
    required this.title,
    required this.inputWidget,
    this.isRequire = false,
  });
  final String title;
  final FWidget inputWidget;
  final bool? isRequire;
  final TextStyle labelStyle = TextStyle(fontSize: 16);
  @override
  FWidget build(BuildContext context) {
    return FRow(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        FContainer(
          margin: EdgeInsets.symmetric(
            horizontal: 15,
          ),
          width: 100,
          child: FText.rich(
            TextSpan(
              children: [
                TextSpan(
                  text: title,
                  style: labelStyle,
                ),
                isRequire!
                    ? TextSpan(
                        text: ' *',
                        style: labelStyle.copyWith(color: Colors.red),
                      )
                    : const TextSpan(),
              ],
            ),
          ),
        ),
        FExpanded(
          child: FContainer(
            margin: const EdgeInsets.symmetric(
              horizontal: 15,
            ),
            child: inputWidget,
          ),
        )
      ],
    );
  }
}
