import 'package:fis_lib_media_rt/implementations/nt_rtmp/channel/channel.dart';
import 'package:fis_lib_media_rt/implementations/nt_rtmp/members.dart';
import 'package:fis_lib_media_rt/implementations/nt_rtmp/models.dart';
import 'package:flutter/material.dart';

import '../iframe.dart';

class InnerView extends StatefulWidget {
  final NTPlayChannel channel;
  const InnerView({super.key, required this.channel});

  @override
  State<StatefulWidget> createState() => _InnerViewState();
}

class _InnerViewState extends State<InnerView> {
  late final RtmpLiveInfo liveInfo;

  @override
  void initState() {
    liveInfo = widget.channel.liveInfo;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WebIframeWidget(
      src: _buildSrc(),
      onCreated: (id) {
        widget.channel.viewId = id;
      },
    );
  }

  String _buildSrc() {
    if ((widget.channel.member as NTRemoteMember).isDevice) {
      final info = liveInfo as DeviceRtmpLiveInfo;
      final merge = info.isMerge ? 1 : 0;
      final params =
          "&merge=$merge&w=${info.width}&h=${info.height}&fw=${info.mergeWidth}&fh=${info.mergeHeight}";
      return "./wwwroot/remote_flv_view.html?url=${liveInfo.flvUrl}$params";
    } else {
      return "./wwwroot/remote_flv_view.html?url=${liveInfo.flvUrl}";
    }
  }
}
