// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import BagManager from "../Manager/BagManager";
import LevelUpManager from "../Manager/LevelUpManager";
import UserManager from "../Manager/UserManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ChengJiuItemNode extends cc.Component {

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(cc.Label)
    title_str: cc.Label = null;

    @property(cc.Label)
    miaoshu_str: cc.Label = null;

    @property(cc.Label)
    num_str: cc.Label = null;

    @property(cc.Node)
    btn_go: cc.Node = null;
    @property(cc.Node)
    btn_lingqu: cc.Node = null;
    @property(cc.Node)
    btn_weiwancheng: cc.Node = null;
    @property(cc.Node)
    yiwancheng: cc.Node = null;

    config: any;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {

    }

    init(data) {
        this.config = data
        // Common5.addUrlSprite_custom(this.config.icon, this.icon)
        Common5.getSpriteFrameFromBundle("ChengJiu", this.config.icon, this.icon)
        this.title_str.string = data.descLab
        this.miaoshu_str.string = data.miaoshu
        this.refreshState()
    }

    refreshState() {
        this.btn_go.active = false
        this.btn_lingqu.active = false
        this.btn_weiwancheng.active = false
        this.yiwancheng.active = false
        let ChengJiuReward = User.getChengJiuReward()
        let configId = this.config.configId
        this.num_str.node.active = false
        if (this.config.configId == 0) {
            this.checkIsShowJiangLiBtn()
        } else if (this.config.configId >= 1 && this.config.configId <= 3) {
            let timeLimit = this.config.needTime
            let isNeedAd = this.config.isNeedAd
            if (ChengJiuReward[configId] == 1) {
                this.yiwancheng.active = true
            } else {
                console.log(UserManager.onlineTime)
                if (UserManager.onlineTime >= timeLimit) {
                    this.btn_lingqu.active = true
                } else {
                    this.btn_weiwancheng.active = true
                }
            }
        } else if (this.config.configId >= 4 && this.config.configId <= 5) {
            let num = User.getDengLuDayNum()
            let needDayNum = this.config.needDayNum
            if (ChengJiuReward[configId] == 1) {
                this.yiwancheng.active = true
            } else {
                if (num >= needDayNum) {
                    this.btn_lingqu.active = true
                } else {
                    this.btn_weiwancheng.active = true
                }
            }
        } else if (this.config.configId >= 6 && this.config.configId <= 7) {
            let adnum = User.getShowAdNum()
            let needADNum = this.config.needADNum
            this.num_str.node.active = true
            this.num_str.string = `${adnum >= this.config.needADNum ? this.config.needADNum : adnum}/${this.config.needADNum}`
            if (ChengJiuReward[configId] == 1) {
                this.yiwancheng.active = true
                this.num_str.node.active = false
            } else {
                if (adnum >= needADNum) {
                    this.btn_lingqu.active = true
                } else {
                    this.btn_weiwancheng.active = true
                }
            }
        } else if (this.config.configId == 8) {
            let num = User.getDengLuDayNum()
            let needDayNum = this.config.needDayNum
            if (ChengJiuReward[configId] == 1) {
                this.yiwancheng.active = true
            } else {
                if (num >= needDayNum) {
                    this.btn_lingqu.active = true
                } else {
                    this.btn_weiwancheng.active = true
                }
            }
        }
    }

    checkIsShowJiangLiBtn() {
        //是否已经领取过奖励

        let isGetAward = User.getFinishCeBianLanAward()

        console.log(isGetAward, 'isGetAward')
        console.log(Common5.isCeBianLanEnter, 'isCeBianLanEnter')
        if (Common5.isCeBianLanEnter && !isGetAward) {
            this.btn_go.active = false
            this.btn_lingqu.active = true
        } else {

            if (isGetAward) {
                this.yiwancheng.active = true
            } else {
                this.btn_go.active = true
                this.btn_lingqu.active = false
            }
        }
    }

    onTouchGo() {
        if (this.config.configId == 0) {
            //Common5.playEffect("sound/按键点击")
            PrefabManage.loadPrefabByType(GameType.CeBianLan)
        } else if (this.config.configId >= 1 && this.config.configId <= 4) {

        } else if (this.config.configId >= 5 && this.config.configId <= 6) {

        } else if (this.config.configId >= 7 && this.config.configId <= 8) {

        }
    }

    onTouchLingQu() {
        if (this.config.configId == 0) {
            // UserManager.addMoney(10000)
            // let str = Common5.getNumberChangeHanzi(10000, '1', 1)
            // PrefabManage.showTextTips('成功获得' + str + '金币!')
            //自动点击*1
            let time = User.getAutoClickTime()
            User.setAutoClickTime(5 * 60 + time)
            User.addAutoLevel()
            EventMgr.emitEvent_custom(ryw_Event.RefreshLevelLab)
        } else if (this.config.configId == 1) {
            //点击收益
            cc.sys.localStorage.setItem("成就首次点击收益", '1')
            // let num = LevelUpManager.getShouyi()
            // User.setClickIncomeNum(num)
            EventMgr.emitEvent_custom(ryw_Event.closeChengJiu)
        } else if (this.config.configId == 2) {
            //自动点击*1
            cc.sys.localStorage.setItem("成就首次自动点击", '1')
            // let time = User.getAutoClickTime()
            // User.setAutoClickTime(5 * 60 + time)
            // User.addAutoLevel()
            EventMgr.emitEvent_custom(ryw_Event.closeChengJiu)
        } else if (this.config.configId == 3) {
            // User.setZhuanPanFree(true)
            // PrefabManage.loadPrefabByType(GameType.MultipleIncome)
            //加等级
            // let mylevel = User.getMyLevel()
            // User.setMyLevel(10+mylevel)

            // LevelUpManager.peopleLevelUp(10)
            // PrefabManage.showTextTips('等级增加10级')

            // 无广卷*1
            BagManager.addBagList({ goodId: 3700, goodNum: 1 })
        } else if (this.config.configId == 4) {
            //自动点击*1
            let time = User.getAutoClickTime()
            User.setAutoClickTime(5 * 60 + time)
            User.addAutoLevel()
            EventMgr.emitEvent_custom(ryw_Event.RefreshLevelLab)
        } else if (this.config.configId == 5) {
            BagManager.addBagList({ goodId: 3700, goodNum: 1 })
        } else if (this.config.configId == 6) {
            // 无广卷*1
            BagManager.addBagList({ goodId: 3700, goodNum: 1 })
        } else if (this.config.configId == 7) {
            // 无广卷*1
            BagManager.addBagList({ goodId: 3700, goodNum: 2 })
        } else if (this.config.configId == 8) {
            // 无广卷*1
            BagManager.addBagList({ goodId: 3700, goodNum: 2 })
            User.setDengLuDayNum(0)
        }

        this.yiwancheng.active = true
        this.btn_lingqu.active = false
        //奖励状态change
        let onlineGameRewards = User.getChengJiuReward()
        onlineGameRewards[this.config.configId] = 1

        User.setChengJiuReward(onlineGameRewards)
    }

    // update (dt) {}
}