// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

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

const { ccclass, property } = cc._decorator;

@ccclass
export default class ClockInItem extends cc.Component {


    /**普通bg */
    @property(cc.Node)
    node_nromalClockInBg: cc.Node = null;
    /**未打卡bg */
    @property(cc.Node)
    node_unClockInBg: cc.Node = null;
    /**已经打卡icon */
    @property(cc.Node)
    node_haveClockInIcon: cc.Node = null;

    /**红包 */
    @property(cc.Node)
    node_reaPacket: cc.Node = null;
    /**提示 */
    @property(cc.Node)
    node_tip: cc.Node = null;
    /**宝箱 */
    @property(cc.Node)
    node_box: cc.Node = null;

    /**当前打卡天 */
    public curClockInDay: number = 0;
    /**奖励类型 null 是无奖励 1 红包 2 是宝箱 */
    public rewardType: number = null;
    /**奖励数量 */
    public rewardNum: number = null;

    /**红包数目 */
    public reaPacketCashNum: number = 0;

    /**是否打卡成功 */
    public ifClockInDay: boolean = false;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        this.node.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
    }

    // update (dt) {}

    init(day: number = null) {
        // LogUtil.log("day",day)

        // if (!day) {
        //     return;
        // }

        if (day) {
            this.curClockInDay = day;
        }

        // LogUtil.log("this.curClockInDay", this.curClockInDay);

        let config = GameConst.config_clockIn[this.curClockInDay - 1];
        // LogUtil.log("GameConst.config_clockIn", GameConst.config_clockIn);

        this.rewardType = config.rewardType;
        this.rewardNum = config.rewardNum

        this.ifClockInDay = PlayerConst.ifClockInToday; 

        let curRealClockInDay: number = 0;
        //如果已经打卡  1 对应 1
        if (this.ifClockInDay) {
            curRealClockInDay = this.curClockInDay;
        }
        //如果未打开   1 对应 0
        else {
            curRealClockInDay = this.curClockInDay - 1;
        }

        if (curRealClockInDay < PlayerConst.clockInDay) {
            this.node_unClockInBg.active = false;

            this.node_nromalClockInBg.active = true;
            if (this.rewardType == 1) {
                this.node_nromalClockInBg.setScale(1, 1);
            }
            else {
                this.node_nromalClockInBg.setScale(0.8, 0.8);
            }
            this.node_reaPacket.active = this.rewardType == 1;
            this.node_haveClockInIcon.active = true;
            
            this.node_tip.active = false;

            this.node_box.active = this.curClockInDay == GameConst.maxClockInDay;
        }
        else if (curRealClockInDay == (PlayerConst.clockInDay)) {

            this.node_unClockInBg.active = false;

            this.node_nromalClockInBg.active = true;
            this.node_nromalClockInBg.setScale(1, 1);

            this.node_reaPacket.active = this.rewardType == 1;
            mk.console.log(" this.ifClockInDay", this.ifClockInDay);
            this.node_haveClockInIcon.active = this.ifClockInDay;
            this.node_tip.active = !this.ifClockInDay;

            this.node_box.active = this.curClockInDay == GameConst.maxClockInDay;
        }
        else {

            this.node_haveClockInIcon.active = false;
            this.node_nromalClockInBg.active = false;
            this.node_unClockInBg.active = true;
            this.node_reaPacket.active = this.rewardType == 1;

            this.node_box.active = this.rewardType == 2;
        }



        // if (this.curClockInDay < PlayerConst.clockInDay) {

        //     //第一天情况特殊
        //     if (PlayerConst.clockInDay == 0) {
        //         this.node_unClockInBg.active = false;

        //         this.node_nromalClockInBg.active = true;
        //         this.node_nromalClockInBg.setScale(1, 1);

        //         this.node_reaPacket.active = this.rewardType == 1;
        //         LogUtil.log(" this.ifClockInDay", this.ifClockInDay);
        //         this.node_haveClockInIcon.active = this.ifClockInDay;
        //         this.node_tip.active = !this.ifClockInDay;

        //         this.node_box.active = this.curClockInDay == GameConst.maxClockInDay;
        //     }
        //     else {
        //         this.node_unClockInBg.active = false;

        //         this.node_nromalClockInBg.active = true;
        //         if (this.rewardType == 1) {
        //             this.node_nromalClockInBg.setScale(1, 1);
        //         }
        //         else {
        //             this.node_nromalClockInBg.setScale(0.8, 0.8);
        //         }
        //         this.node_reaPacket.active = this.rewardType == 1;
        //         this.node_haveClockInIcon.active = true;

        //         this.node_box.active = this.curClockInDay == GameConst.maxClockInDay;
        //     }
        // }
        // else if (this.curClockInDay == (PlayerConst.clockInDay)) {

        //     this.node_unClockInBg.active = false;

        //     this.node_nromalClockInBg.active = true;
        //     this.node_nromalClockInBg.setScale(1, 1);

        //     this.node_reaPacket.active = this.rewardType == 1;
        //     LogUtil.log(" this.ifClockInDay", this.ifClockInDay);
        //     this.node_haveClockInIcon.active = this.ifClockInDay;
        //     this.node_tip.active = !this.ifClockInDay;

        //     this.node_box.active = this.curClockInDay == GameConst.maxClockInDay;
        // }
        // else {

        //     this.node_haveClockInIcon.active = false;
        //     this.node_nromalClockInBg.active = false;
        //     this.node_unClockInBg.active = true;
        //     this.node_reaPacket.active = this.rewardType == 1;

        //     this.node_box.active = this.rewardType == 2;
        // }
    }

    onClick() {

        GameMgr.Inst.sendEvent(UIITEM_NAME.ClockInItem, `点击${this.curClockInDay}打卡Item`);

        if (this.curClockInDay == PlayerConst.clockInDay && this.ifClockInDay) {
            if (this.rewardType != null) {
                EffectMgr.Inst.addTip(`打卡成功，可以提现啦`);
            }
            else {
                EffectMgr.Inst.addTip(`继续累计打卡，领取后面红包吧`);
            }
        }
        else {
            if (this.rewardType != null) {
                if (this.curClockInDay == GameConst.maxClockInDay) {
                    EffectMgr.Inst.addTip(`连续打卡${this.curClockInDay}天,领取神秘宝箱`)
                }
                else {
                    EffectMgr.Inst.addTip(`连续打卡${this.curClockInDay}天,提现${this.rewardNum}元`)
                }
            }
        }
    }
}
