
import { Button, Component, find, Label, Node, ProgressBar, RichText, v3, Widget } from 'cc';
import { constants } from '../game/data/constants';
import { msgac } from '../game/data/msgac';
import { audioManager } from '../game/manager/audioManager';
import { eventManager } from '../game/manager/eventManager';
import { layerManager } from '../game/manager/layerManager';
import { cocosUtil } from '../utils/cocosUtil';

export class BaseLayer extends Component {

    obj: any;
    layerName: string = "DefaultLayer";

    // {路径:节点对象}
    pathNodeObj: any = {};

    onLoad() {
        // 背景适配
        let bgNode = find("bg", this.node);
        this.screenAdapterBG(bgNode);

        // ui适配
        let uiNode = find("ui", this.node);
        this.screenAdapterUI(uiNode);

        this.addButtonListener(this.node);
        this.addPathNode(this.node, "");
    }

    addPathNode(node: Node, path: string) {
        if (node != this.node) {
            this.pathNodeObj[path] = node;
        }
        if (path) {
            path += "/";
        }
        let chs = node.children;
        for (let i = 0, len = chs.length; i < len; i++) {
            let pNode = chs[i];
            this.addPathNode(pNode, path + pNode.name);
        }
    }

    getNodeByPath(path: string) {
        let node = this.pathNodeObj[path];
        if (node) {
            return node;
        }

        node = find(path, this.node);
        if (node) {
            this.pathNodeObj[path] = node;
            return node;
        }

        return null;
    }

    /**
     * 缩放背景图，达到满屏显示，背景图部分内容超出屏幕也没事
     * @param node 背景图节点
     */
    screenAdapterBG(node: Node) {
        if (!cocosUtil.isNodeValid(node)) {
            return;
        }
        let widget = node.getComponent(Widget);
        if (widget) {
            widget.enabled = false;
        }
        let scale = cocosUtil.getScreenBgAdapterScale();
        node.setScale(v3(scale, scale, 1));
    }

    /**
     * 缩放UI，达到屏幕适配，ui上的全部内容在屏幕内显示，如果是全屏UI，ui节点如果有widget，缩放后依然有效
     * @param node ui根节点
     */
    screenAdapterUI(node: Node) {
        if (!cocosUtil.isNodeValid(node)) {
            return;
        }
        let scale = cocosUtil.getScreenAdapterScale();
        node.setScale(v3(scale, scale, 1));
    }

    addButtonListener(node: Node) {
        if (node.getComponent(Button)) {
            node.on("click", this.preOnButtonClick, this);
        }

        let chs = node.children;
        for (let i = 0, max = chs.length; i < max; i++) {
            let ch = chs[i];
            this.addButtonListener(ch);
        }

    };

    preOnButtonClick(btn: any) {
        let node = btn.node;
        let name = node.name;
        console.log(this.layerName + " onButtonClick " + name);

        switch (name) {
            case "btnClose":
                this.closeLayer();
                break;
            default:
                break;
        }

        this.onButtonClick(node, node.name);
        if (!node["hasPlayAudio"]) {
            audioManager.instance.onButtonClick(this, node);
        }
    }

    onButtonClick(node: Node, name: String) {

    }

    openLayer(layer: object, data?: any, cb?: Function) {
        layerManager.instance.openLayer(layer, data, cb);
    }

    closeLayer() {
        layerManager.instance.closeLayer(this);
    }

    addEventArr(arr: any) {
        for (let i in arr) {
            let val = arr[i];
            if (!val) {
                continue;
            }
            eventManager.instance.on(val, this.process_event.bind(this), this);
        }
    }

    removeEventArr(arr: any) {
        for (let i in arr) {
            let val = arr[i];
            if (!val) {
                continue;
            }
            eventManager.instance.off(val, this.process_event.bind(this), this);
        }
    }

    process_event(event: any) {

    }

    createNotice(content) {
        layerManager.instance.createNotice(content);
    }

    openCoinGetEffectLayer(node: Node) {
        let info: any = {};
        info.name = constants.layers.CoinGetEffectLayer.name;
        info.posStart = cocosUtil.convertToWorldSpace(node);
        eventManager.instance.send(msgac.openLayer, info);
    }

    setProgressBar(progressBar: Node | ProgressBar, percent: number) {
        if (progressBar instanceof Node) {
            progressBar = progressBar.getComponent(ProgressBar);
        }
        if (progressBar) {
            progressBar.progress = percent;
        }
    }

    setString(node: Node, info: any) {
        info = info + "";
        let label = node.getComponent(Label);
        if (label) {
            label.string = info;
            return;
        }

        let rich = node.getComponent(RichText);
        if (rich) {
            rich.string = info;
            return;
        }
    }

}

