import CashOutUI from "../CashOutUI";
import GameMgr, { UIITEM_NAME } from "../../mgr/GameMgr";
import PlayerConst from "../../data/PlayerConst";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CashOutItem extends cc.Component {

    @property(cc.Node)
    node_tip: cc.Node = null;
    @property(cc.Label)
    label_tip: cc.Label = null;
    @property(cc.Sprite)
    spr_tipBg: cc.Sprite = null;


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

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

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

    public cashOutUI: CashOutUI = null;

    public color_tip_cashOuting: cc.Color = new cc.Color(248, 213, 72);
    public color_tip_normal: cc.Color = new cc.Color(247, 44, 33);

    public color_bg_normal: cc.Color = new cc.Color(255, 255, 255);
    public color_bg_select: cc.Color = new cc.Color(25, 185, 103);
    public color_bg_cashOuting: cc.Color = new cc.Color(255, 255, 255);
    public color_bg_cashOuted: cc.Color = new cc.Color(174, 174, 174);

    public cashIndex: number = null;
    public curShowNpde: cc.Node = null;
    public cashOutItemState: CASHOUTITEMSTATE = null;

    /**需满足的天数 */
    public day: number = null;
    /**需满足的视频次数 */
    public videoTime: number = 30;
    /**提现数量 */
    public cashOutNum: number = null;
    /**提现提示 */
    public cashOutTip: string = "";


    public ifSelect: boolean = false;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {

        let node_cashOutUI = mk.ui.getCurOnPanel("CashOutUI");
        if (node_cashOutUI) {
            this.cashOutUI = node_cashOutUI.getComponent(CashOutUI);
        }
        
        this.node.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
    }

    // update (dt) {}

    /**
     * 初始化
     * @param cashIndex 提现item的索引值 
     * @param cashOutItemState 提现item的状态
     */
    init(cashIndex: number, cashOutItemState: CASHOUTITEMSTATE = CASHOUTITEMSTATE.Normal) {

        this.cashIndex = cashIndex;

        this.cashOutItemState = cashOutItemState;

        //读取本地配置
        // let config = GameConst.config_cashOut[cashIndex];
        // this.day = Number(config.day);
        // this.cashOutNum = Number(config.denominationAmount);
        // this.cashOutTip = config.cashOutTip;
        // LogUtil.log("PlayerConst.clockInCashInfo",PlayerConst.clockInCashInfo,cashIndex);
        let config = PlayerConst.clockInCashInfo[cashIndex];
        // LogUtil.log("config",config);
        this.day = Number(config.signinTimes);
        this.cashOutNum = Number(config.denominationAmount);
        this.videoTime = Number(config.videoTimes);

        if (cashIndex == (PlayerConst.clockInCashInfo.length - 1)) {
            this.cashOutTip = `满${this.cashOutNum}即可提现`;
        }
        else {
            this.cashOutTip = `${this.day}天打卡专享`;
        }

        this.initTip();
        this.initCashNum();
    }

    initTip() {

        if (this.cashOutItemState == CASHOUTITEMSTATE.CashOuting) {
            this.label_tip.string = "提现中...";
            this.spr_tipBg.node.color = this.color_tip_cashOuting;
            this.label_tip.node.color = cc.Color.BLACK;
        }
        else if (this.cashOutItemState == CASHOUTITEMSTATE.CashOuted) {
            this.label_tip.string = "已经提过喽";
            this.spr_tipBg.node.color = this.color_tip_normal;
            this.label_tip.node.color = cc.Color.WHITE;
        }
        else {
            this.label_tip.string = this.cashOutTip;
            this.spr_tipBg.node.color = this.color_tip_normal;
            this.label_tip.node.color = cc.Color.WHITE;
        }

        let timeOut = setTimeout(() => {
            this.node_tip.width = this.label_tip.node.width + 35;
            this.spr_tipBg.node.width = this.node_tip.width;
            this.node_tip.x = (this.node.width - this.node_tip.width) * 0.5;
        }, 10)
    }

    initCashNum() {
        this.label_cashNum.string = this.cashOutNum.toString();
        switch (this.cashOutItemState) {
            case CASHOUTITEMSTATE.Normal:
                this.node_bg.color = this.color_bg_normal;
                break;
            case CASHOUTITEMSTATE.Select:
                this.node_bg.color = this.color_bg_select;
                break;
            case CASHOUTITEMSTATE.CashOuting:
                this.node_bg.color = this.color_bg_cashOuting;
                break;
            case CASHOUTITEMSTATE.CashOuted:
                this.node_bg.color = this.color_bg_cashOuted;
                break;
        }
    }

    changeState(cashOutItemState: CASHOUTITEMSTATE) {

    }

    select() {
        this.label_cashNum.node.color = cc.Color.WHITE;
        this.node_selectBg.active = true;
    }

    cancelSelect() {
        this.label_cashNum.node.color = cc.Color.BLACK;
        this.node_selectBg.active = false;
    }

    onClick() {
        mk.console.log("点击！！！！！！！！！！！！！！！！！！！！！！！！！！", this.cashOutItemState, CASHOUTITEMSTATE.CashOuted);

        GameMgr.Inst.sendEvent(UIITEM_NAME.CashOutItem, `点击${this.cashOutNum}元档位`);

        this.cashOutUI.setCurSelectItemBtn(this);
    }
}

export enum CASHOUTITEMSTATE {
    Normal = 0,
    Select = 1,
    CashOuting = 2,
    CashOuted = 3
}
