/**
 * Created by tangqianchong on 2015/12/23.
 */
define(["adoreapp/widget/BaseWidget"], function (BaseWidget) {
    "use strict";
    return BaseWidget.extend(function (base) {
        return {
            //初始化只需要帮ID设置初始值就好
            _initWidget: function (attrs) {
                this.jqueryNode = $(attrs.template).attr({id: this.id});
                this.jqueryNode.find("video").attr("id", this.id + "-content");
                this.content = this.jqueryNode.find("video");
            },
            _setState: function (attrs) {
                var state = attrs.state;
                if (state === "start") {
                    this.content.play();
                }
                else if (state === "stop") {
                    this.content.pause();
                }
            },
            //设置视频路径（只支持mp4格式的视频）
            _setVideoPath: function (attrs) {
                if (attrs.videoPath) {
                    this.videoPath = attrs.videoPath;
                    this.appendVideoSrc();
                    this.content.get(0).load();
                }
            },
            //设置是否自动播放
            _setAutoPlay: function (attrs) {
                if (attrs.autoPlay) {
                    this.content.attr("autoplay", "autoplay");
                }
            },
            appendVideoSrc: function () {
                if (!this.videoPath) {
                    alert("视频地址尚未设置");
                    return;
                }
                var videoSource = $("<source>").attr("src", this.videoPath).attr("type", "video/mp4");
                this.content.append(videoSource);
            },
            _layout: function () {
                base._layout.call(this);
                var tempWidth = parseInt(this.jqueryNode.css("width"));
                var tempHeight = parseInt(this.jqueryNode.css("height"));
                this.content.attr("width", tempWidth - 6);
                this.content.attr("height", tempHeight - 6);
            }
        };
    });
});
