var RequestVideo = require("request/video");    //视频相关API
var Store = require("store");
var _ = require("underscore");
var Util = require("util");

var tpl_player = __inline("./tpl/player.tmpl");

class Player {
    constructor(_opts) {
        //配置
        var opts = {};
        opts.__PLAYVID__ = _opts.__PLAYVID__;
        opts.$wrap = $(_opts.$wrap);
        opts.comment3_domain = _opts.comment3_domain;
        opts.comment3_url = _opts.comment3_url;
        opts.comment3_uniqid = _opts.comment3_uniqid;
        opts.element = _opts.element;
        opts.video_url = _opts.video_url;   //视频地址
        opts.video_big_cover = _opts.video_big_cover;   //视频封面
        opts.video_title = _opts.video_title;   //视频标题
        opts.video_channel = _opts.video_channel;   //视频所属频道
        opts.video_upload_time = _opts.video_upload_time;   //视频发布时间
        opts.vid = _opts.vid;   //视频id
        opts.video_play_num = _opts.video_play_num;     //视频播放数
        opts.video_support = _opts.video_support;   //视频点赞数
        opts.video_raw_support = _opts.video_raw_support;
        opts.video_hotlink_intro = _opts.video_hotlink_intro;   //视频简介
        opts.video_tag_links = _opts.video_tag_links;   //视频相关标签对象
        opts.video_comment_num = _opts.video_comment_num;   //视频评论数
        opts.user_id = _opts.user_id;   //作者id
        opts.user_avatar = _opts.user_avatar;   //作者头像
        opts.user_nickname = _opts.user_nickname;   //作者名称
        opts.user_homepage = _opts.user_homepage;   //用户个人中心地址
        opts.shareUrl = `http://video.duowan.com/play/${_opts.vid}.html`;
        opts.shareWeiboUrl = `http://service.weibo.com/share/mobile.php?appkey=1343713053&url=${opts.shareUrl}&title=${opts.video_title}&appkey=1343713053&searchPic=true`;;
        opts.shareQQurl = `https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=${opts.shareUrl}&title=${opts.video_title}&thumbnail=90*90&desc=${opts.video_hotlink_intro}`;


        this.opts = opts;

        this.ReqVideo = new RequestVideo();

        this.init();
    }

    init() {
        /** 渲染播放器主体框架 */
        this.render();

        /** 获取本视频的被收藏数 */
        this.getCollectNumByVid();

        /** 事件绑定 */
        this.bindEvent();
    }

    render() {
        var opts = this.opts;
        var dom = tpl_player({
            data: opts
        });
        opts.$wrap.append(dom);
    }

    getCollectNumByVid () {
        var opts = this.opts;
        this.ReqVideo.getCollectNumByVid(opts.vid).done(function (ret) {
            $(`#player${opts.vid}`).find(".video-collect em").text(ret.map[opts.vid]);
        })
    }

    //事件绑定
    bindEvent(){
        var self = this;
        var opts = this.opts;
        var $parent  = $(`#player${opts.vid}`);

        /** 点赞 */
        $parent.on("click", ".video-agree", _.debounce( function() {
            if($(this).attr("data-do") == 1) {
                return;
            }

            var vid = $(this).attr("data-vid");
            var zanObj = Store.get("userSupport") || {};
            self.ReqVideo.doSupport(vid).done((ret) => {
                $(this).attr("data-do", 1);
                if(ret.rs == 1) {
                    $(this).addClass("clicked").html(`<em>${parseInt(opts.video_raw_support) + 1}</em>`)
                    zanObj[vid] = 1;
                    Store.set("userSupport", zanObj);
                } else {
                    alert("你已经点过赞了");
                }
            })
        }, 200));

        /** 收藏 */
        $parent.on("click", ".video-collect", _.debounce( function() {
            if(!Util.isLoggedIn()){
                var href = location.href;
                location.href="http://www.duowan.com/mlogin/?calbackUrl="+href;
                return;
            }

            if($(this).attr("data-do") == 1) {
                return;
            }

            var vid = $(this).attr("data-vid");
            var num = parseInt($(this).text());
            self.ReqVideo.doCollectByVid(vid).done((ret) => {
                $(this).attr("data-do", 1);
                if(ret.rs) {
                    $(this).addClass("clicked").html(`<em>${num + 1}</em>`)
                } else {
                    alert("你已经收藏过了");
                }
            })
        }, 200));

        /** 订阅 */
        $parent.on("click", ".follow-btn", _.debounce( function() {
            if(!Util.isLoggedIn()){
                var href = location.href;
                location.href="http://www.duowan.com/mlogin/?calbackUrl="+href;
                return;
            }

            if($(this).hasClass("followed")) {
                return;
            }

            var uid = $(this).attr("data-uid");
            if(uid) {
                self.ReqVideo.doSubscribe(uid).done((ret) => {
                    if(ret.rs) {
                        $(this).removeClass('not-follow').addClass("followed").text("已订阅");
                    }
                })
            }
        }, 200));
    }
}
module.exports = Player;






























