
vk.require("dcore.FileUtil");
vk.require("dijit.form.Button");

vk.declare("vk.Capture",[vkw._Widget,vkw._Templated],{
    templatePath: "js/Capture.html",
    widgetsInTemplate: true,
    previewControls: null,
    deviceSelection: null,
    viewer: null,
    _outPath: "",
    _ext: "",
    _futil: null,
    _audio: "",
    _video: "",
    _state: "stopped", // stopped,recording,previewing,deviceinit
    _devicestream: null,
    _outstream: null,
    _avstream: null,
    constructor: function() {
        vk.connect(window, "onbeforeunload",this, "destroy");
        var ext = "";
        if (navigator.userAgent.indexOf("Win")!=-1) {
            this._ext = ".avi";
        }
        if (navigator.userAgent.indexOf("Mac")!=-1) {
            this._ext =".mov";
        }

        //if (navigator.userAgent.indexOf("Windows NT 6.1")!=-1) {
        //    this._isWin7 = true;
        //} else {
        //    this._isWin7 = false;
       //}

        this.futil = new dcore.FileUtil({});
        this.futil.getUserSandbox({
            handle: vk.hitch(this,function(value) {
                //if (this._isWin7) {
                //    this._outPath = value + "\\AppData\\LocalLow\\capturedemo" +this._ext;
                //} else {
                    this._outPath = value + "/capturedemo" +this._ext;
                //}
                console.log(this._outPath);
            }),
            error: function(err) {
                console.log("Publish.getUserSandbox err.name=" + err.name);
            }
        });
    },
    postCreate: function() {
        vk.connect(this.videoselector,"onDeviceChange",this,"devicechange");
        vk.connect(this.audioselector,"onDeviceChange",this,"devicechange");
        
    },
    devicechange: function(val) {
        console.dir(val);
        var dirty = false;
        if (val.type === "video") {
            this._video = val.value;
            dirty = true;
        } else if (val.type === "audio") {
            this._audio = val.value;
            dirty = true;
        }
        
        if (this._video === "none") {
            this.time.innerHTML = "No Video Devices Found";
            vk.style(this.time,"color","red");
            return;
        } else {
            vk.style(this.time,"color","white");
        }
        if (this._video !== "" && this._audio !== "" && this._state === "stopped") {
            console.log("device selected");
            //debugger;

            if (dirty) {
                if (this._devicestream === null) {

                    this._devicestream = new dcore.DeviceInputStream({
                        audio: this._audio,
                        video: this._video,
                        height: 240,
                        width: 320
                    },{
                        onStateChange: function(state) {
                            if (state === "playing") {
                                this._devicestream.setViewer(this.viewer.id);
                            }
                        }
                    },this).setViewer(this.viewer.id);
                    this._devicestream.play();

                } else {
                    //this._devicestream.stop();
                    this.videoselector.setDisabled(true);
                    this.audioselector.setDisabled(true);
                    this._devicestream.stop();
                    
                    var df = this._devicestream.setAVDevice({
                        audio: this._audio,
                        video: this._video
                    });
                    df.addCallback(vk.hitch(this, function() {
                        console.log("setAVDevice callback");

                        this._devicestream.play();
                        this._devicestream.setViewer(this.viewer.id);
                        this.videoselector.setDisabled(false);
                        this.audioselector.setDisabled(false);
                    }))
                }
            } else {
                this._devicestream.stop();
                this._devicestream.play();
            }
            this.time.innerHTML = "Click the Record Button to Start...";

            this.button.attr("disabled",false);
        //vk.style(this.button,"backgroundImage","images/recordbutton.jpg");

        }
    },
    _buttonClick: function() {
        
        switch (this._state) {
            case "stopped":
                this._setState("recording");
                break;
            case "recording":
                //this._setState("previewing");
                this._outstream.unpublish();
                break;
            case "previewing":
                //this._setState("stopped");
                break;
        }
    },
    _setState: function(state) {
        console.log("current state: " + this._state + " new state: " + state);
        switch (state) {
            case "stopped":
                this._avstream.close();
                this._avstream = null;
                this._state = "stopped";
                this.time.innerHTML = "Click the Record Button to start...";
                this.videoselector.setDisabled(false);
                this.audioselector.setDisabled(false);
                this.button.attr("disabled",false);
                this.button.attr("label","Record");
                //this.button.style.backgroundImage = "url(images/recordbutton.jpg)";
                this.devicechange("");
                break;
            case "recording":
                
                this._state = "recording";
                this.videoselector.setDisabled(true);
                this.audioselector.setDisabled(true);
                this.button.attr("disabled",false);
                this.button.attr("label","Stop");
                this.button.style.backgroundImage = "url(images/recordstop.jpg)";
                this._outstream = new dcore.AVOutputStream({
                    path: this._outPath,
                    onStateChange: vk.hitch(this,function(state) {
                        console.log(state);
                        if (state === "stopped") {
                            if (this._state !== "stopped" && this._state !== "previewing") {
                                //debugger;
                                this._setState("previewing");
                            }
                        }
                    }),
                    onProgressChange: vk.hitch(this,function(value) {
                        console.log(value.time);
                        this.time.innerHTML = "Recording: " + value.time + " seconds";
                    })
                });
                this._outstream.setInputStream(this._devicestream);
                this._outstream.publish();
                break;
            case "previewing":
                //debugger;
                this._state = "previewing";
                this.videoselector.setDisabled(true);
                this.audioselector.setDisabled(true);
                this.button.attr("label","Record");
                this.button.attr("disabled",true);
                //this.button.style.backgroundImage = "url(images/recordbuttondisabled.jpg)";
                //this._outstream.unpublish();
                this._outstream._close();
                this._avstream = new dcore.AVInputStream({
                    file: this._outPath
                },
                {
                    onStateChange: vk.hitch(this,function(state) {
                        console.log(state);
                        if (state === "playing") {
                            this.time.innerHTML = "Playing back recorded file";
                        }
                        if (state === "ended") {
                            this._setState("stopped");
                        }
                    })
                },this);
                this._avstream.setViewer(this.viewer.id);
                this._avstream.play();
                break;
        }
    },
    destroy: function() {
        if (this._avstream !== null) {
            this._avstream.close();
            this._avstream = null;
        }
        if (this._outstream !== null) {
            this._outstream._close();
            this._outstream = null;
        }
        if (this._devicestream !== null) {
            this._devicestream.close();
            this._devicestream = null;
        }
        this.inherited(arguments);
    }
});
