import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/usual/star.dart';
import 'package:fis_ui/values/spacings.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/pages/consultation/controller.dart';

class EvaluationPage extends FisView<MobileConsultationController>
    implements FPage {
  final ConsultationDetailDTO? consultationDetail;
  EvaluationPage({this.consultationDetail});
  @override
  String get pageName => "EvaluationPage";
  final TextStyle _style = TextStyle(
    fontSize: 20,
  );

  @override
  FWidget build(BuildContext context) {
    if (controller.state.consultationEvaluateDetail != null) {
      controller.setEvaluateScore(
          controller.state.consultationEvaluateDetail?.evaluateScore ?? 0);
    }

    return FScaffold(
      appBar: FAppBar.simple(
        title: i18nBook.realTimeConsultation.consultationEvaluation.t,
        onBackClicked: () {
          controller.state.gradeDescription = '';
          router.back();
        },
        actions: [
          if (controller.state.consultationEvaluateDetail == null &&
              (consultationDetail?.isEvaluateShow ?? false))
            FElevatedButton(
              child: FText(i18nBook.common.submit.t),
              onPressed: () {
                controller.submitEvaluateDetail();
              },
              businessParent: this,
              name: 'submit',
              style: ElevatedButton.styleFrom(
                elevation: 0,
              ),
            ),
        ],
      ),
      body: FContainer(
        margin: EdgeInsets.only(left: 15, top: 25, right: 15),
        child: FColumn(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildEvaluateScore(),
            const FSizedBox(height: FSpacings.XXL),
            FRow(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                FContainer(
                  margin: EdgeInsets.only(top: FSpacings.M),
                  child: FText(
                    i18nBook.common.evaluation.t,
                    style: _style,
                  ),
                ),
                const FSizedBox(width: 10),
                _buildInputEvaluate(),
              ],
            )
          ],
        ),
      ),
    );
  }

  FWidget _buildEvaluateScore() {
    FWidget row = FRow(
      children: [
        FText(
          i18nBook.common.rating.t,
          style: _style,
        ),
        const FSizedBox(width: 10),
        FExpanded(
          child: FFStars(
            starMargin: i18nBook.isCurrentChinese ? 10 : 1,
            normalStar: FIcon(
              Icons.star,
              size: 30,
              color: Color.fromARGB(255, 228, 224, 224),
            ),
            selectedStar: FIcon(
              Icons.star,
              color: FTheme.ins.colorScheme.primary,
            ),
            step: 0.5,
            justShow: controller.state.consultationEvaluateDetail != null,
            defaultStars:
                controller.state.consultationEvaluateDetail?.evaluateScore ?? 0,
            starsChanged: (realStars, choosedStars) {
              controller.setEvaluateScore(realStars);
            },
          ),
        ),
        if (!i18nBook.isCurrentRussian) ...[
          const FSizedBox(width: 15),
          FObx(
            () => FText(
              controller.state.gradeDescription,
              style: _style.copyWith(
                color: Colors.black45,
                fontSize: i18nBook.isCurrentChinese ? null : 14,
              ),
            ),
          ),
          const FSizedBox(width: 15),
        ],
      ],
    );
    if (i18nBook.isCurrentRussian) {
      return FColumn(
        children: [
          row,
          FSizedBox(
            height: 15,
          ),
          FRow(
            children: [
              FExpanded(child: FSizedBox()),
              FObx(
                () => FText(
                  controller.state.gradeDescription,
                  style: _style.copyWith(
                    color: Colors.black45,
                    fontSize: i18nBook.isCurrentChinese ? null : 14,
                  ),
                ),
              ),
              const FSizedBox(width: 15),
            ],
          ),
        ],
      );
    }
    return row;
  }

  ///评价输入框
  FWidget _buildInputEvaluate() {
    return FExpanded(
      child: FTextField(
        maxLines: 10,
        minLines: i18nBook.isCurrentRussian ? 8 : 6,
        decoration: InputDecoration(
          border: OutlineInputBorder(borderRadius: BorderRadius.circular(8.0)),
          hintText: i18nBook.realTimeConsultation.evaluationTip.t,
          hintStyle: TextStyle(color: Colors.grey),
          hintMaxLines: i18nBook.isCurrentRussian ? 8 : 6,
        ),
        maxLength: 200,
        readOnly: controller.state.consultationEvaluateDetail != null,
        controller: controller.evaluationController,
      ),
    );
  }
}
