import { DefaultLevelControl } from "./DefaultLevelControl";
import { LevelControlHelper } from "../LevelControlHelper";

const __ = {
    private: Symbol('private')
}

/**
 * @class RoomLevelControl
 * 园区的房间层级控制类。
 * @extends THING.DefaultLevelControl
 * @public
 */
class RoomLevelControl extends DefaultLevelControl {

    static levelCondition = 'tags:or(Room)';

    static levelName = 'room';

    constructor(options) {
        super(options);
        let _private = this[__.private] = {};
        _private.transparentObjects = [];
        _private.isObjectTransparent = false;
    }

    onSetEffect(param) {
        let _private = this[__.private];
        const object = param.current;

        if (object) {
            const brothers = object.brothers;

            LevelControlHelper.traverseByIgnore(object, 'ignoreStyle', (child) => {
                const style = child.body.style;
                if (style) {
                    style.opacity = null;
                    style.transparent = null;
                }
            });

            brothers.forEach(element => {
                const roomFloor = element.children.queryByTags(_private.isObjectTransparent ? 'RoomFloor||Placement||Entity||Line||Geometry||Region||Window||Door' : 'RoomFloor');
                if (roomFloor) {
                    LevelControlHelper.traverseByIgnore(roomFloor, 'ignoreStyle', (child) => {
                        const style = child.body.style;
                        if (style) {
                            style.opacity = 0.25;
                            style.transparent = true;
                        }
                        _private.transparentObjects.push(child);
                    });
                }
            });;
        }
    }

    onResetEffect(param) {
        let _private = this[__.private];

        // 如果开启了隐藏对象,并且接下来要进入的是子层级,就不用恢复半透明了
        if (_private.isObjectTransparent) {
            if (param.next.parent === param.current) {
                return;
            }
        }

        _private.transparentObjects.forEach(element => {
            const style = element.style;
            if (style) {
                style.opacity = null;
                style.transparent = null;
            }
        });
        _private.transparentObjects.length = 0;
    }

    filterObject(object) {
        const tags = object.tags;
        if (object.tags.has('Wall')) {
            return null;
        }

        if (tags.has('RoomFloor') || tags.has('RoomCeiling') || tags.has('RoomRoof')) {
            if (object.parent == object.app.level.current) {
                return null;
            }
        }

        if (object.parents.find('tags:or(Misc,Wall)')) {
            return null;
        }

        return super.filterObject(object);
    }

    onFlyTo(flyParam) {
        const target = flyParam.target;

        if (target && target.isObject3D) {
            const boundingBox = target.boundingBox;

            flyParam.position = LevelControlHelper.getNearestObjectPosition({
                target: target,
                boundingBox: boundingBox,
                camera: this.app.camera,
            });

            flyParam.target = boundingBox.center;
        }

        return super.onFlyTo(flyParam);
    }

    /**
     * 开启/关闭 物体半透明。(默认关闭,开启后进入房间时会半透明其他房间下的物体。并且在继续进入子层级时不会主动恢复其他房间的半透明,只有返回父层级才会恢复其他房间的半透明。)
     * @type {boolean}
     * @public
     */
    get isObjectTransparent() {
        const _private = this[__.private];
        return _private.isObjectTransparent;
    }
    set isObjectTransparent(value) {
        const _private = this[__.private];
        _private.isObjectTransparent = !!value;
    }

}

export { RoomLevelControl };