import 'package:fis_jsonrpc/services/appletAPI.m.dart';
import 'package:fis_lib_media_rt/interface/index.dart';
import 'package:fis_lib_media_rt/widgets/index.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/router/mobile/names.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/widgets/video_loading.dart';
import 'package:flyinsono/views/wechat_mini_program/wx_public_widgets/wx_scaffold.dart';
import 'package:flyinsono/views/wechat_mini_program/wx_public_widgets/wx_screen_direction_lock.dart';
import 'package:get/get.dart';
import 'index.dart';

class WxConsultationRoomPage extends FisView<WxConsultationRoomController> {
  static const double _smallWindowWidth = 280;
  static const double _smallWindowHeight = 160;

  /// 左侧小窗口尺寸
  final Size? __smallWindowSize = Size(
    _smallWindowWidth,
    _smallWindowHeight,
  );
  WxConsultationRoomPage({Key? key}) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FisBuilder<WxConsultationRoomController>(
      builder: (_) {
        return WxScreenDirectionLock(
          direction: ScreenDirection.horizontal,
          page: WxScaffold(
            body: _buildMainView(),
            title: "会诊房间",
            parentRoute: MobileRouteNames.Wechat.MiniProgramConsultation,
          ),
        );
      },
    );
  }

  // 主视图
  Widget _buildMainView() {
    return Container(
      color: Colors.black,
      child: Row(
        children: [
          _buildLeftViewsList(),
          Expanded(child: _buildDeviceMainView()),
        ],
      ),
    );
  }

  ///左侧区域视图
  Widget _buildLeftViewsList() {
    return Container(
      color: Color.fromARGB(255, 77, 77, 77),
      height: MediaQuery.of(Get.context!).size.height,
      width: _smallWindowWidth,
      child: ListView(
        children: [
          _buildDeviceSubView(),
          _buildCurrUserView(),
          _buildOtherUsersView(),
        ],
      ),
    );
  }

  /// 构建自己的摄像头内容
  Widget _buildCurrUserView() {
    return Obx(() {
      final ConsultationMemberModel currUser = controller.state.currUser;
      IChannel? channel = controller.getChannelById(currUser.trtcId);
      Widget channelView = VideoLoadingWidget();
      if (channel != null && channel is IPublishChannel) {
        channelView = FRTPublishPreviewView(
          channel,
          size: __smallWindowSize,
        );
      }
      return Container(
          height: _smallWindowHeight,
          width: _smallWindowWidth,
          color: Color.fromARGB(255, 77, 77, 77),
          child: channelView);
    });
  }

  /// 构建他人的摄像头内容
  Widget _buildOtherUsersView() {
    return Obx(() {
      final List<ConsultationMemberModel> othersList =
          controller.state.othersMemberList;
      Widget channelView = VideoLoadingWidget();
      IChannel? channel;
      if (othersList.length > 0) {
        channel = controller.getChannelById(othersList[0].trtcId);
      }
      if (channel != null && channel is IPlayChannel) {
        channelView = FRTPlayChannelView(
          channel,
          size: __smallWindowSize,
        );
      }
      return Container(
        height: _smallWindowHeight,
        width: _smallWindowWidth,
        color: Color.fromARGB(255, 77, 77, 77),
        child: channelView,
      );
    });
  }

  ///超声画面
  FWidget _buildDeviceMainView() {
    return FStack(
      fit: StackFit.expand,
      children: [
        FObx(
          () {
            final ConsultationMemberModel currDevice =
                controller.state.currDevice;
            IChannel? channel;
            if (currDevice.isEntered) {
              channel = controller.getChannelById(currDevice.trtcId);
            }
            if (channel != null && channel is IPlayChannel) {
              final LiveTerminalInfo2 device = controller.deviceInfo;

              /// 完整视频帧尺寸
              final Size videoSize = Size(
                (device.cameraWidth * 2).toDouble(),
                (device.cameraHeight / 2 + device.terminalHeight).toDouble(),
              );

              /// 超声画面尺寸
              final Size viewSize = Size(
                device.terminalWidth.toDouble(),
                device.terminalHeight.toDouble(),
              );
              return QuickFWidget(
                // 合图模式
                Center(
                  child: AspectRatio(
                    aspectRatio: viewSize.aspectRatio,
                    child: FRTMergedChannelView(
                      isSubView: false,
                      videoSize: videoSize,
                      viewSize: viewSize,
                      viewAlignment: Alignment.topLeft,
                      channel,
                    ),
                  ),
                ),
              );
            }
            return QuickFWidget(
              VideoLoadingWidget(),
            );
          },
        ),
      ],
    );
  }

  /// 扫查手法画面（超声画面子窗口）
  FWidget _buildDeviceSubView() {
    return FObx(
      () {
        final ConsultationMemberModel currDevice = controller.state.currDevice;
        final LiveTerminalInfo2 device = controller.deviceInfo;
        IChannel? channel;
        if (currDevice.isEntered && device.cameraLiveEnabled) {
          channel = controller.getChannelById(currDevice.trtcId);
        }
        if (channel != null && channel is IPlayChannel) {
          /// 完整视频帧尺寸
          final Size videoSize = Size(
            (device.cameraWidth * 2).toDouble(),
            (device.cameraHeight / 2 + device.terminalHeight).toDouble(),
          );

          /// 摄像头画面尺寸
          final Size viewSize = Size(
            device.cameraWidth.toDouble(),
            device.cameraHeight.toDouble(),
          );
          return QuickFWidget(
            // 合图模式
            AspectRatio(
              aspectRatio: viewSize.aspectRatio,
              child: FRTMergedChannelSubViewOldVersion(
                videoSize: videoSize,
                mainViewSize: viewSize,
                cameraViewSize: viewSize,
                viewAlignment: Alignment.topLeft,
                channel,
              ),
            ),
          );
        }
        return QuickFWidget(
          SizedBox(),
        );
      },
    );
  }
}
