import Vue from 'vue';
import $ from 'jquery';
import lib from 'lib';

import wx from 'weixin-js-sdk';

import PhotoSwipe from 'photoswipe';
import PhotoSwipeUI_Default  from '../../../depend/photoswipe/photoswipe-ui-default';
import PhotoSwipe_Css from '../../../depend/photoswipe/photoswipe.css';
import PhotoSwipe_Skin from '../../../depend/photoswipe/default-skin/default-skin.css';


var PhotoSwipe_Tpl = '<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">\
    <div class="pswp__bg"></div>\
    <div class="pswp__scroll-wrap">\
        <div class="pswp__container">\
            <div class="pswp__item"></div>\
            <div class="pswp__item"></div>\
            <div class="pswp__item"></div>\
        </div>\
        <div class="pswp__ui pswp__ui--hidden">\
            <div class="pswp__top-bar">\
                <div class="pswp__counter"></div>\
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>\
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>\
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>\
                <div class="pswp__preloader">\
                    <div class="pswp__preloader__icn">\
                      <div class="pswp__preloader__cut">\
                        <div class="pswp__preloader__donut"></div>\
                      </div>\
                    </div>\
                </div>\
            </div>\
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"></button>\
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"></button>\
            <div class="pswp__caption">\
                <div class="pswp__caption__center"></div>\
            </div>\
        </div>\
    </div>\
</div>';

let pathname = location.pathname;
let domain = pathname.indexOf('fhtest')>-1 ? 'http://test.api.fhvideo.oxzj.net' : 'http://api-fhvideo.duowan.com';

export default {
    data() {
        return {
            postId: lib.getLocalData('postId'),
            user: {},
            type: 1,
            createTime: "",
            extend: {},
            comments : [],
            shareObj: {},
            picList : {},
            // url : this.initDownloadUrl()
            url : "http://a.app.qq.com/o/simple.jsp?pkgname=com.ouj.fhvideo"
        }
    },
    mounted() {
        this.getMomentDetail();
        this.getComments();
    },
    beforeDestroy() {
    },
    methods: {
        /**获取视频数据*/
        getMomentDetail() {
            if (this.postId == null) {
                return;
            }

            let self = this;
            let url = `${domain}/other/getMomentDetail.do?postId=${self.postId}`;
            
            $.ajax({
                type: "get",
                url: url,
                dataType: "jsonp",
                success: function (ret) {
                    if (ret.code == 0) {
                        let _data = ret.data.data;

                        self.user = _data.user;
                        self.type = _data.type;
                        self.extend = _data.extend;
                        self.createTime = self.handlerTime(_data.createTime);

                        self.shareObj = {
                            title: _data.user.nick+"的动态",
                            desc: (_data.type==1 ? _data.extend.text : _data.extend.title),
                            img: _data.user.head
                        }

                        if (_data.type==1) {
                            self.picList = _data.extend.pics;
                            self.initPhotoSwipe();
                        }
                        self.wxHandler();
                    }
                }
            });
        },
        getComments() {
            if (this.postId == null) {
                return;
            }
            
            let self = this;
            let url = `${domain}/other/getComments.do.do?postId=${self.postId}&count=10`;
            
            $.ajax({
                type: "get",
                url: url,
                dataType: "jsonp",
                success: function (ret) {
                    if (ret.code == 0) {
                        if (!ret.data.comments) {
                            return;
                        }

                        let comments = ret.data.comments;
                        let len = comments.length;

                        for (var i=0; i<len; i++) {
                            comments[i]["createTime"] = self.handlerTime(comments[i]["createTime"]);
                        }

                        self.comments = comments;
                    }
                }
            });
        },
        handlerTime(createTime){
            var current =  (new Date()).getTime();
            var restTime = (current - createTime)/1000;


            var day = Math.floor(restTime/(60*60*24));
            var hour = Math.floor(restTime%(60*60*24)/(60*60));
            var min = Math.floor(restTime%(60*60*24)%(60*60)/(60));
            var second = Math.floor(restTime%(60*60*24)%(60*60)%60);

            if (day>1) {
                return day+"天前";
            }
            if (hour>1) {
                return hour+"小时前";
            }
            if (min>1) {
                return min+"分钟前";
            } else {
                return second+"秒前";
            }
        },
        initDownloadUrl(){
            // var u = navigator.userAgent;

            // if (u.match(/micromessenger/ig)) {
            //     return 'http://a.app.qq.com/o/simple.jsp?pkgname=com.duowan.lolbox'
            // } else {
            //     var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            //     var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

            //     if (isAndroid) {
            //         return 'http://box.dwstatic.com/upgrade/download.php';
            //     } else if(isiOS) {
            //         return 'https://itunes.apple.com/cn/app/duo-wan-fan-he-lol-he-zi/id698076980?mt=8';
            //      }
            // } 
            var u = navigator.userAgent;
            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

            if (isAndroid) {
                return 'http://www.hiyd.com/static/apkDownload?appid=12';
            } else {
                return 'https://itunes.apple.com/us/app/%E9%A5%AD%E7%9B%92%E8%A7%86%E9%A2%91/id1237446875?l=zh&ls=1&mt=8';
            }
        },
        initPhotoSwipe(){
            $("head").append("<style>" + PhotoSwipe_Css + "</style>");
            $("head").append("<style>" + PhotoSwipe_Skin + "</style>");
            $("body").append(PhotoSwipe_Tpl);
        },
        previewImage(curPic){

            var index;
            var items = [];
            var len = this.picList.length;

            for(var i=0; i<len; i++){
                if(curPic == this.picList[i]) {
                    index = i;
                }
                items.push({
                    src: this.picList[i],
                    w: 1242,
                    h: 1080
                })
            }

            var options = {
                index: index
            };
            
            var gallery = new PhotoSwipe($('.pswp').get(0), PhotoSwipeUI_Default, items, options);
            gallery.init();
        },
        wxHandler: function () {
            let self = this;
            if (!navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger") {
                return;
            }
            var url = 'http://m.hiyd.com/weixin/getJsSign',
                data = {
                    url: location.href,
                    appid: 7
                };

            $.ajax({
                type: "get",
                url: url,
                data: data,
                dataType: "jsonp",

                success: function (ret) {
                    ret.data.debug = false;
                    ret.data.jsApiList = ["onMenuShareTimeline", "onMenuShareAppMessage"];
                    wx.config(ret.data);
                    wx.ready(function () {
                        //分享到朋友圈
                        wx.onMenuShareTimeline({
                            title: self.shareObj.desc,		//分享标题
                            imgUrl: self.shareObj.img,		//分享图标
                            success: function () {

                            },
                            cancel: function () {
                                // 用户取消分享后执行的回调函数
                            }
                        });
                        //分享给朋友
                        wx.onMenuShareAppMessage({
                            title: self.shareObj.title, 		//分享标题
                            desc: self.shareObj.desc,
                            imgUrl: self.shareObj.img, 	//分享图标
                            success: function () {
                            },
                            cancel: function () {
                                // 用户取消分享后执行的回调函数
                            }
                        });
                    });
                }
            });
        }
    }
}