import { RedPointInfo } from "./RedPointInfo";
import { IRedPointData } from "./IRedPointData";
import { RedCondEvent } from "./RedPointCoust";
import { RedPoint } from "./RedPoint";

export class RedPointTreeNode {


    constructor(){
        this.initData();
    }
    /**
     * 数据展示
     */
    public info: RedPointInfo = null;
    // /**
    //  * 是否绑定节点
    //  */
    // public isBind: boolean = false;
    /**
     * 展示类型
     */
    private showType: number = 0;

    // public showCount: number = 0;
    /**
     * id唯一值
     */
    private id: number;


    // public isBind: boolean = false;
    public data: IRedPointData = null;

    //自身是否有逻辑控制红点
    public isSelfControl: boolean = false;

    //是否要显示自身红点(不依赖子红点)
    public isSelfShowRed: boolean = false;

    public mCondition: Array<IRedTreeCond> = [];

    public callFunc: Function = null;

    /**
     *红点更新目标
     */
    public stepTag: number = 0;

    /**
     * 红点树初始化
     * @param info 
     * @param showType 
     * @param key 
     * @param isBind 
     */
    private initData() {
        this.id = RedPoint.initId++;
        let data: IRedPointData = {
            mId: '',
            mName: '',
            mFunctionId: 0,
            mParent: null,
            mParentNode: null,
            mRedNode: null,
            mRefreshType: 0,
            mShowCount: 0,
            mShowType: -1
        };
        this.data = data;
    }

   public get Id() {
    return this.id
   }

    /**
     * 绑定脚本
     * @param info 
     */
    public bindInfo(info: RedPointInfo, showType, isSelfControl: boolean = false) {
        this.info = info;
        this.showType = showType;
        this.isSelfControl = isSelfControl;
        console.log("updateRedPoint 绑定脚本" + this.data.mId);
        this.updateRedPoint(this.data.mShowType >= 0 ? true : false, this.data.mShowCount);
    }

 
    /**
     * 更新节点红点
     * @param isShow 
     * @param showCount 
     * @returns 
     */
    updateRedPoint(isShow: boolean, showCount: number) {
        // this.stepTag+= isShow?1:-1;
        let _showType: number = isShow ? this.showType : -1;
       

        this.data.mShowType = _showType;
        this.data.mShowCount = showCount;

        this.updateInfo();

        if (this.data.mParent) {
            if (isShow) {
                if (this.data.mParent.data.mShowType >= 0) return;
            } else {
                if (this.data.mParent.data.mShowType < 0) return;
            }
            this.data.mParent.updateRedPoint(isShow, showCount);
        }
    }
    private updateInfo() {
        if (this.data.mRedNode && this.data.mRedNode.active && this.data.mRedNode.isValid) {
            //   console.log("updateRedPoint" + this.data.mId, this.data.mRedNode);
            // this.info.updateRedPoint(this.data.mShowType, showCount);
            this.data.mRedNode.getChildByName('RedPoint').getComponent(RedPointInfo).updateRedPoint(this.data.mShowType, this.data.mShowCount);

        }
    }

    /**
     * 添加条件
     * @param cond 
     */
    public addCond(cond: IRedTreeCond) {
        this.mCondition.push(cond);
        this.updateCondCount(cond)
    }


    /**
     * 更新条件
     * @param event 
     * @param count 
     */
    public updateCondCount(event: IRedTreeCond) {
        let condIndex = this.mCondition.find(p => p.condEvent == event.condEvent && p.condId == event.condId && event.condType == p.condType);
        if (condIndex != null) {
            condIndex.currValue = event.currValue;
            if (event.condValue != 0) {
                condIndex.condValue = event.condValue;
            }
            this.updateCond();
        }
    }

    /**
     * 删除条件
     * @param cond 
     */
    public removeCond(redEvent: string) {
        let condIndex = this.mCondition.find(p => p.condEvent == redEvent);
        if (condIndex != null) {
            let index = this.mCondition.indexOf(condIndex);
            this.mCondition.splice(index, 1);
        }
        this.updateCond();
    }

    /**
     * 刷新红点
     */
    refresh() {
        let isShow = true;
        for (let i = 0; i < this.mCondition.length; i++) {
            let item = this.mCondition[i];
            if (item.condValue > item.currValue) {
                isShow = false;
                break;
            }
        }
        this.data.mShowType = isShow ? this.showType : -1;
        this.updateInfo();
    }


    /**
     * 更新条件
     */
    private updateCond() {
        let cond = this.callFunc==null?false: this.callFunc();
        if (this.data.mShowType < 0) {
            if (cond) {
                this.updateRedPoint(true, this.data.mShowCount);
            }
        } else {
            if (!cond) {
                this.updateRedPoint(false, this.data.mShowCount);
            }
        }

    }

}

export interface IRedTreeCond {
    /**
     * 条件Id
     */
    condId: number
    /**
     * 条件类型
     */
    condType: number
    /**
     * 条件值
     */
    condValue: number
    /**
     * 事件名字
     */
    condEvent: RedCondEvent;

    /**
     * 当前值
     */
    currValue: number;
}