import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.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/managers/interfaces/appointment_consultation.dart';
import 'package:flyinsono/managers/interfaces/entities/consultation_member.dart';
import 'package:flyinsono/managers/interfaces/entities/live_consultation.dart';
import 'package:flyinsono/managers/interfaces/live_consultation.dart';
import 'package:flyinsono/pages/main/home/widgets/dialog_page.dart';
import 'package:get/get.dart';

///会诊请求对话框
class ConsulationRequestDialog extends FisView implements FPage {
  static const double dialogWidth = 180;
  static const double radius = 80;
  static const double circleDiameter = 80;
  static const double bottomSpacing = 40;
  final LiveConsultation liveConsultation;
  final VoidCallback? clickCallBack;
  final bool isJoin;
  final appointmentConsultationManager = Get.find<IAppointmentConsultation>();
  final liveConsultationManager = Get.find<ILiveConsultationManager>();
  ConsulationRequestDialog(
    this.liveConsultation,
    this.clickCallBack,
    this.isJoin,
  );
  @override
  String get pageName => 'ConsulationRequestDialog';
  @override
  FWidget build(BuildContext context) {
    ///获取发起者信息
    var initiaor = liveConsultation.memberLiveDatas![0];
    if (liveConsultation.operator != null) {
      initiaor = ConsultationMember.fromDto(liveConsultation.operator!)
          .fixMergedVideoOutputSize()
          .fixOnlyCamera();
    }
    var consultationCode = liveConsultation.consultationCode ?? '';
    if (FPlatform.isAndroid || FPlatform.isIOS) {
      return _buildMobileDialog(consultationCode);
    }
    return FOverlayContainer(
      child: FSimpleDialog(
        isDefault: true,
        okString: i18nBook.realTimeConsultation.accept.t,
        cancelString: i18nBook.user.refuse.t,
        contentPadding: EdgeInsets.all(FSpacings.XXL),
        onCancel: () {
          router.back();
          clickCallBack?.call();
          liveConsultationManager.rejectLiveConsultationAsync(consultationCode);
        },
        onOk: () {
          clickCallBack?.call();
          _openConsultationPage(consultationCode);
        },
        children: [
          FSizedBox(height: FSpacings.XXL),
          if (initiaor.headImageToken.isNullOrEmpty) ...[
            FCircleAvatar(
              backgroundImage: AssetImage('assets/images/avatar_man.png'),
              radius: radius,
            ),
          ] else ...[
            FCircleAvatar(
              backgroundImage: NetworkImage(initiaor.headImageToken!),
              radius: radius,
            ),
          ],
          FContainer(
            margin: EdgeInsets.symmetric(horizontal: FSpacings.XXL),
            width: dialogWidth,
            child: FSizedBox(height: FSpacings.XXL),
          ),
          FAlign(
            alignment: Alignment.center,
            child: FText(
              i18nBook.notification.consultationInvitation
                  .translate([initiaor.name]),
            ),
          ),
        ],
      ),
    );
  }

  ///手机端弹窗
  FWidget _buildMobileDialog(String consultationCode) {
    ///获取发起者信息
    var initiaor = liveConsultation.memberLiveDatas![0];
    var headImageToken = initiaor.headImageToken ?? '';
    return FContainer(
      color: Color.fromRGBO(39, 40, 34, 1.0),
      child: FColumn(
        children: [
          const FExpanded(child: FSizedBox()),
          FColumn(
            children: [
              _buildAvatar(headImageToken),
              const FSizedBox(height: FSpacings.L),
              FAlign(
                alignment: Alignment.center,
                child: FText(
                  i18nBook.notification.consultationInvitation
                      .translate([initiaor.name]),
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
              ),
            ],
          ),
          const FExpanded(child: FSizedBox()),
          FRow(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              //挂断按钮
              _buildCircularOperationButton(
                i18nBook.user.refuse.t,
                () {
                  liveConsultationManager
                      .rejectLiveConsultationAsync(consultationCode);
                  router.back();
                },
                true,
              ),
              //接听按钮
              _buildCircularOperationButton(
                i18nBook.realTimeConsultation.accept.t,
                () {
                  _openConsultationPage(consultationCode);
                },
                false,
              ),
            ],
          ),
        ],
      ),
    );
  }

  //头像
  FWidget _buildAvatar(String headImageToken) {
    return FContainer(
      child: FCenter(
        child: headImageToken.isEmpty
            ? FCircleAvatar(
                backgroundImage: AssetImage('assets/images/avatar_man.png'),
                radius: radius,
              )
            : FCircleAvatar(
                backgroundImage: NetworkImage(headImageToken),
                radius: radius,
              ),
      ),
    );
  }

  ///接听or挂断按钮
  FWidget _buildCircularOperationButton(
      String title, GestureTapCallback onTap, bool isHangUp) {
    return FGestureDetector(
      businessParent: this,
      onTap: onTap,
      name: title,
      child: FColumn(
        children: [
          FContainer(
            margin: EdgeInsets.only(bottom: FSpacings.M),
            width: circleDiameter,
            height: circleDiameter,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: isHangUp ? Colors.red : Colors.green,
            ),
            child: FIcon(
              isHangUp ? Icons.call_end : Icons.call,
              color: Colors.white,
            ),
          ),
          FText(
            isHangUp
                ? i18nBook.user.refuse.t
                : i18nBook.realTimeConsultation.accept.t,
          ),
          const FSizedBox(height: bottomSpacing),
        ],
      ),
    );
  }

  void _openConsultationPage(String consultationCode) {
    router.back();
    appointmentConsultationManager.startConsultation(consultationCode,
        isJoin: isJoin, pageIndex: 1);
  }
}
