import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/usual/star.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/index.dart';
import 'package:get/get.dart';

class AddConsultationEvaluationView extends FStatefulWidget {
  final bool isOnlyRead; //是否只读
  AddConsultationEvaluationView({this.isOnlyRead = false, super.key});
  @override
  FState<AddConsultationEvaluationView> createState() =>
      _AddConsultationEvaluationViewState();
}

class _AddConsultationEvaluationViewState
    extends FState<AddConsultationEvaluationView> {
  final controller = Get.find<RealTimeConsultationController>();

  bool isOnlyRead = false;

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        isOnlyRead
            ? i18nBook.realTimeConsultation.viewComments.t
            : i18nBook.realTimeConsultation.consultationEvaluation.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: FFontSizes.H5,
        ),
      ),
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      isDefault: !isOnlyRead,
      children: [_buildmainContent()],
      onOk: () {
        controller.evaluationController.addEvaluation();
      },
      onCancel: () {
        router.back(result: {'isCancel': true});
      },
    );
  }

  @override
  void initState() {
    super.initState();
    isOnlyRead = widget.isOnlyRead;
    controller.state.consultationEvaluation = "";
    controller.state.score = 0;
    controller.state.gradeDescription = "";
    controller.evaluationController.setGradeDescription(isOnlyRead);
  }

  ///会诊评价输入框
  FWidget _buidEvaluation() {
    String? feedBack = i18nBook.remedical.notFill.t;
    if (isOnlyRead) {
      feedBack = (controller.state.consultationEvaluateDetail?.feedback ?? "")
              .isNotEmpty
          ? controller.state.consultationEvaluateDetail?.feedback
          : i18nBook.remedical.notFill.t;
      return FText(
        feedBack ?? i18nBook.remedical.notFill.t,
        overflow: TextOverflow.ellipsis,
        maxLines: 5,
      );
    } else {
      return FTextField(
        maxLength: 200,
        maxLines: 8,
        decoration: InputDecoration(
          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,
        ),
        controller: TextEditingController(
          text: controller.state.consultationEvaluation,
        ),
        onChanged: ((value) {
          controller.state.consultationEvaluation = value.trim();
        }),
      );
    }
  }

  FWidget _buildCommonWidget(
      String title, FWidget contentWidget, bool isRequired) {
    return FFlex(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FExpanded(
          child: FText.rich(
            TextSpan(children: [
              TextSpan(
                text: title,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: FFontSizes.H6,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              isRequired
                  ? TextSpan(text: ' *', style: TextStyle(color: Colors.red))
                  : TextSpan(),
            ]),
          ),
          flex: 1,
        ),
        FExpanded(
          child: contentWidget,
          flex: 3,
        ),
      ],
    );
  }

  FWidget _buildmainContent() {
    return FContainer(
      /// TODO:[Gavin] 多语言-UI-整合
      width: i18nBook.isCurrentChinese ? 560 : 700,
      height: isOnlyRead ? 250 : 350,
      margin: EdgeInsets.only(left: 20, right: 20, top: 15, bottom: 15),
      child: FColumn(
        children: [
          if (!isOnlyRead)
            FText(
              i18nBook.realTimeConsultation.evaluationTip.t,
              style: TextStyle(
                color: Colors.grey,
                fontSize: FFontSizes.SMALL,
              ),
            ),
          _buildSpaceWidget(),
          _buildCommonWidget(
            i18nBook.realTimeConsultation.consultationScore.t,
            _buildScore(),
            true,
          ),
          _buildSpaceWidget(),
          _buildCommonWidget(
            i18nBook.realTimeConsultation.consultationEvaluation.t,
            _buidEvaluation(),
            false,
          ),
          _buildSpaceWidget(),
        ],
      ),
    );
  }

  ///会诊评分
  FWidget _buildScore() {
    return FRow(
      children: [
        FFStars(
          normalStar: FIcon(
            Icons.star,
            color: Color.fromARGB(255, 228, 224, 224),
          ),
          selectedStar: FIcon(
            Icons.star,
            color: FTheme.ins.colorScheme.primary,
          ),
          step: 0.5,
          justShow: isOnlyRead,
          defaultStars: isOnlyRead
              ? (controller.state.consultationEvaluateDetail?.evaluateScore ??
                  0)
              : 0,
          starsChanged: (realStars, choosedStars) {
            print("realStars:$realStars,choosedStars:$choosedStars");
            controller.evaluationController.setScore(realStars);
          },
        ),
        FContainer(
          width: 20,
        ),
        FObx(
          () => FText(
            controller.state.gradeDescription,
            style: TextStyle(color: Colors.orange),
          ),
        ),
      ],
    );
  }

  ///行与行之间的间隔
  FWidget _buildSpaceWidget() {
    return FContainer(
      height: 20,
    );
  }
}
