/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书

 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer
 * Software" and other relevant administrative regulations and international treaties. Without
 * the written permission of the Company, no person may use (including but not limited to the
 * illegal copy, distribute, display, image, upload, and download) and disclose the above
 * technical documents to any third party. Otherwise, any infringer shall afford the legal
 * liability to the company.
 */
/**
 * Created by heweijun on 2015/8/7.
 */
define(["adoreapp/widget/BaseWidget", "adoreapp/widget/EventHub", "adoreapp/hotkey/Keys"], function (BaseWidget, EventHub, Keys) {
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                this.jqueryNode = $(attrs.template).attr("id", this.id);
                navigator.getUserMedia = navigator.getUserMedia ||
                                         navigator.webkitGetUserMedia ||
                                         navigator.mozGetUserMedia;
                var exArray = [];//存储设备源ID

                function gotSources(sourceInfos) {
                    for(var i=0; i != sourceInfos.length; i++) {
                        var sourceInfo = sourceInfos[i];
                        if(sourceInfo.kind === 'video') {
                            exArray[i] = sourceInfo.id;
                        }
                    }
                }

                if(typeof MediaStreamTrack === 'undefined') {
                    alert("this browser does not support MediaStreamTrack.");
                } else {
                    MediaStreamTrack.getSources(gotSources);
                }

                node = this.jqueryNode;
                var $video = node.find("video");
                var video = $video.get(0);

                function successCallback(stream) {
                    window.stream = stream;
                    var url = window.URL.createObjectURL(stream);
                    $video.attr("src",url);
                    video.play();
                }

                function errorCallback(error) {
                    console.log("navigator.getUserMedia error: ", error);
                }

                   function start() {
                    if(!!window.stream) {
                        $video.attr("src",null);
                        window.stream.stop();
                    }
                    var constraints = {
                        video:{
                            optional: [{sourceId: exArray[3]}]
                        }
                    };
                    navigator.getUserMedia(constraints, successCallback, errorCallback);
                    }



                var $start = node.find("#b1");
                $start.click(start);

                var $capture = node.find("#b2");
                $capture.click(function() {
                    var canvas = document.createElement("canvas");
                    var context = canvas.getContext("2d");
                    context.drawImage(video, 0, 0, 640, 480);
                    var link = document.createElement('a');
                    link.download = "test.png";
                    link.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
                    link.click();
                    alert("拍照成功！");
                });
            },

            _layout: function () {
                base._layout.call(this);
            }
        }
    })
})

