using FastReport.Dialog;
using System.Windows.Forms;
using static FastReport.Web.Constants;

namespace FastReport.Web
{
    public partial class Dialog
    {

        private void ButtonClick(ButtonControl button)
        {
            if (button.DialogResult == DialogResult.OK)
            {
                if (FormClose(button, CloseReason.None))
                    CurrentForm++;
            }
            else if (button.DialogResult == DialogResult.Cancel)
            {
                if (FormClose(button, CloseReason.UserClosing))
                    _webReport.Canceled = true;
            }
            button.OnClick(null);
        }

        private bool FormClose(ButtonControl button, CloseReason reason)
        {
            DialogPage dialog = button.Report.Pages[CurrentForm] as DialogPage;
            dialog.Form.DialogResult = button.DialogResult;
            FormClosingEventArgs closingArgs = new FormClosingEventArgs(reason, false);
            dialog.OnFormClosing(closingArgs);
            if (closingArgs.Cancel)
                return false;
            FormClosedEventArgs closedArgs = new FormClosedEventArgs(reason);
            dialog.OnFormClosed(closedArgs);
            dialog.ActiveInWeb = false;
            return true;
        }

        private string GetButtonHtml(ButtonControl control)
        {
            return $"<input style=\"{GetButtonStyle(control)}\"" +
                $" type=\"button\" name=\"{control.Name}\"" +
                $" value=\"{control.Text}\" onclick=\"{GetEvent(ONCLICK, control, REALOAD)}\"" +
                $" title=\"{control.Text}\" {(control.Enabled ? "" : "disabled")}/>";
        }

        private string GetButtonStyle(ButtonControl control)
        {
            return $"{GetStandardStyle(control)} {GetControlAlign(control)} padding:0;margin:0;";
        }

    }
}
