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

import 'shell_camera.dart';

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

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

class _InnerViewState extends State<InnerView> {
  static const _previewLink = "http://stream.platform.fis.plus/rtmp_preview";

  late final ShellCameraPlayController _playController;

  @override
  void initState() {
    _playController = ShellCameraPlayController(_previewLink);
    _playController.init().then((value) {
      widget.channel.previewStateChanged.addListener(_onPreviewStateChanged);
      if (widget.channel.isPreviewing) {
        _playController.play();
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    widget.channel.previewStateChanged.removeListener(_onPreviewStateChanged);
    _playController.stop();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ShellCameraPlayer(controller: _playController);
  }

  void _onPreviewStateChanged(_, bool e) {
    if (e) {
      _playController.play();
    } else {
      _playController.stop();
    }
  }
}
