// Class: openfl.net.NetStream

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../hxClasses_stub").default;
var $import = require("./../../import_stub").default;
var $bind = require("./../../bind_stub").default;
var $extend = require("./../../extend_stub").default;
function openfl_events_EventDispatcher() {return require("./../../openfl/events/EventDispatcher");}
function haxe_Timer() {return require("./../../haxe/Timer");}
function openfl_events_NetStatusEvent() {return require("./../../openfl/events/NetStatusEvent");}
function js_Browser() {return require("./../../js/Browser");}

// Constructor

var NetStream = function(connection,peerID) {
	(openfl_events_EventDispatcher().default).call(this);
	this.__connection = connection;
	this.__video = (js_Browser().default).get_document().createElement("video");
	this.__video.setAttribute("playsinline","");
	this.__video.setAttribute("webkit-playsinline","");
	this.__video.addEventListener("error",$bind(this,this.video_onError),false);
	this.__video.addEventListener("waiting",$bind(this,this.video_onWaiting),false);
	this.__video.addEventListener("ended",$bind(this,this.video_onEnd),false);
	this.__video.addEventListener("pause",$bind(this,this.video_onPause),false);
	this.__video.addEventListener("seeking",$bind(this,this.video_onSeeking),false);
	this.__video.addEventListener("playing",$bind(this,this.video_onPlaying),false);
	this.__video.addEventListener("timeupdate",$bind(this,this.video_onTimeUpdate),false);
	this.__video.addEventListener("loadstart",$bind(this,this.video_onLoadStart),false);
	this.__video.addEventListener("stalled",$bind(this,this.video_onStalled),false);
	this.__video.addEventListener("durationchanged",$bind(this,this.video_onDurationChanged),false);
	this.__video.addEventListener("canplay",$bind(this,this.video_onCanPlay),false);
	this.__video.addEventListener("canplaythrough",$bind(this,this.video_onCanPlayThrough),false);
	this.__video.addEventListener("loadedmetadata",$bind(this,this.video_onLoadMetaData),false);
}

// Meta

NetStream.__name__ = ["openfl","net","NetStream"];
NetStream.__super__ = (openfl_events_EventDispatcher().default);
NetStream.prototype = $extend((openfl_events_EventDispatcher().default).prototype, {
	close: function() {
		this.__video.pause();
		this.__video.src = "";
		this.time = 0;
	},
	pause: function() {
		this.__video.pause();
	},
	play: function(url,_,_1,_2,_3,_4) {
		this.__video.src = url;
		this.__video.play();
	},
	requestVideoStatus: function() {
		var _gthis = this;
		if(this.__timer == null) {
			this.__timer = new (haxe_Timer().default)(1);
		}
		this.__timer.run = function() {
			if(_gthis.__video.paused) {
				_gthis.__playStatus("NetStream.Play.pause");
			} else {
				_gthis.__playStatus("NetStream.Play.playing");
			}
			_gthis.__timer.stop();
		};
	},
	resume: function() {
		this.__video.play();
	},
	seek: function(time) {
		if(time < 0) {
			time = 0;
		} else if(time > this.__video.duration) {
			time = this.__video.duration;
		}
		this.__video.currentTime = time;
		this.__connection.dispatchEvent(new (openfl_events_NetStatusEvent().default)("netStatus",false,false,{ code : "NetStream.Seek.Complete"}));
	},
	togglePause: function() {
		if(this.__video.paused) {
			this.__video.play();
		} else {
			this.__video.pause();
		}
	},
	__playStatus: function(code) {
		if(this.client != null) {
			try {
				var handler = this.client.onPlayStatus;
				handler({ code : code, duration : this.__video.duration, position : this.__video.currentTime, speed : this.__video.playbackRate, start : this.__video.startTime});
			} catch( e ) {
			}
		}
	},
	video_onCanPlay: function(event) {
		this.__playStatus("NetStream.Play.canplay");
	},
	video_onCanPlayThrough: function(event) {
		this.__playStatus("NetStream.Play.canplaythrough");
	},
	video_onDurationChanged: function(event) {
		this.__playStatus("NetStream.Play.durationchanged");
	},
	video_onEnd: function(event) {
		this.__connection.dispatchEvent(new (openfl_events_NetStatusEvent().default)("netStatus",false,false,{ code : "NetStream.Play.Stop"}));
		this.__playStatus("NetStream.Play.Complete");
	},
	video_onError: function(event) {
		this.__connection.dispatchEvent(new (openfl_events_NetStatusEvent().default)("netStatus",false,false,{ code : "NetStream.Play.Stop"}));
		this.__playStatus("NetStream.Play.error");
	},
	video_onLoadMetaData: function(event) {
		if(this.client != null) {
			try {
				var handler = this.client.onMetaData;
				handler({ width : this.__video.videoWidth, height : this.__video.videoHeight, duration : this.__video.duration});
			} catch( e ) {
			}
		}
	},
	video_onLoadStart: function(event) {
		this.__playStatus("NetStream.Play.loadstart");
	},
	video_onPause: function(event) {
		this.__playStatus("NetStream.Play.pause");
	},
	video_onPlaying: function(event) {
		this.__connection.dispatchEvent(new (openfl_events_NetStatusEvent().default)("netStatus",false,false,{ code : "NetStream.Play.Start"}));
		this.__playStatus("NetStream.Play.playing");
	},
	video_onSeeking: function(event) {
		this.__playStatus("NetStream.Play.seeking");
	},
	video_onStalled: function(event) {
		this.__playStatus("NetStream.Play.stalled");
	},
	video_onTimeUpdate: function(event) {
		this.time = this.__video.currentTime;
		this.__playStatus("NetStream.Play.timeupdate");
	},
	video_onWaiting: function(event) {
		this.__playStatus("NetStream.Play.waiting");
	},
	get_speed: function() {
		return this.__video.playbackRate;
	},
	set_speed: function(value) {
		return this.__video.playbackRate = value;
	}
});
NetStream.prototype.__class__ = $hxClasses["openfl.net.NetStream"] = NetStream;

// Init

Object.defineProperty(NetStream.prototype,"speed",{ get : function () { return this.get_speed (); }, set : function (v) { return this.set_speed (v); }});

// Statics




// Export

exports.default = NetStream;