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

var host = location.host;
var domain;

if (host.indexOf("127.0.0.1") > -1) {
    domain = " //yankam.webdev2.duowan.com/";
} else if(host.indexOf("test.h5-glance.duowan.com") > -1){
    domain = "//test.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
    }

    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
        }, 500)
    }

    stop(){
        let self = this

        self.lottering = false
        clearInterval(self.lotteryTimer)

        if(self.scoreRet.amount==0) {
            let msg = `<section class="dialog-wzry-lotmsg">
                            <p>邀请1个好友安装微剧院，可再抽奖一次，邀请越多，中奖机率越高。</p>
                        </section>`
            lib.showDialog(msg,self.callback,"抱歉，你没有中奖","马上邀请好友")
        } else {
            let msg = `<section class="dialog-wzry-lotmsg">
                    <ul class="list-process">
                        <li>
                            <label>奖品：</label>
                            <p>获得1Q币</p>
                        </li>
                        <li>
                            <label>领取方式：</label>
                            <p>关注微信“duowanmall”，回复“领奖”领取</p>
                        </li>
                    </ul>
                    <p>再邀请1个好友安装微剧院，抽588点券！</p>
                </section>`

            lib.showDialog(msg,self.callback,"恭喜，中奖了","马上邀请好友")
        }
    }
}

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

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

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

            self.lottery.start()
            self.glanceGacha()
        },
        showModGuide(){
            this.isShowGuide = true
        },
        closeModGuide(){
            this.isShowGuide = false
        },
        showModAward(){
            this.isShowAward = true
        },
        closeModAward(){
            this.isShowAward = false
        },
        glanceLottery(){ 
            let self = this 
            let url = domain+"/activity/glanceLottery";
            let param = {
                _from : location.href
            }

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

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

                        self.bingoList = data.bingoList
                        self.gachaCount = data.gachaCount
                        self.glanceId = data.glanceId
                        self.glanceNick = data.glanceNick
                        self.isLoginedWjy = data.glanceId ? true : false
                        self.invitedList = data.invitedList
                        self.userBingo = data.userBingo

                        if(self.isLoginedWjy) {
                            self.shareTitle = `${self.glanceNick}邀你来看【微剧院】`
                            self.shareLink = `//${location.host}/?page=act-invite&uid=${self.glanceId}`
                        }
                    } else {
                        lib.showDialog(ret.msg)
                    }

                    lib.setWxShare({
                        title: self.shareTitle,
                        desc: `电影解说APP，五分钟呈现电影的精彩。`, 
                        link: self.shareLink, 
                        imgUrl: `//h5-glance.duowan.com/_src/img/icon-logo.png`
                    });
                }
            });
        },
        glanceGacha(){
            let self = this
            let url = domain+"/activity/glanceGacha"
            let param = {
                glanceId : self.glanceId
            }

            $.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.gachaCount

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

                        setTimeout(function () {
                            self.lottery.scoreRet = data
                        }, 4000);
                    } else {
                        lib.showDialog(ret.msg)
                    }
                }
            });
        },
        setWxShare(){
            let self = this
            lib.setWxShare({
                title: self.shareTitle,
                desc: `电影解说APP，五分钟呈现电影的精彩。`, 
                link: self.shareLink, 
                imgUrl: `//h5-glance.duowan.com/_src/img/icon-logo.png`
            });
        }
    },
    mounted () {
        let self = this;
        lib.setTitle("微剧院X王者荣耀活动")

        if (lib.checkWXAgent()) {
            self.isWx = true
            self.glanceLottery()

            self.lottery = new Lottery(function(){
                self.showModGuide()
            })
        } else {
            lib.showDialog("请在微信端打开活动页面")
        }
    },
    watch: {
        
    },
    data() {
        return {
            downloadUrl : lib.downloadUrl,
            bingoList: [],
            gachaCount: 0,
            glanceId: 0,
            invitedList: [],
            userBingo: {},
            isLoginedWjy: false,
            isWx: false,
            lottery : {}, //抽奖对象
            isShowGuide : false,
            isShowGuideAct : false,
            isShowAward : false,
            shareTitle : "微剧院X王者荣耀活动",
            shareLink : `//${location.host}/?page=act-wzry`
        };
    }
};
