using Telerik.WinControls;

namespace Pilz.UI.Telerik.Dialogs;

public partial class RadFlyoutBase : UserControl
{
    public static RadSvgImage? CancelSvg { get; set; } = null;
    public static RadSvgImage? ConfirmSvg { get; set; } = null;

    public DialogResult Result { get; protected set; }
    public bool RegisterDialogAccept { get; set; } = true;
    public bool RegisterDialogCancel { get; set; } = false;

    protected bool ActionPanelVisible
    {
        get => tableLayoutPanel_ActionPanel.Visible;
        set => tableLayoutPanel_ActionPanel.Visible = value;
    }

    protected bool CancelButtonVisible
    {
        get => radButton_Cancel.Visible;
        set => radButton_Cancel.Visible = value;
    }

    protected bool CancelButtonEnable
    {
        get => radButton_Cancel.Enabled;
        set => radButton_Cancel.Enabled = value;
    }

    protected bool ConfirmButtonEnable
    {
        get => radButton_Confirm.Enabled;
        set => radButton_Confirm.Enabled = value;
    }

    public string Title
    {
        get => radLabel_Title.Text;
        set
        {
            radLabel_Title.Text = value;
            SetShowTitlePanel();
        }
    }

    public RadSvgImage TitleIcon
    {
        get => radLabel_Title.SvgImage;
        set
        {
            radLabel_Title.SvgImage = value;
            SetShowTitlePanel();
        }
    }

    protected RadFlyoutBase()
    {
        InitializeComponent();
        ParentChanged += FlyoutDialogBase_ParentChanged;

        // Change TabIndex to a very high value
        // -> prevent conflicts with controls based on this class
        radButton_Cancel.TabIndex = int.MaxValue - 1;
        radButton_Cancel.TabIndex = int.MaxValue;

        // SVG Symbols
        radButton_Cancel.SvgImage = CancelSvg;
        radButton_Confirm.SvgImage = ConfirmSvg;

        // Hide bars in Designer
        //if (DesignMode)
        //{
        //    tableLayoutPanel_ActionPanel.Visible = false;
        //    tableLayoutPanel_TitlePanel.Visible = false;
        //}
    }

    protected virtual void FlyoutDialogBase_ParentChanged(object? sender, EventArgs e)
    {
        var frm = FindForm();

        if (frm != null)
        {
            if (RegisterDialogAccept)
                frm.AcceptButton = radButton_Confirm;

            if (RegisterDialogCancel)
                frm.CancelButton = radButton_Cancel;

            if (AutoSize)
            {
                frm.AutoSize = true;
                frm.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            }
        }
    }

    protected void Close(DialogResult result)
    {
        Result = result;

        if (FindForm() is RadDialogBase dialogForm)
            dialogForm.Close();
        else
            CloseFlyout();
    }

    protected virtual bool ValidateOK()
    {
        return true;
    }

    protected virtual void SetShowTitlePanel()
    {
        tableLayoutPanel_TitlePanel.Visible = !string.IsNullOrWhiteSpace(radLabel_Title.Text) || radLabel_Title.SvgImage != null;
    }

    protected virtual void RadButton_Confirm_Click(object sender, EventArgs e)
    {
        if (ValidateOK())
            Close(DialogResult.OK);
    }

    protected virtual void RadButton_Cancel_Click(object sender, EventArgs e)
    {
        Close(DialogResult.Cancel);
    }
}
