// 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;
        //     }
        // },
        goldLabel:cc.Label,
        content:cc.Node,
        products:null,
        userInfo:null,
    },

    // LIFE-CYCLE CALLBACKS:

    onEnable () {
        this.getBottles(Global.user);
        this.goldLabel.string = Global.user.gold;
        // if(window.wx!=undefined){
        //     wx.getStorage({
        //         key:"userInfo",
        //         success:(response)=>{
        //             this.userInfo = response.data;
        //             this.goldLabel.string = this.userInfo.gold;
        //             this.getBottles(this.userInfo);
        //         },
        //         fail:()=>{
        //             console.log('获取用户数据失败');
        //         }
        //     })
        // }
    },

    getBottles(userInfo){
        wx.request({
            url:Global.storeUrl,
            data:{
                token:userInfo.token,
                uid:userInfo.uid,
                channel:Global.channel,
                os:Global.os,
                ver:Global.ver,
                type:1
            },
            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.gold = user.coin;
                this.goldLabel.string = Global.user.gold;
                if(this.products!=undefined){
                    for(let i =0;i<this.products.length;i++){
                        if(i<this.content.childrenCount){
                            let selected = false;
                            if(Global.bottleSkinId == -1 && i == 0){
                                selected = true;
                            }else if(Global.bottleSkinId == this.products[i].id){
                                selected = true;
                            }
                            let skinItem = this.content.children[i].getComponent('BottleSkinItem');
                            skinItem.init(this.products[i],selected,this,i);
                        }
                    }
                }else{
                    wx.showToast({
                        title:'数据出错，请重试！'
                    })
                }
            },
        });
    },

    onItemClick(position){
        let product = this.products[position];
        let skinItem = this.content.children[position].getComponent('BottleSkinItem');
        if(!skinItem.lockedSprite.active){
            this.selectItem(position);
        }else{
            if(product.price <= Global.user.gold){
                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.bottleSkinId = product.id;
        switch (position){
            case 0:
                Global.bottleSkinIndex = 0;
                break;
            case 1:
                Global.bottleSkinIndex = 4;
                break;
            case 2:
                Global.bottleSkinIndex = 5;
                break;
            case 3:
                Global.bottleSkinIndex = 7;
                break;
            case 4:
                Global.bottleSkinIndex = 3;
                break;
            case 5:
                Global.bottleSkinIndex = 2;
                break;
            case 6:
                Global.bottleSkinIndex = 1;
                break;
            case 7:
                Global.bottleSkinIndex = 8;
                break;
            case 8:
                Global.bottleSkinIndex = 6;
                break;
            case 9:
                Global.bottleSkinIndex = 10;
                break;
        }

        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('BottleSkinItem');
                skinItem.setSelected(i==position);
            }
        }
    },

    buySkin(product,position){

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

        let productJson =JSON.stringify({
            id:product.id,
            price:product.price,
            type:1
        });
        wx.request({
            url:Global.skinBuyUrl,
            data:{
                token:Global.user.token,
                uid:Global.user.uid,
                channel:Global.channel,
                os:Global.os, 
                ver:Global.ver,
                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);
                    Global.user.gold = Global.user.gold - product.price;
                    this.goldLabel.string = Global.user.gold;
                    wx.hideLoading();
                    wx.showToast({
                        title:'解锁成功',
                        icon:null,
                    })
                }else{
                    console.log(response.data); 
                }
            },
            fail(){
                wx.hideLoading();
            },
        })
    },

    saveUserInfo(){

    }

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