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';
import 'package:get/get.dart';

///词条选择对话框
class VocabularyDialogPage extends FStatefulWidget {
  VocabularyDialogPage({
    Key? key,
    this.onOkTap,
    this.vocabularyCardItem,
    this.onCancelTap,
    this.onDeleteTap,
  }) : super(key: key);
  ValueCallback<bool>? onOkTap;
  VoidCallback? onDeleteTap;
  VoidCallback? onCancelTap;
  ThesaurusItemDTO? vocabularyCardItem;

  @override
  FState<VocabularyDialogPage> createState() => _VocabularyDialogPageState();
}

///词条选择对话框状态组件
class _VocabularyDialogPageState extends FState<VocabularyDialogPage> {
  final controller = Get.find<VocabularyEntryController>();
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        controller.state.isEnableEdit
            ? widget.vocabularyCardItem?.thesaurusItemCode != null
                ? i18nBook.remedical.vocEdit.t
                : i18nBook.remedical.vocAdd.t
            : i18nBook.remedical.vocDetails.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      isDefault: controller.state.isEnableEdit ? true : false,
      onOk: () => widget.onOkTap
          ?.call(widget.vocabularyCardItem?.thesaurusItemCode != null),
      cancelString: widget.vocabularyCardItem?.thesaurusItemCode != null
          ? i18nBook.common.delete.t
          : i18nBook.common.cancel.t,
      okString: i18nBook.common.confirm.t,
      onCancel: () => widget.vocabularyCardItem?.thesaurusItemCode != null
          ? widget.onDeleteTap?.call()
          : widget.onCancelTap?.call(),
      onClose: () => widget.onCancelTap?.call(),
      insetPadding: EdgeInsets.symmetric(vertical: 100),
      // backgroundColor: Color.fromRGBO(230, 230, 230, 1),
      children: [
        FContainer(
          width: 450,
          height: 350,
          padding: EdgeInsets.only(
            top: 25,
            left: 25,
            right: 25,
          ),
          child: _EditVocabulary(),
        ),
      ],
    );
  }
}

///编辑词条界面
class _EditVocabulary extends FisView<VocabularyEntryController> {
  @override
  FWidget build(BuildContext context) {
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FSizedBox(
          height: 20,
        ),
        FContainer(
          child: FRow(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              FText(
                i18nBook.remedical.description.t,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),
              FSizedBox(
                width: 20,
              ),
              _buildDescriptionEditorExpander()
            ],
          ),
        ),
        FSizedBox(
          height: 20,
        ),
        FContainer(
          child: FRow(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              FText(
                i18nBook.remedical.summary.t,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),
              FSizedBox(
                width: 20,
              ),
              _buildConclustionExpaner()
            ],
          ),
        ),
      ],
    );
  }

  FWidget _buildDescriptionEditorExpander() {
    return FExpanded(
      child: FTextField(
        maxLines: 4,
        readOnly: controller.state.isEnableEdit ? false : true,
        controller: TextEditingController(
          text: controller.state.vocabularyCardItem.thesaurusItemDescription,
        ),
        onChanged: (val) =>
            controller.state.vocabularyCardItem.thesaurusItemDescription = val,
        decoration: InputDecoration(
          hintText: i18nBook.remedical.descriptionInput.t,
          hintStyle: TextStyle(
            fontSize: 16,
          ),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.white.withOpacity(0.5),
              width: 0.5,
              style: BorderStyle.solid,
            ),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: FTheme.ins.colorScheme.primary,
              width: 0.5,
              style: BorderStyle.solid,
            ),
          ),
          filled: true,
        ),
      ),
    );
  }

  FWidget _buildConclustionExpaner() {
    return FExpanded(
      child: FTextField(
        maxLines: 4,
        readOnly: controller.state.isEnableEdit ? false : true,
        controller: TextEditingController(
          text: controller.state.vocabularyCardItem.thesaurusItemConclusion,
        ),
        onChanged: (val) =>
            controller.state.vocabularyCardItem.thesaurusItemConclusion = val,
        decoration: InputDecoration(
          hintText: i18nBook.remedical.concolusionInput.t,
          hintStyle: TextStyle(
            fontSize: 16,
          ),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: Colors.white.withOpacity(0.5),
              width: 0.5,
              style: BorderStyle.solid,
            ),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: FTheme.ins.colorScheme.primary,
              width: 0.5,
              style: BorderStyle.solid,
            ),
          ),
          filled: true,
        ),
      ),
    );
  }
}
