/* ================================================================================
 * dever.js v1.0
 * http://git.shemic.com/dever/script
 * ================================================================================
 * Copyright 2017-2018 Dever(dever.cc)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */

 //<filter><script src="../js/lib/jquery.min.js"></script><script src="../js/dever.js"></script></filter>


//分享 暂时只支持手机版微信分享 请同时安装wechat组件
var Dever_Share = 
{
    wechat : false,
    url : '',
    project : '',
    uid : '-1',
    param : {
        title : '',
        img : '',
        desc : '',
        url : '',
        timelineCallback: function() {},
        sendMessageCallback: function() {},
        shareQQCallback: function() {}
    },
    Init : function(uid, project, url, param, button)
    {
        var self = this;
        self.wechat = self.CheckWechat();
        self.project = project;
        self.url = url;
        self.uid = uid;
        //self.param = param;
        $.extend(true, self.param, param)

        if (button) {
            $(button).on('click', function() {
                if (self.wechat == 1) {
                    self.WechatGuide();
                } else {
                    self.WeiboShare($(this));
                }
            })
        }
        if (self.wechat == 1) {
            self.WechatInit();
        }
        self.Reflux();
    }

    ,CheckWechat : function()
    {
        var state = navigator.userAgent.toLowerCase().indexOf("micromessenger") > -1;
        if (state) {
            return 1;
        } else {
            return 2;
        }
    }

    ,WechatGuide : function()
    {
        var param = this.param;
        var tpl = '<div id="weixin_guide" style="position: fixed;background:url(../img/background.png) repeat;left:0;top:0;right:0;bottom:0;z-index:1000;"><img src="../img/weixin-guide.png" style="position: absolute;z-index:100;" width="295" height="273"/></div>';
        $('#weixin_guide').remove();
        $(tpl).attr('id', 'weixin_guide').appendTo($('body'));

        $('#weixin_guide img').css(weixinCfg.style || {})
        setTimeout(function() {
            $('#weixin_guide').click(function() {
                $(this).remove();
            })
        }, 300)
    }

    ,WechatInit: function()
    {
        var self = this;
        var url = this.url + 'init?callback=?';
        var param = this.param;
        var project = this.project;
        $.ajax({
            type:'post',
            dataType:'jsonp',
            jsonp: 'jsonp_cb',
            url: url,
            data: {
                'project' : project,
                'url' : location.href.split('#')[0]
            },
            success:function(result) {
                var data = result.data;
                wx.config({
                    //debug:true,
                    appId: data.appId,
                    timestamp: data.timestamp,
                    nonceStr: data.nonceStr,
                    signature: data.signature,
                    jsApiList: [
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage',
                        'onMenuShareQQ'
                    ]
                });

                wx.ready(function() {
                    wx.onMenuShareAppMessage({
                        title: param.title,
                        desc: param.desc,
                        link: param.url,
                        imgUrl: param.img,
                        success: function(res) {
                            param.sendMessageCallback('success', res)
                            self.ShareLog(1, 1);
                        },
                        cancel: function(res) {
                            param.sendMessageCallback('cancel', res)
                            self.ShareLog(1, 2);
                        },
                        fail: function(res) {
                            param.sendMessageCallback('fail', res)
                            self.ShareLog(1, 3);
                        }
                    });
                    wx.onMenuShareTimeline({
                        title: param.title,
                        link: param.url,
                        imgUrl: param.img,
                        success: function(res) {
                            param.timelineCallback('success', res)
                            self.ShareLog(2, 1);
                        },
                        cancel: function(res) {
                            param.timelineCallback('cancel', res)
                            self.ShareLog(2, 2);
                        },
                        fail: function(res) {
                            param.timelineCallback('fail', res)
                            self.ShareLog(2, 3);
                        }
                    });
                    wx.onMenuShareQQ({
                        title: param.title,
                        link: param.url,
                        imgUrl: param.img,
                        success: function(res) {
                            param.shareQQCallback('success', res)
                            self.ShareLog(3, 1);
                        },
                        cancel: function(res) {
                            param.shareQQCallback('cancel', res)
                            self.ShareLog(3, 2);
                        },
                        fail: function(res) {
                            param.shareQQCallback('fail', res)
                            self.ShareLog(3, 3);
                        }
                    });
                });
            },
            async:'true',
            timeout: 3000            
        });
    }

    ,Reflux: function() {
        var refer = document.referrer,
            url = encodeURIComponent(document.location.href),
            param = location.search.substr(1),
            ua = encodeURIComponent(navigator.userAgent),
            project = this.project,
            uid = this.uid,
            wechat = this.wechat;

        if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
            $.getJSON(this.url + 'reflux?callback=?' + '&project='+project+'&url=' + url + '&ua=' + ua + '&param=' + encodeURIComponent(param) + '&uid=' + uid + '&type=' + wechat);
        }
    }

    ,ShareLog: function(actType, actResult) {
        var project = this.project;
        var uid = this.uid;
        var wechat = this.wechat;
        var ua = encodeURIComponent(navigator.userAgent);
        var url = encodeURIComponent(document.location.href);
        $.getJSON(this.url + 'collect?callback=?' + '&project='+project+'&url=' + url + '&actType=' + actType + '&actResult=' + actResult + '&ua=' + ua + '&uid=' + uid + '&type=' + wechat);
    }
}