// 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 taskState from "../DH/taskState";
import ChaoCai from "../GameMethodRes/ChaoCai/ChaoCai";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
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)
    ganxie_str: cc.Label = null;

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

    @property(cc.Node)
    btn_renzheng: cc.Node = null;
    @property(cc.Node)
    btn_shenqing: 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() {
        // this.updateMainTask()
        EventMgr.onEvent_custom(ryw_Event.updateMainTask, () => {
            this.updateMainTask()
        }, this)
    }

    init(data) {
        this.config = data
        // Common5.addUrlSprite_custom(this.config.icon, this.icon)
        Common5.getSpriteFrameFromBundle("ChuShiXieHui", this.config.icon, this.icon)
        this.title_str.string = data.descLab
        this.miaoshu_str.string = data.miaoshu
        this.money_str.string = `认证费用:${Common5.getNumberChangeHanzi(data.moneystr)}`
        if (!data.ganxiezhi) {
            this.ganxie_str.node.active = false
            this.miaoshu_str.node.y = 0
        } else {
            this.ganxie_str.node.active = true
            this.miaoshu_str.node.y = 17
            this.ganxie_str.string = `公益选票:${data.ganxiezhi}`
        }
        this.refreshState()
    }

    refreshState() {
        this.btn_renzheng.active = false
        this.btn_shenqing.active = false
        this.btn_weiwancheng.active = false
        this.yiwancheng.active = false
        let mylevel = User.getMyLevel()
        let ChuShiXieHuiLevel = User.getChuShiXieHuiLevel()
        let configId = this.config.configId
        if (ChuShiXieHuiLevel[configId] == 1) {
            this.yiwancheng.active = true
        } else {
            if (mylevel >= this.config.dengji && User.getMoney() >= this.config.moneystr) {
                if (this.config.isShenQing) {
                    this.btn_shenqing.active = true
                } else {
                    this.btn_renzheng.active = true
                }
            } else {
                this.btn_weiwancheng.active = true
            }
        }
    }

    onTouchRenZheng() {
        if (User.getMoney() >= this.config.moneystr && User.getMyLevel() >= this.config.dengji) {
            PrefabManage.loadPrefabByType(GameType.ChaoCai, null, (prefabNode) => {
                prefabNode.getComponent(ChaoCai).initView(this.config);
            })
        } else {
            PrefabManage.showTextTips(`人物等级需要${this.config.dengji},报名费用需要${Common5.getNumberChangeHanzi(this.config.moneystr)}`)
        }

        // let ChuShiXieHuiLevel = User.getChuShiXieHuiLevel()
        // UserManager.subMoney(this.config.moneystr)
        // if (this.config.configId <= ChuShiXieHuiLevel) {

        // } else {
        //     User.setChuShiXieHuiLevel(ChuShiXieHuiLevel + 1)
        // }
        // this.btn_renzheng.active = false
        // this.btn_shenqing.active = false
        // this.yiwancheng.active = true
    }

    onTouchShenQing() {
        if (User.getMoney() >= this.config.moneystr && User.getMyLevel() >= this.config.dengji) {


            // if (this.config.configId <= ChuShiXieHuiLevel[configId]) {

            // } else {
            //     User.setChuShiXieHuiLevel(ChuShiXieHuiLevel + 1)
            // }



            let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
            let mainId = mainTaskInfo.Id
            if (mainId == MainTaskIdEnum.MainTask_516_2 && this.config.descLab == '协会会员') {
                let ChuShiXieHuiLevel = User.getChuShiXieHuiLevel()
                UserManager.subMoney(this.config.moneystr)
                let configId = this.config.configId
                ChuShiXieHuiLevel[configId] = 1

                this.btn_renzheng.active = false
                this.btn_shenqing.active = false
                this.yiwancheng.active = true

                PrefabManage.loadPrefabByType(GameType.taskState, null, (node) => {
                    node.getComponent(taskState).showSuccess("恭喜成为厨师协会会员", () => {
                    })
                })

                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_516_2)
                TaskManager.setCurTask(MainTaskIdEnum.MainTask_517)
            } else if (mainId == MainTaskIdEnum.MainTask_540 && this.config.descLab == '协会副会长') {
                PrefabManage.loadPrefabByType(GameType.DH_31)
            } else if (mainId == MainTaskIdEnum.MainTask_552 && this.config.descLab == '协会会长') {
                PrefabManage.loadPrefabByType(GameType.DH_40)
            }
        } else {
            PrefabManage.showTextTips(`人物等级需要${this.config.dengji},报名费用需要${Common5.getNumberChangeHanzi(this.config.moneystr)}`)
        }
    }

    updateMainTask() {
        // let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        // let mainId = mainTaskInfo.Id
        // let config = TaskManager.getTaskConfigById(mainId)
        this.refreshState()
    }
    // update (dt) {}
}