/**
 * 节点置灰组件
 * @date 20210514
 * @author kaka
 * @description 暂时不包括spine动画
 */

const { ccclass, property } = cc._decorator;

@ccclass
export default class SetGray extends cc.Component {
    @property(cc.Material)
    private normal: cc.Material = null;   //内置正常材质

    @property(cc.Material)
    private gray: cc.Material = null;   //内置灰色材质

    @property({ type: false, displayName: "是否置灰" })
    public grayState: boolean = false;   //默认置灰状态

    //上次置灰状态
    private lastState = false;
    private spArr = null;
    private btnArr = null;
    private labArr = null;
    private labArrC = [];
    private outLineArr = null;
    private outLineArrC = [];
    private labRichArr = null;
    private labRichArrC = [];
    private grayColor = new cc.Color(166, 166, 166, 255);
    private grayOutLineColor = new cc.Color(230, 230, 230, 255);

    private skeArr = null;

    onLoad() {
        this.spArr = this.node.getComponentsInChildren(cc.Sprite);
        this.btnArr = this.node.getComponentsInChildren(cc.Button);
        this.labArr = this.node.getComponentsInChildren(cc.Label);
        this.outLineArr = this.node.getComponentsInChildren(cc.LabelOutline);
        this.labRichArr = this.node.getComponentsInChildren(cc.RichText);
        this.skeArr = this.node.getComponentsInChildren(sp.Skeleton);

        this.setGray(this.grayState);
    }

    /**
     * 设置颜色
     * @param isGray true 置灰 false 还原正常 
     */
    setGray(isGray: boolean) {
        if (this.lastState == isGray) {
            console.log('和上次设置相同')
            return
        }
        this.lastState = isGray;
        //置灰
        if (isGray) {
            //内建材质
            let len = this.spArr.length;
            for (var i = 0; i < len; i++) {
                this.spArr[i].setMaterial(0, this.gray);
            }

            let btnLen = this.btnArr.length;
            for (var n = 0; n < btnLen; n++) {
                (this.btnArr[n] as cc.Button).enableAutoGrayEffect = true;
                (this.btnArr[n] as cc.Button).grayMaterial = this.gray;
                (this.btnArr[n] as cc.Button).interactable = false;
            }

            //设置所有文本颜色
            let labLen = this.labArr.length;
            for (var j = 0; j < labLen; j++) {
                this.labArrC[j] = this.labArr[j].node.color;
                this.labArr[j].node.color = this.grayColor;
            }

            //设置所有描边颜色
            let outLineLen = this.outLineArr.length;
            for (var k = 0; k < outLineLen; k++) {
                this.outLineArrC[k] = this.outLineArr[k].color;
                this.outLineArr[k].color = this.grayOutLineColor;
            }

            //设置所有富文本颜色
            let labRichLen = this.labRichArr.length;
            for (var m = 0; m < labRichLen; m++) {
                this.labRichArrC[m] = this.labRichArr[m].node.color;
                this.labRichArr[m].node.color = this.grayColor;
            }

            // let skeLen = this.skeArr.length;
            // for (var p = 0; p < skeLen; p++) {
            //     this.skeArr[p].setMaterial(0, this.gray);
            //     (this.skeArr[p] as any).markForRender(true);
            // }
        }
        else {
            //内建材质
            let len = this.spArr.length;
            for (var i = 0; i < len; i++) {
                this.spArr[i].setMaterial(0, this.normal);
            }

            let btnLen = this.btnArr.length;
            for (var n = 0; n < btnLen; n++) {
                (this.btnArr[n] as cc.Button).interactable = true;
            }

            //设置所有文本颜色
            let labLen = this.labArr.length;
            for (var j = 0; j < labLen; j++) {
                this.labArr[j].node.color = this.labArrC[j];
            }

            //设置所有描边颜色
            let outLineLen = this.outLineArr.length;
            for (var k = 0; k < outLineLen; k++) {
                this.outLineArr[k].color = this.outLineArrC[k];
            }

            //设置所有富文本颜色
            let labRichLen = this.labRichArr.length;
            for (var m = 0; m < labRichLen; m++) {
                this.labRichArr[m].node.color = this.labRichArrC[m];
            }

            // let skeLen = this.skeArr.length;
            // for (var p = 0; p < skeLen; p++) {
            //     this.skeArr[p].setMaterial(0, this.normal);
            //     (this.skeArr[p] as any).markForRender(true);
            // }
        }
    }
}
