class PrettyMessageBox extends Engine.Dialog
{
    //public delegate void MessageBoxCallback(bool doSomething);
    constructor(parentClip /*Drawable*/)
    {
        super(parentClip);

        this.okButton = new Engine.DrawableButton("Button_SimpleGreen", "OK");
        this.cancelButton = new Engine.DrawableButton("Button_SimpleRed", "Cancel");
        this.text = new Engine.DrawableText();
        this.scrollText = new ScrollingTextBox();
        this.boxWidth = 380;
        this.boxHeight = 200;
        this.fixedBoxWidth = 380;
        this.fixedBoxHeight = 200;
        this.callbackFunction = null;
        
        this.background = new Engine.Drawable("BGStandard");
        
        this.text.textAreaWidth = this.boxWidth * 0.75;
        this.text.textAreaHeight = this.boxHeight * 0.75;
        
        this.name = "MessageBox";
        this.background.setNineRect(0.4, 0.4, 0.6, 0.6);
        this.background.width = this.boxWidth;
        this.background.height = this.boxHeight;
        
        this.backgroundBounds = new Engine.Drawable();
        this.backgroundBounds.setAsBox(this.backgroundBounds.graphics, this.boxWidth, this.boxHeight, new Color(0, 0, 0, 127), 1);
        
        this.addChild(this.background);
        this.addChild(this.text);
        
        this.okButton.setWidth(100);
        this.cancelButton.setWidth(100);
        
        var space = (this.boxWidth - this.okButton.width - this.cancelButton.width) / 3;
        
        this.okButton.y = this.boxHeight - (this.okButton.height * 1.5);
        this.okButton.x = space;
        
        this.cancelButton.y = this.okButton.y;
        this.cancelButton.x = this.okButton.x + this.okButton.width + space;
        
        this.addChild(this.okButton);
        this.addChild(this.cancelButton);
        
        this.cancelButton.onClick = () => this.clickedCancel();
        this.okButton.onClick = () => this.clickedOK();
        
        this.dialogWidth = this.boxWidth;
        this.dialogHeight = this.boxHeight;
    }

    clickedCancel()
    {
        if (this.callbackFunction != null) this.callbackFunction(false);
        this.hide();
    }

    clickedOK()
    {
        if (this.callbackFunction != null) this.callbackFunction(true);
        this.hide();
    }

    show(message /*String*/, okText /*String*/, closeText /*String*/, callbackFunction /*Action<bool>*/)
    {
        this.active = true;
        this.callbackFunction = callbackFunction;
        this.parentClip.addChild(this);
        
        this.boxWidth = this.fixedBoxWidth;
        this.boxHeight = this.fixedBoxHeight;
        
        this.background.width = this.boxWidth;
        this.background.height = this.boxHeight;
        
        this.addChild(this.text);
        this.removeChild(this.scrollText);

        this.text.textAreaWidth = this.background.width * 0.75;
        this.text.setText(message, "Arial", 14);
        
        this.okButton.setButtonText(okText);
        this.cancelButton.setButtonText(closeText);
        
        if (closeText == "") this.removeChild(this.cancelButton);
        else this.addChild(this.cancelButton);
        
        if (okText == "") this.removeChild(this.okButton);
        else this.addChild(this.okButton);
                
        this.layout();
        this.showDialog();
    }

    layout()
    {
        //Rect texDims = text.getRect();
        //text.SetTint(Color.blue);
        this.text.x = (this.boxWidth - this.text.width) * 0.5;
        this.text.y = 30;
        
        this.scrollText.x = 10;
        this.scrollText.y = this.text.y;

        var space = (this.boxWidth - this.okButton.width - this.cancelButton.width) * 0.33;
        this.okButton.x = space;
        if (!this.cancelButton.hasParent) this.okButton.x = (this.boxWidth * 0.5 - this.okButton.width * 0.5);
        this.okButton.y = this.boxHeight - (this.okButton.height * 1.5);

        this.cancelButton.x = this.okButton.x + this.okButton.width + space;
        if (!this.okButton.hasParent) this.cancelButton.x = this.boxWidth * 0.5 - this.cancelButton.width * 0.5;
        this.cancelButton.y = this.boxHeight - (this.cancelButton.height * 1.5);

        this.dialogWidth = this.boxWidth;
        this.dialogHeight = this.boxHeight;

        // autosize the box on default view
        if (this.text.y + this.text.height > this.okButton.y)
        {
            this.background.height = this.text.y + this.text.height + this.okButton.height * 2;
            this.dialogHeight = this.background.height;
            this.okButton.y = this.background.height - (this.okButton.height * 1.5);
            this.cancelButton.y = this.okButton.y;
        }
    }
}