import Vue from 'vue'
import lib from 'lib'

var host = location.host;
var domain;
var from_uid = lib.getParam("from_uid"),openid = lib.getParam("openid")

if (host.indexOf("127.0.0.1") > -1 || host.indexOf("baichun.duowan.com") > -1) {
    // domain = " //yankam.webdev2.duowan.com/";
    domain = "//new.mka.duowan.com";
} else if(host.indexOf("test.h5-glance.duowan.com") > -1){
    // domain = "//test.mka.duowan.com";
    domain = "//new.mka.duowan.com";
} else {
    domain = "//mka.duowan.com";
}

class Lottery{
    constructor(callback){
        this.$lotterys = $('.lottery-item')
        this.lottering = false
        this.callback = callback
        this.scoreRet = null
    }
    init(){
        this.$lotterys.removeClass('active')
        this.stop()
    }

    start(){
        let self = this
        let len = self.$lotterys.length

        self.lottering = true
        self.scoreRet = null
        self.lotteryIndex = 0
        self.lotteryTimer = setInterval(function () {
            self.$lotterys.removeClass('active')

            let $current = self.$lotterys.filter('[data-order=' + self.lotteryIndex + ']').addClass('active')
            let score = $current.attr('data-score')*100

            if (self.scoreRet && self.scoreRet.amount == score) {
                self.stop()
            }

            self.lotteryIndex = (self.lotteryIndex + 1) % len
        }, 200)
    }

    stop(){
        clearInterval(this.lotteryTimer)
        this.lottering = false
        this.callback(this.scoreRet)
    }
}

export default {
    methods: {
        startLottery(){
            let self = this

            if(self.times==0) {
                return;
            }

            if(self.lottery.lottering) {
                return;
            }

            self.lottery.start()
            self.gorgeGacha()
        },
        toggleModGuide(){
            this.isShowGuide = this.isShowGuide ? false : true
        },
        toggleModAward(){
            this.isShowAward = this.isShowAward ? false : true
        },
        toggleModResult(){
            this.isShowResult = this.isShowResult ? false : true
        },
        //中奖滚动
        scrollMenu(){
            let self = this
            let $list = $(".list-menu"),$item = $(".list-menu li"),$scroll=$(".menu-scroll")

            let listHeight = $list.height(),listLen = self.bingoList.length
            let itemHeight = 56,viewHeight = 336

            if(listLen <= 5) {
                clearInterval(self.scrollTimer)
                return
            }

            if(viewHeight-self.scrollHeight+10 > listHeight) {
                self.scrollHeight = 0;
            } else {
                self.scrollHeight = self.scrollHeight-itemHeight;
            }
        },
        gorgeLottery(){ 
            let self = this 
            let url = domain+"/activity/gorgeLottery";
            let param = {
                from_uid : from_uid,
                openid : openid
            }

            lib.showLoading()
            $.ajax({
                type: "get",
                url: url,
                data: param,
                dataType: "jsonp",
                success: function(ret){
                    lib.hideLoading()
                    ret = lib.formatHttpProtocol(ret);

                    if (ret.code == -5) {
                        self.wxLogin()
                    }else if (ret.code == -7) {
                        location.href = ret.url
                    } else if (ret.code ==0) {
                        let data = ret.data

                        self.bingoList = data.bingoList
                        self.friendTotal = data.friendTotal
                        self.friends = data.friends
                        self.times = data.times
                        self.userBingo = data.userBingo
                        self.getLotteryCount = data.getLotteryCount

                        lib.setWxShare({
                            title: `邀您参与王者荣耀送英雄活动`,
                            desc: `帮我增加一次抽奖机会，提高中奖概率`, 
                            link: `${self.shareLink}&from_uid=${data.userId}`, 
                            imgUrl: `//h5-glance.duowan.com/build/gorge.jpg`
                        });

                        self.scrollTimer = window.setInterval(()=>{
                            self.scrollMenu()
                        },2000)
                    } else {
                        lib.showDialog(ret.msg)
                    }
                }
            });
        },
        gorgeGacha(){
            let self = this
            let url = domain+"/activity/gorgeGacha"
            let param = {
                
            }

            $.ajax({
                type: "get",
                url: url,
                data: param,
                dataType: "jsonp",
                success: function(ret){
                    ret = lib.formatHttpProtocol(ret);

                    if (ret.code ==0) {
                        let data = ret.data
                        --self.times

                        if(data.amount>0) {
                            self.gorgeLottery()
                        }

                        setTimeout(function () {
                            self.lottery.scoreRet = data
                        }, 4000);
                    } else {
                        setTimeout(function () {
                            self.lottery.init();
                            lib.showDialog(ret.msg)
                        }, 4000);
                    }
                }
            });
        },
        wxLogin(){
            var url = "//mka.duowan.com/login/wxAjax?url="+encodeURIComponent(location.href);

            $.ajax({
                type : "get",
                url : url,
                dataType : "jsonp",
                success : function(ret){
                    if (ret.code == 0) {
                        location.reload();
                    } else if(ret.code == -7){
                        location.href = ret.data;
                    } else {
                        lib.showTip(ret.msg);
                    }
                }
            });
        }
    },
    activated (){
        let url = `http://h5game.nbwx.com/act/?page=act-gorgelottery`

        if(from_uid) url += `&from_uid=${from_uid}`
        if(openid) url += `&openid=${openid}`
        
        location.replace(url)
    },
    mounted () {
        let self = this;
        let user_id = lib.getCookie("user_id")
        
        lib.setTitle("王者荣耀英雄抽奖活动")

        if(user_id==null) user_id=""
        if (lib.checkWXAgent() || 1) {
            self.isWx = true
            self.gorgeLottery()
    
            self.lottery = new Lottery(function(data){
                self.scoreRet = data
                self.toggleModResult()
            })
        } else {
            lib.showTip("请在微信端打开活动页面")
        }
    },
    watch: {
        
    },
    data() {
        return {
            downloadUrl : lib.downloadUrl,
            bingoList : [],
            friendTotal : 0,
            friends : [],
            times : 0,     //抽奖次数
            getLotteryCount : 0, //历史邀请抽奖次数
            userBingo : [],
            isWx: false,
            lottery : {},  //抽奖对象
            isShowGuide : false,
            isShowResult : false,
            isShowAward : false,
            scoreRet : {}, //抽奖结果
            scrollHeight : 0, //中奖滚动
            shareLink : `https://${location.host}/?page=act-gorgelottery`
        };
    }
};

window.Lottery = Lottery