import Vue from 'vue'
import lib from 'lib'
import ext from 'extSdk'
import Clipboard from 'clipboard'
import _ from 'underscore'
import Swiper from 'swiper'

let from = lib.getParam("from")
let isFromApp = from=="wjy" ? true : false;
let uid = lib.getParam("uid")
let token = lib.getParam("token")


export default {
    methods: {
        copy(){ 
            let self = this
            this.clipboard.on('success', function () {
                location.href = self.downloadUrl;
            });
        },
        //意见反馈
        openFeedback(){
            ext.openFeedback();
        },
        getSupportShareType(){
            let self = this
            
            ext.run2('getSupportShareType', [function(list){ //返回[0,1,2,3,4]对应微信,朋友圈,微博,qq,qq空间
                if (!list || list.length == 0) {
                    list = [0,1,2,3,4]
                }

                if (_.indexOf(list,0) < 0) {
                    self.isSupportList[0] = false
                }
    
                if (_.indexOf(list,1) < 0) {
                    self.isSupportList[1] = false
                }

                if (_.indexOf(list,2) < 0) {
                    self.isSupportList[4] = false
                }

                if (_.indexOf(list,3) < 0) {
                    self.isSupportList[2] = false
                }
                
                if (_.indexOf(list,4) < 0) {
                    self.isSupportList[3] = false
                }

            }]);
        },
        //app分享
        shareMessage(type){
            ext.shareMessage(type, this.shareMsg.title, this.shareMsg.link, this.shareMsg.desc, this.shareMsg.imgUrl)
        },
        getUserInfo () {
            let self = this 
            let url = lib.apiUrl+"/share/getUserInfo.do";
            let param = {
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                uid : 1,
                targetUid : uid,
                token : "lucifer_test_token"
            }
    
            $.ajax({
                type: "get",
                url: url,
                data: param,
                dataType: "jsonp",
    
                success: function (ret) {
                    ret = lib.formatHttpProtocol(ret);

                    let data = ret.data
                 
                    self.userHeadInfo = {
                        head : data.head,
                        id : data.id,
                        nick :　data.nick
                    }

                    lib.setWxShare(self.getShareInfo(data.nick));
                }
            })
        },
        getInviteInfo () {
            let self = this 
            let url = `${lib.apiUrl}/share/inviteInfo.do`;
            let param = {
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                uid : uid,
                token : token
            }
    
            $.ajax({
                type: "get",
                url: url,
                data: param,
                dataType: "jsonp",
    
                success: function (ret) {
                    ret = lib.formatHttpProtocol(ret);

                    let data = ret.data

                    self.invitedUser = data.invitedUser
                    self.invitedCount = data.invitedCount
                    self.userHeadInfo = data.userHeadInfo

                    if(data.userHeadInfo) self.shareMsg = self.getShareInfo(data.userHeadInfo.nick)
                }
            })
        },
        //根据专题获取影片列表
        getArticleByFeature () {
            let self = this 
            let url = `${lib.apiUrl}/share/getArticleByFeature.do?featureId=38&count=10&timeline=0&channel=LuciferChannel&ver=1&os=1&uid=1&token=lucifer_test_token`;
    
            $.ajax({
                type: "get",
                url: url,
                dataType: "jsonp",
    
                success: function (ret) {
                    ret = lib.formatHttpProtocol(ret);

                    self.recommendFilms = ret.data.articles
                    
                    setTimeout(function(){
                        new Swiper('.swiper-video', {
                            slidesPerView: 'auto',
                            freeMode: true
                        })
                    },0);
                }
            })
        },
        getShareInfo : function(nick){
            var shareObj = {}
            shareObj.title = `${nick}邀你来看【微剧院】`
            shareObj.link = `https://${location.host}/?page=act-invite&uid=${uid}`
            shareObj.desc = `电影解说APP，五分钟呈现电影的精彩。`
            shareObj.imgUrl = `https://h5-glance.duowan.com/_src/img/icon-logo.png`

            return shareObj;
        }
    },
    mounted () {
        let self = this

        lib.setTitle(`专属邀请码`)

        self.getArticleByFeature();
        self.clipboard = new Clipboard(this.$refs.copy)

        if (lib.getParam("from") == 'wjy'){
            self.getSupportShareType()
            self.getInviteInfo()
        } else {
            self.getUserInfo()
        }
    },
    watch: {
        
    },
    data() {
        return {
            isApp : isFromApp,
            downloadUrl : lib.downloadUrl,
            isSupportList : [true,true,true,true,true], //是否支持微信,朋友圈,QQ,QQ空间,新浪微博
            shareMsg : {},
            invitedUser : [],
            invitedCount : 0 ,
            userHeadInfo : {},
            recommendFilms : [],
            clipboard : null
        };
    }
};