Engine.DialogLayer = class extends Engine.Drawable
{

    constructor(name)
    {
        super();
        this.name = name;

        this.activeBlockers = 0;
        this.updateCallback = null;

        this.blockers = [];
    }


    resolutionChanged(width, height)
    {
        for (var i = 0; i < this.blockers.length; i++)
        {
            this.blockers[i].draw(EngineSettings.screenWidth, EngineSettings.screenHeight);
        }
    }

    addChild(child)
    {
        super.addChild(child);
        if (!child.isBlocker) this.updateBlockers();
    }

    addChildAt(child, index)
    {
        super.addChildAt(child, index);
        if (!child.isBlocker) this.updateBlockers();
    }

    removeChild(child)
    {
        super.removeChild(child);
        if (!child.isBlocker) this.updateBlockers();
    }

    removeChildAt(index)
    {
        var child = this.getChildAt(index);
        super.removeChildAt(index);
        if (!child.isBlocker) this.updateBlockers();
    }

    getTopDialog()
    {
        for (var i = this.children.length - 1; i >= 0; i--)
        {
            var drawableChild = this.getChildAt(i);
            if (drawableChild.isDialog) return drawableChild;
        }
        return null;
    }

    updateBlockers()
    {
        var len = this.blockers.length;
        this.bottomChildren = [];
        for (var i = this.children.length - 1; i >= 0; i--)
        {
            var child = this.getChildAt(i);
            if (child.isDialog && !child.alwaysOnBottom && child.blockerInfo != null)
            {
                var blockerInfo = child.blockerInfo;

                var found = false;
                for (var j = 0; j < this.blockers.length; j++)
                {
                    if (blockerInfo.equals(this.blockers[j].blockerInfo))
                    {
                        found = true;
                        if (!this.blockers[j].used)
                        {
                            var index = this.getChildIndex(this.blockers[j]);
                            if (index + 1 != i)
                            {
                                this.addChildAt(this.blockers[j], index < i ? i - 1 : i);
                            }
                            this.blockers[j].used = true;
                        }
                        break;
                    }
                }

                if (!found)
                {
                    var block = new Engine.DialogBlocker();
                    block.blockerInfo = new Engine.DialogBlockerInfo();
                    if (blockerInfo != null) 
                    {
                        block.blockerInfo.color = blockerInfo.color;
                        block.blockerInfo.alpha = blockerInfo.alpha;
                    }
                    block.drawFullScreen();
                    this.addChildAt(block, i);
                    block.used = true;
                    this.blockers.push(block);
                }

            }
            else if (child != null && child.alwaysOnBottom == true)
            {
                this.bottomChildren.push(child);
            }
        }

        for (var i = 0; i < this.bottomChildren.length; i++)
        {
            this.setChildIndex(this.bottomChildren[i], 0);
        }

        var count = 0;
        this.activeBlockers = 0;
        for (var i = 0; i < this.blockers.length; i++)
        {
            if (!this.blockers[i].used)
            {
                this.removeChild(this.blockers[i]);
                this.blockers[i].used = false;
                this.blockers.remove(this.blockers[i]);
                i--;
                count++;
            }
            else
            {
                this.activeBlockers++;
                this.blockers[i].used = false;
            }
        }

        

    }

}