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

import 'package:diplomaticquarterapp/pages/conference/widgets/noise_box.dart';
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 localStream;
  BoxConstraints constraints;
  StreamController<bool> onButtonBarVisibleStreamController;
  StreamController<double> onButtonBarHeightStreamController;

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

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

class _CamViewWidgetState extends State<CamViewWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Stack(
        children: [
          FractionallySizedBox(
            heightFactor: 1, widthFactor: 1,
            child: Container(
              color: Colors.black87,
              child: RTCVideoView(widget.remoteRenderer, mirror: true,filterQuality: FilterQuality.medium,),
            ),
          ),

          if(widget.remoteRenderer.srcObject == null)
            Positioned.fill(child: _buildNoiseBox()),

          Positioned.fill(
              child: RTCVideoView(widget.remoteRenderer)
          ),

          DraggableCam(
            key: Key('publisher'),
            onButtonBarHeight: widget.onButtonBarHeightStreamController.stream,
            onButtonBarVisible: widget.onButtonBarVisibleStreamController.stream,
            availableScreenSize: widget.constraints.biggest,
            child: RTCVideoView(widget.localRenderer)
          ),

          if(widget.remoteRenderer.srcObject == null)
            Container(
                margin: EdgeInsets.all(MediaQuery.of(context).size.width/8),
                child: Text(
                  'Waiting for another participant to connect to the call...',
                  key: Key('text-wait'),
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white),
                )
            ),
        ],
      ),
    );
  }


  Widget _buildNoiseBox() {
    return NoiseBox(
      density: NoiseBoxDensity.xHigh,
      backgroundColor: Colors.grey.shade900,
    );
  }
}
