import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_jsonrpc/services/liveConsultation.m.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/age_text.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/controller.dart';

/// 病人详情
class PatientInfoDialog extends FisView<ConsultationRecordViewController>
    implements FInteractiveContainer {
  PatientInfoDialog({
    this.pageName = 'PatientInfoDialog',
    required this.patientInfo,
    Key? key,
  }) : super(key: key);
  @override
  final String pageName;
  final PatientInfo patientInfo;
  final ScrollController _scrollController = ScrollController();

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.realTimeConsultation.patientInformation.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      onClose: () {
        router.back();
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      bottomButton: FContainer(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(vertical: 15),
        child: FFlex(
          direction: Axis.horizontal,
          mainAxisSize: MainAxisSize.min,
          children: [
            FElevatedButton(
              name: "confirm:",
              businessParent: this,
              onPressed: () {
                router.back();
              },
              style: ButtonStyle(
                padding: MaterialStateProperty.all(
                  EdgeInsets.symmetric(
                    vertical: 15,
                    horizontal: 25,
                  ),
                ),
              ),
              child: FText(i18nBook.common.confirm4Btn.t),
            ),
          ],
        ),
      ),
      children: [
        FContainer(
          width: 700,
          child: FContainer(
            child: FColumn(
              children: [
                _buildPatientInfo(context),
              ],
            ),
          ),
        ),
      ],
    );
  }

  FWidget _buildPatientInfo(BuildContext context) {
    return FContainer(
      color: Colors.white,
      padding: EdgeInsets.all(30),
      child: FAlwaysScrollListView(
        scrollController: _scrollController,
        child: QuickFWidget(
          ScrollConfiguration(
            behavior:
                ScrollConfiguration.of(context).copyWith(scrollbars: false),
            child: FListView(
              shrinkWrap: true,
              controller: _scrollController,
              children: [
                FContainer(
                  child: FColumn(
                    children: [
                      FRow(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _buildHeadImage(patientInfo),
                          _buildBasicInfo(patientInfo),
                        ],
                      )
                    ],
                  ),
                ),
                FContainer(
                  child: FColumn(
                    children: [
                      FRow(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _buildPlaceholderContainer(),
                          FColumn(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              _buildOtherInfoTitle(),
                              _buildIdInfo(patientInfo),
                              _buildAgeInfo(patientInfo),
                              _buildSexInfo(patientInfo),
                              _buildSocialSecurityCardInfo(patientInfo),
                              _buildVarietyInfo(patientInfo),
                            ],
                          ),
                        ],
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  ///头像区域，显示图Or姓名首个文字
  FWidget _buildHeadImage(PatientInfo patientInfo) {
    String avatar = ' ';
    final name = (patientInfo.name);
    if (name != null) {
      if (name.toString().isNotEmpty) {
        avatar = name.toString().substring(0, 1);
      }
    }
    return FContainer(
      alignment: Alignment.center,
      margin: EdgeInsets.only(
        top: 15,
        left: 15,
        right: 30,
        bottom: 15,
      ),
      width: 100,
      height: 100,
      decoration: ShapeDecoration(
        color: patientInfo.headImgUrl == '' || patientInfo.headImgUrl == null
            ? FTheme.ins.colorScheme.primary
            : Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadiusDirectional.circular(
            4,
          ),
        ),
      ),
      child: patientInfo.headImgUrl == '' || patientInfo.headImgUrl == null
          ? FText(
              avatar,
              style: TextStyle(
                fontSize: 46,
                color: Colors.white,
              ),
            )
          : FClipRRect(
              borderRadius: BorderRadius.circular(
                4,
              ),
              child: FConstrainedBox(
                constraints: BoxConstraints.expand(), // 自适应屏幕
                child: FImage.network(
                  patientInfo.headImgUrl!,
                  fit: BoxFit.cover,
                ),
              ),
              // FText(patientInfo['headImgUrl']),
            ),
    );
  }

  ///基础信息
  FWidget _buildBasicInfo(PatientInfo patientInfo) {
    String name = patientInfo.name ?? '';
    String phone = patientInfo.phone ?? '';
    if (name.isEmpty) {
      name = i18nBook.remedical.notFill.t;
    }
    if (phone.isEmpty) {
      phone = i18nBook.remedical.notFill.t;
    }
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FContainer(
          margin: EdgeInsets.symmetric(vertical: 15),
          child: _buildPatientInfoLable(
            i18nBook.profileComplete.basicInformation.t,
            fontSize: 18,
          ),
        ),
        FContainer(
          margin: EdgeInsets.only(bottom: 15),
          child: FRow(
            children: [
              FContainer(
                width: 200,
                child: _buildPatientInfoLable(
                  controller.state.patientType ==
                          OrganizationPatientTypeEnum.Person
                      ? i18nBook.common.fullName.t
                      : i18nBook.remedical.petName.t,
                ),
              ),
              FContainer(
                width: 200,
                child: FText(
                  name,
                ),
              ),
            ],
          ),
        ),
        FContainer(
          margin: EdgeInsets.only(bottom: 5),
          child: FRow(
            children: [
              FContainer(
                width: 200,
                child: _buildPatientInfoLable(
                  controller.state.patientType ==
                          OrganizationPatientTypeEnum.Person
                      ? i18nBook.common.mobileNumber.t
                      : i18nBook.remedical.ownerPhone.t,
                ),
              ),
              FContainer(
                width: 200,
                child: FText(
                  phone,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  ///占位
  FWidget _buildPlaceholderContainer() {
    return FContainer(
      width: 100,
      height: 100,
      margin: EdgeInsets.only(
        top: 15,
        left: 15,
        right: 30,
        bottom: 15,
      ),
      child: FCenter(
        child: FContainer(),
      ),
    );
  }

  ///其他信息
  FWidget _buildOtherInfoTitle() {
    return FContainer(
      margin: EdgeInsets.only(
        bottom: 15,
        top: 5,
      ),
      child: _buildPatientInfoLable(
        i18nBook.remedical.otherInfo.t,
        fontSize: 18,
      ),
    );
  }

  ///身份证Or宠物号
  FWidget _buildIdInfo(PatientInfo patientInfo) {
    var id = '';
    var identityCard = patientInfo.identityCard ?? '';
    if (identityCard.isEmpty) {
      id = i18nBook.remedical.notFill.t;
    } else {
      id = identityCard;
    }
    return FContainer(
      margin: EdgeInsets.only(bottom: 15),
      child: FRow(
        children: [
          FContainer(
            width: 200,
            child: _buildPatientInfoLable(
              controller.state.patientType == OrganizationPatientTypeEnum.Person
                  ? i18nBook.remedical.idCard.t
                  : i18nBook.remedical.petId.t,
            ),
          ),
          FContainer(
            width: 200,
            child: FText(id),
          ),
        ],
      ),
    );
  }

  ///年龄信息
  FWidget _buildAgeInfo(PatientInfo patientInfo) {
    return FContainer(
      margin: EdgeInsets.only(bottom: 15),
      child: FRow(
        children: [
          FContainer(
            width: 200,
            child: _buildPatientInfoLable(i18nBook.remedical.age.t),
          ),
          // 年龄变更
          FContainer(
            width: 200,
            child: AgeText(
              ageUnits: patientInfo.ageUnits,
              ageDay: patientInfo.ageDay,
              ageMonth: patientInfo.ageMonth,
              ageWeek: patientInfo.ageWeek,
              ageYear: patientInfo.ageYear,
            ),
          ),
        ],
      ),
    );
  }

  ///性别信息
  FWidget _buildSexInfo(PatientInfo patientInfo) {
    return FContainer(
      margin: EdgeInsets.only(bottom: 15),
      child: FRow(
        children: [
          FContainer(
            width: 200,
            child: _buildPatientInfoLable(i18nBook.remedical.gender.t),
          ),
          FContainer(
            width: 200,
            child: FText(
              patientInfo.sex != null && patientInfo.sex != ''
                  ? controller.getValues(patientInfo.sex!)
                  : i18nBook.remedical.notFill.t,
            ),
          ),
        ],
      ),
    );
  }

  ///社保卡号Or动物种类
  FWidget _buildSocialSecurityCardInfo(PatientInfo patientInfo) {
    var code = '';
    var insuranceCode = patientInfo.insuranceCode;
    if (insuranceCode == null || insuranceCode.isEmpty) {
      code = i18nBook.remedical.notFill.t;
    } else {
      code = insuranceCode;
    }
    return FContainer(
      margin: EdgeInsets.only(bottom: 15),
      child: FRow(
        children: [
          FContainer(
            width: 200,
            child: controller.state.patientType ==
                    OrganizationPatientTypeEnum.Person
                ? _buildPatientInfoLable(
                    i18nBook.remedical.socialSecurityCardId.t)
                : _buildPatientInfoLable(i18nBook.remedical.type.t),
          ),
          FContainer(
            width: 200,
            child: controller.state.patientType ==
                    OrganizationPatientTypeEnum.Person
                ? FText(code)
                : FText(
                    patientInfo.species == ''
                        ? i18nBook.remedical.notFill.t
                        : controller.getValues(
                            patientInfo.species ?? i18nBook.remedical.notFill.t,
                          ),
                  ),
          ),
        ],
      ),
    );
  }

  ///品种
  FWidget _buildVarietyInfo(PatientInfo patientInfo) {
    return controller.state.patientType == OrganizationPatientTypeEnum.Person
        ? FContainer()
        : FContainer(
            margin: EdgeInsets.only(bottom: 15),
            child: FRow(
              children: [
                FContainer(
                  width: 200,
                  child: _buildPatientInfoLable(i18nBook.remedical.variety.t),
                ),
                FContainer(
                  width: 200,
                  child: FText(
                    patientInfo.breed == ''
                        ? i18nBook.remedical.notFill.t
                        : patientInfo.breed!,
                  ),
                ),
              ],
            ),
          );
  }

  FWidget _buildPatientInfoLable(String text, {double fontSize = 14}) {
    return FText(
      text,
      style: TextStyle(
        fontSize: fontSize,
        fontWeight: FontWeight.bold,
      ),
    );
  }

  ///修改病人信息
  FWidget _modifyPatientInfo(PatientInfo patientInfo) {
    return FContainer(
      margin: EdgeInsets.only(
        top: 20,
      ),
      padding: EdgeInsets.symmetric(vertical: 15),
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(
            color: Color.fromRGBO(216, 216, 216, 1),
            width: 1,
          ),
          bottom: BorderSide(
            color: Color.fromRGBO(216, 216, 216, 1),
            width: 1,
          ),
        ),
      ),
      child: FRow(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          FContainer(
            color: Colors.white,
            child: FColumn(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                FElevatedButton(
                  businessParent: this,
                  name: "editPatient:${patientInfo.name}",
                  onPressed: () {},
                  style: ButtonStyle(
                    padding: MaterialStateProperty.all(
                      EdgeInsets.symmetric(
                        vertical: 15,
                        horizontal: 25,
                      ),
                    ),
                    alignment: Alignment.center,
                  ),
                  child: FText(i18nBook.common.modify.t),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
