// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        scoreLabel:cc.Label,
        content:cc.Node,
        products:null,
        selectedPosition:0,
    },

    // LIFE-CYCLE CALLBACKS:

    onEnable () {
        this.getArrows(Global.user);
        this.scoreLabel.string = Global.user.score;
    },

    getArrows(userInfo){
        wx.request({
            url:Global.storeUrl,
            data:{
                token:userInfo.token,
                uid:userInfo.uid,
                channel:Global.channel,
                os:Global.os, 
                ver:Global.ver,
                type:2
            },
            success:(response)=>{
                if(response.data.code==-5){
                    wx.showToast({
                        title:'登录过期'
                    })
                    Global.showLoginPage = true;
                    cc.director.loadScene('MainMenu');
                }
                this.products = response.data.data.products;
                let user = response.data.data.user;
                Global.user.score = user.score;
                this.scoreLabel.string = user.score;
                if(this.products!=undefined){
                    for(let i =0;i<this.products.length;i++){
                        if(i<this.content.childrenCount){
                            let skinItem = this.content.children[i].getComponent('ArrowSkinItem');
                            let selected = false;
                            let product = this.products[i];
                            if(Global.arrowSkinId == -1&&i==0){
                                selected = true;
                            }else if(Global.arrowSkinId == product.id){
                                selected = true;
                            }
                            skinItem.init(product,selected,this,i);
                        }
                    }
                }else{
                    wx.showToast({
                        title:'数据出错，请重试！',
                        icon:none,
                    })
                }
            },
        });
    },

    onItemClick(position){
        let product = this.products[position];
        let skinItem = this.content.children[position].getComponent('ArrowSkinItem');
        if(!skinItem.lockedSprite.active){
            this.selectItem(position);
        }else{
            if(product.price <= Global.user.score){
                wx.showModal({
                    title:'',
                    content:'是否解锁这个皮肤？',
                    cancelButton:true,
                    cancelText:'取消',
                    confirmText:'确定',
                    success:(res)=>{
                        if(res.confirm){
                            wx.showLoading();
                            this.buySkin(product,position);
                        }
                    }
                })
            }else{
                wx.showToast({
                    title:'分数不够，继续努力吧！',
                    icon:null
                })
            }
        }
    },

    selectItem(position){
        let product = this.products[position];
        Global.arrowSkinId = product.id;
        Global.arrowSkinIndex = position;
        wx.setStorage({
            key:"skinId",
            data:{
                bottleSkinId:Global.bottleSkinId,
                arrowSkinId:Global.arrowSkinId,
                bottleSkinIndex:Global.bottleSkinIndex,
                arrowSkinIndex:Global.arrowSkinIndex
            }
        })
        for(let i =0;i<this.products.length;i++){
            if(i<this.content.childrenCount){
                let skinItem = this.content.children[i].getComponent('ArrowSkinItem');
                skinItem.setSelected(i==position);
            }
        }
    },

    buySkin(product,position){

        let skinItem = this.content.children[position].getComponent('ArrowSkinItem');

        let productJson =JSON.stringify({
            id:product.id,
            price:product.price,
            type:2
        });
        console.log(productJson);
        wx.request({
            url:Global.skinBuyUrl,
            data:{
                token:Global.user.token,
                uid:Global.user.uid,
                channel:Global.channel,
                os:Global.os, 
                ver:100,
                productJson:productJson
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:"POST",
            success:(response)=>{
                if(response.data.code==-5){
                    wx.showToast({
                        title:'登录过期'
                    })
                    Global.showLoginPage = true;
                    cc.director.loadScene('MainMenu');
                }
                if(response.data.code==0||response.data.code==-5101){
                    let data = response.data.data;
                    skinItem.setUnLock();
                    this.selectItem(position);

                    wx.showToast({
                        title:'解锁成功',
                        icon:null
                    })
                }else{
                    console.log(response.data.msg); 
                }
            },
            complete:()=>{
                wx.hideLoading();
            }
        })
    }


    // update (dt) {},
});
