import 'dart:async';
import 'dart:core';

import 'package:flutter/material.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';

import 'draggable_cam.dart';

class CamViewWidget extends StatefulWidget {
  RTCVideoRenderer localRenderer;
  RTCVideoRenderer remoteRenderer;
  MediaStream stream;
  BoxConstraints constraints;
  StreamController<bool> onButtonBarVisibleStreamController;
  StreamController<double> onButtonBarHeightStreamController;

  CamViewWidget({this.localRenderer, this.remoteRenderer, this.stream, this.constraints, this.onButtonBarVisibleStreamController, this.onButtonBarHeightStreamController});

  @override
  _CamViewWidgetState createState() => _CamViewWidgetState();
}

class _CamViewWidgetState extends State<CamViewWidget> {
  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(milliseconds: 300), () {
      showCamera();
    });
  }

  showCamera() async {
    setState(() async {
      widget.localRenderer.srcObject = widget.stream;
      widget.remoteRenderer.srcObject = widget.stream;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Stack(
        children: [
          Container(
            child: RTCVideoView(widget.localRenderer, mirror: true),
          ),
          DraggableCam(
            key: Key('publisher'),
            onButtonBarHeight: widget.onButtonBarHeightStreamController.stream,
            onButtonBarVisible: widget.onButtonBarVisibleStreamController.stream,
            availableScreenSize: widget.constraints.biggest,
            child: RTCVideoView(widget.remoteRenderer),
          ),
          // Expanded(child: RTCVideoView(widget.remoteRenderer)),
        ],
      ),
    );
  }
}
