// 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 GameReport from "../../FrameWork/Report/ZyZyReport";
import User from "../../FrameWork/User/User";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import { DateUtils } from "../../FrameWork/Util/DateUtils";
import Common5 from "../../Platform/th/Common5";
import TaskManager from "../JuQingChat/TaskManager";
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 OnLineGameItem extends cc.Component {

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

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

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

    // LIFE-CYCLE CALLBACKS:
    @property(cc.Label)
    timeLab: cc.Label = null;
    @property(cc.Node)
    redPoint: cc.Node = null;

    // onLoad () {}
    config = null
    timeAdd = 0
    start() {
        let level = User.getAutoLevel()
        // if(level==0){
        //     PrefabManage.preloadPrefabByType(GameType.AutoLevelUpTips)
        // }

    }

    refreshView() {
        let descLab = this.config.descLab
        let str = descLab.split("/");
        this.nameLabel.string = str[1]
        this.descLabel.string = str[0]

        Common5.addUrlSprite_custom(this.config.icon, this.iconSpr)
        let timeLimit = this.config.needTime
        let isNeedAd = this.config.isNeedAd
        let configId = this.config.configId
        let onlineGameRewards = User.getOnlineGameReward()
        if (onlineGameRewards[configId] == 1) {
            this.node.getChildByName('灰色').active = false

            this.node.getChildByName('videoAdBtn').active = false
            this.node.getChildByName('videoBtn').active = false
            this.node.getChildByName('已领取').active = true
            this.redPoint.active = false
        } else {
            if (UserManager.onlineTime >= timeLimit) {
                this.node.getChildByName('灰色').active = false
                this.redPoint.active = true
                if (isNeedAd) {
                    this.node.getChildByName('videoAdBtn').active = true
                } else {
                    this.node.getChildByName('videoBtn').active = true
                }
            } else {

            }
        }
        this.timeLab.node.active = false
        this.timeAdd = timeLimit - UserManager.onlineTime
        if (this.timeAdd > 0) {
            this.schedule(() => {
                if (this.timeAdd > 0) {
                    this.timeAdd--
                    let time = DateUtils.formatTime3_custom(this.timeAdd)
                    this.timeLab.string = time

                    if (!this.timeLab.node.active) {
                        this.timeLab.node.active = true
                    }
                }
            }, 1)
        } else {
            this.timeLab.node.active = false
        }
    }


    setDateView(config) {
        this.config = config
        console.log(this.config.configId, UserManager.onlineTime, 'UserManager.onlineTime++==')
        this.refreshView()


    }

    lingquClickEvent() {
        //Common5.playEffect("sound/按键点击") 
        this.getReward()
    }

    lingquAdClickEvent() {
        //Common5.playEffect("sound/按键点击") 


        let tab = {
            onClose: (finish) => {
                if (finish) {
                    let descLab = this.config.descLab
                    let str = descLab.split("/");
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-在线奖励` + str[1])
                    // Common5.ReportDY("inLevel", "在线奖励-AD-" + str[1]);
                    this.getReward()

                }
                else {

                    Common5.showTips_custom("广告未观看完");
                }

            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)
    }


    getReward() {
        // fix me
        if (this.config.configId == 0) {
            //加钱
            let earnMoney = LevelUpManager.levelUpSpned()
            if (earnMoney * 3 >= 100000) {
                earnMoney = earnMoney * 3
            } else {
                earnMoney = 100000
            }
            UserManager.addMoney(earnMoney)
            let str = Common5.getNumberChangeHanzi(earnMoney, '1', 1)
            PrefabManage.showTextTips('领取在线奖励' + str)
        } else if (this.config.configId == 1) {
            //自动点击*1
            let time = User.getAutoClickTime()
            User.setAutoClickTime(5 * 60 + time)
            User.addAutoLevel()
            EventMgr.emitEvent_custom(ryw_Event.RefreshLevelLab)

        } else if (this.config.configId == 2) {
            //多倍收益*1

            //点击收益
            let num = LevelUpManager.getShouyi()
            User.setClickIncomeNum(num)
            EventMgr.emitEvent_custom(ryw_Event.levelUpChange)

        } else if (this.config.configId == 3) {
            //加等级
            // let mylevel = User.getMyLevel()
            // User.setMyLevel(10+mylevel)
            LevelUpManager.peopleLevelUp(10)
            PrefabManage.showTextTips('等级增加10级')
        } else if (this.config.configId == 4) {
            //加等级
            LevelUpManager.peopleLevelUp(15)
            PrefabManage.showTextTips('等级增加15级')
        }

        this.node.getChildByName('videoAdBtn').active = false
        this.node.getChildByName('videoBtn').active = false
        this.node.getChildByName('已领取').active = true
        this.redPoint.active = false
        //在线奖励状态change
        let onlineGameRewards = User.getOnlineGameReward()
        onlineGameRewards[this.config.configId] = 1

        User.setOnlineGameReward(onlineGameRewards)
    }


    // update (dt) {}
}