// 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 DH_8 from "../DH/DH_8";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import ChallengeManager from "../Manager/ChallengeManager";
import LevelUpManager from "../Manager/LevelUpManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ChallengeChooseItem extends cc.Component {

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

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

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


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


    @property(cc.Node)
    redPoint: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    config = null

    start() {
        EventMgr.onEvent_custom(ryw_Event.challengeQuit, () => {
            this.refreshView()
        }, this)
    }

    refreshView() {

        let curChallengeId = ChallengeManager.getCurChallengeId() //当前挑战的id  

        let huiseBtn = this.node.getChildByName('huiseBtn')
        let againBtn = this.node.getChildByName('againBtn')
        let curBtn = this.node.getChildByName('curBtn')
        let jiangIcon = this.node.getChildByName('奖励')
        let zhanIcon = this.node.getChildByName('战力')

        let qianzhiDesc = this.node.getChildByName('qianzhiDesc')


        this.nameLab.node.active = true
        this.zhanliDesc.node.active = true
        this.jiangDesc.node.active = true

        jiangIcon.active = true
        zhanIcon.active = true
        qianzhiDesc.active = false
        Common5.addUrlSprite_custom(this.config.iconUrl, this.touxiangSpr)
        this.nameLab.string = this.config.name
        if (this.config.name == '龙哥' && TaskManager.getCurUnLockMainTaskId() == MainTaskIdEnum.MainTask_506) {
            this.nameLab.node.color = cc.color(33, 152, 0)
            this.nameLab.string = this.config.name + '(任务目标)'
        } else if (this.config.name == '柳家豪' && TaskManager.getCurUnLockMainTaskId() == MainTaskIdEnum.MainTask_521) {
            this.nameLab.node.color = cc.color(33, 152, 0)
            this.nameLab.string = this.config.name + '(任务目标)'
        } else if (this.config.name == '巨无霸' && TaskManager.getCurUnLockMainTaskId() == MainTaskIdEnum.MainTask_534_2) {
            this.nameLab.node.color = cc.color(33, 152, 0)
            this.nameLab.string = this.config.name + '(任务目标)'
        } else if (this.config.name == '马大师' && TaskManager.getCurUnLockMainTaskId() == MainTaskIdEnum.MainTask_551_2) {
            this.nameLab.node.color = cc.color(33, 152, 0)
            this.nameLab.string = this.config.name + '(任务目标)'
        } else {
            this.nameLab.node.color = cc.Color.BLACK
        }
        if (this.config.attackNum < 10000) {
            this.zhanliDesc.string = Common5.getNumberChangeHanzi(this.config.attackNum) + '点击/次'
        } else {
            this.zhanliDesc.string = Common5.getNumberChangeHanzi(this.config.attackNum) + '点击/次'
        }

        this.jiangDesc.string = '金币奖励'
        if (this.config.challengeId == curChallengeId) {
            //当前需要挑战的
            huiseBtn.active = false
            againBtn.active = false
            curBtn.active = true
            let peopleClick = LevelUpManager.getCurClickIncome()
            if (peopleClick >= this.config.attackNum) {
                this.redPoint.active = true
            } else {
                this.redPoint.active = false
            }


        } else if (this.config.challengeId < curChallengeId) {
            //再次挑战的
            huiseBtn.active = false
            againBtn.active = true
            curBtn.active = false



        } else {
            //不能挑战的
            huiseBtn.active = true
            againBtn.active = false
            curBtn.active = false
            qianzhiDesc.active = true
            jiangIcon.active = false
            zhanIcon.active = false
            this.nameLab.node.active = true
            this.zhanliDesc.node.active = false
            this.jiangDesc.node.active = false

        }
    }

    initView(config) {

        this.config = config
        this.refreshView()

    }



    //挑战
    challengeBtnClick() {

        let mainId = TaskManager.getCurUnLockMainTaskId()
        let config = TaskManager.getTaskConfigById(mainId)
        if (mainId == MainTaskIdEnum.MainTask_506) {
            if (config.taskNum && User.getMoney() >= config.taskNum) {
                cc.audioEngine.stopMusic()
                PrefabManage.loadPrefabByType(GameType.DH_8, null, (node) => {
                    node.getComponent(DH_8).init(() => {
                        cc.audioEngine.pauseMusic()
                        PrefabManage.loadPrefabByType(GameType.ChallengeGame, null, (prefab) => {
                            prefab.getComponent('ChallengeGame').setViewDate(this.config, 'first')
                        })
                    })
                })
            } else {
                PrefabManage.showTextTips("金钱不够")
            }
        } else {
            PrefabManage.loadPrefabByType(GameType.ChallengeGame, null, (prefab) => {
                prefab.getComponent('ChallengeGame').setViewDate(this.config, 'first')
            })
        }
    }

    //再次挑战
    challengeAgainClick() {

        PrefabManage.loadPrefabByType(GameType.ChallengeGame, null, (prefab) => {
            prefab.getComponent('ChallengeGame').setViewDate(this.config, 'second')
        })
    }

    // update (dt) {}
}