import 'package:fis_lib_media_rt/implementations/nt_rtmp/channel/channel.dart';
import 'package:fis_lib_media_rt/implementations/nt_rtmp/widgets/remote_view/remote_view.dart';
import 'package:fis_lib_media_rt/implementations/trtc/channel.dart';
import 'package:fis_lib_media_rt/implementations/trtc/widgets/remote_device_view.dart';
import 'package:flutter/material.dart';
import '../config.dart';
import '../interface/channel.dart';

/// 超声机实时画面视图（单路，可能需要裁切）
class FRTDeviceChannelView extends StatelessWidget {
  /// 播放路源
  final IPlayChannel channel;

  /// 视图尺寸
  final Size viewSize;

  /// 指定尺寸
  final Size? size;

  const FRTDeviceChannelView(
    this.channel, {
    super.key,
    this.size,
    required this.viewSize,
  });

  @override
  Widget build(BuildContext context) {
    return Builder(builder: (context) {
      double scale = 1;
      Offset extraOffset = Offset.zero;
      double extraScale = 1;
      // 判断是否是需要缩放
      final realVideoSize = selectResolution(viewSize);
      final canvasAspectRatio = realVideoSize.width / realVideoSize.height;
      final imageAspectRatio = viewSize.width / viewSize.height;
      if (canvasAspectRatio < imageAspectRatio) {
        // 画布较宽，需要调整图像宽度以适应画布宽度
        extraScale = realVideoSize.width / viewSize.width;
        extraOffset = Offset(
          0,
          (realVideoSize.height - viewSize.height * extraScale) /
              2 /
              realVideoSize.height,
        );
      } else {
        // 画布较高，需要调整图像高度以适应画布高度
        extraScale = realVideoSize.height / viewSize.height;
        extraOffset = Offset(
          (realVideoSize.width - viewSize.width * extraScale) /
              2 /
              realVideoSize.width,
          0,
        );
      }
      scale = scale * extraScale;
      return Container(
        color: Colors.black,
        child: Center(
          child: AspectRatio(
            aspectRatio: realVideoSize.width / realVideoSize.height,
            child: ClipRect(
              child: FractionalTranslation(
                translation: extraOffset,
                child: Transform.scale(
                  alignment: Alignment.topLeft,
                  scale: scale,
                  child: _buildView(),
                ),
              ),
            ),
          ),
        ),
      );
    });
  }

  Widget _buildView() {
    final type = FRTMediaConfig.serviceType;
    if (type == FRealTimeMediaServiceType.ntRtmp) {
      return NTRemoteView(channel: channel as NTPlayChannel);
    } else if (type == FRealTimeMediaServiceType.vrtc) {
//
    }
    return TrtcRemoteDeviceView(channel as TrtcPlayChannel);
  }

  Size selectResolution(Size size) {
    final width = size.width;
    final height = size.height;
    if (width <= 640 && height <= 480) {
      return const Size(640, 480);
    } else if (width <= 960 && height <= 544) {
      return const Size(960, 540);
    } else if (width <= 960 && height <= 720) {
      return const Size(960, 720);
    } else if (width <= 1280 && height <= 720) {
      return const Size(1280, 720);
    } else {
      return const Size(1920, 1080);
    }
  }
}
