// 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 ZaoCanManager from "../Manager/ZaoCanManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import ZaoCanDianUnLock from "./ZaoCanDianUnLock";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ZaoCanDianYeWuitemNode extends cc.Component {

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

    @property(cc.Sprite)
    headImg: cc.Sprite = null;
    @property(cc.Label)
    jiachengPercent: cc.Label = null;

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

    @property(cc.ProgressBar)
    levelProgress: cc.ProgressBar = null;

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

    // LIFE-CYCLE CALLBACKS:
    config = null
    // onLoad () {}

    start() {
    }

    setViewDate(config) {
        this.config = config
        this.refreshView()
    }
    refreshView() {
        let lockType = ZaoCanManager.getIsLockById(this.config.id)
        if (lockType == 0) {
            //未解锁
            this.node.getChildByName("未解锁").active = true

            this.node.getChildByName("已解锁").active = false
            this.node.getChildByName("等待解锁").active = false
            this.node.getComponent(cc.Sprite).enabled = false
            this.node.getChildByName("闪光框").active = false
        } else if (lockType == 1) {
            //待解锁
            this.node.getChildByName("等待解锁").active = true
            this.node.getComponent(cc.Sprite).enabled = true
            this.node.getChildByName("闪光框").active = true

            this.node.getChildByName("未解锁").active = false
            this.node.getChildByName("已解锁").active = false
        } else if (lockType == 2) {
            //已解锁
            this.node.getChildByName("已解锁").active = true

            this.node.getChildByName("未解锁").active = false
            this.node.getChildByName("等待解锁").active = false
            this.node.getComponent(cc.Sprite).enabled = false
            this.node.getChildByName("闪光框").active = false
        }
        let str = Common5.getNumberChangeHanzi(this.config.jiacheng * 100)
        this.jiachengPercent.string = str + '%'
        this.nameLabel.string = this.config.zaocan

        let zaocan = this.config.zaocan
        Common5.getSpriteFrameFromBundle("ZaoCanDian", 'res/icon/' + zaocan, this.headImg)

        this.levelLabel.string = `店铺${this.config.needLevel}级解锁`

        let level = User.getMyLevel()
        this.levelProgress.progress = level / this.config.needLevel
        this.levelPercentLabel.string = `${level}/${this.config.needLevel}`
    }

    onTouchUnLock() {
        let lockType = ZaoCanManager.getIsLockById(this.config.id)
        //是不是等待解锁的状态
        if (lockType == 1) {
            +
            PrefabManage.loadPrefabByType(GameType.ZaoCanDianUnLock, null, (node) => {
                node.getComponent(ZaoCanDianUnLock).setViewData(this.config.id)

                ZaoCanManager.setUnLock(this.config.id)
                // this.refreshView()
                EventMgr.emitEvent_custom(ryw_Event.refreshZaoCan)
            })
        }
    }

    // update (dt) {}
}