using Pilz.UI.Telerik.Controls.RadValidationProvider;
using System.ComponentModel;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;

namespace Pilz.UI.Telerik.Dialogs;

public partial class RadFlyoutBase : UserControl
{
    private bool addedControlsToUi;

    protected RadButton radButton_Cancel;
    protected RadButton radButton_Confirm;
    protected TableLayoutPanel tableLayoutPanel_ActionPanel;
    protected TableLayoutPanel tableLayoutPanel_TitlePanel;
    protected RadLabel radLabel_Title;

    public static RadSvgImage? CancelSvg { get; set; } = null;
    public static RadSvgImage? ConfirmSvg { get; set; } = null;

    [Browsable(false)]
    public RadValidationProviderEx ValidationProvider { get; } = new();

    [ReadOnly(true)]
    public DialogResult Result { get; protected set; }

    [DefaultValue(true)]
    public bool RegisterDialogAccept { get; set; } = true;

    [DefaultValue(true)]
    public bool RegisterDialogCancel { get; set; } = true;

    public new Size PreferredSize { get; set; } = Size.Empty;

    [DefaultValue(true)]
    public virtual bool ActionPanelVisible
    {
        get => tableLayoutPanel_ActionPanel.Visible;
        set => tableLayoutPanel_ActionPanel.Visible = value;
    }

    [DefaultValue(true)]
    public virtual bool CancelButtonVisible
    {
        get => radButton_Cancel.Visible;
        set => radButton_Cancel.Visible = value;
    }

    [DefaultValue(true)]
    public virtual bool CancelButtonEnable
    {
        get => radButton_Cancel.Enabled;
        set => radButton_Cancel.Enabled = value;
    }

    [DefaultValue(true)]
    public virtual bool ConfirmButtonEnable
    {
        get => radButton_Confirm.Enabled;
        set => radButton_Confirm.Enabled = value;
    }

    [Localizable(true)]
    [DefaultValue("Okay")]
    public virtual string ConfirmButtonText
    {
        get => radButton_Confirm.Text;
        set => radButton_Confirm.Text = value;
    }

    [Localizable(true)]
    [DefaultValue("Cancel")]
    public virtual string CancelButtonText
    {
        get => radButton_Cancel.Text;
        set => radButton_Cancel.Text = value;
    }

    [Localizable(true)]
    [DefaultValue("")]
    public virtual string Title
    {
        get => radLabel_Title.Text;
        set
        {
            radLabel_Title.Text = value;
            SetShowTitlePanel();
        }
    }

    [DefaultValue(null)]
    public virtual RadSvgImage TitleIcon
    {
        get => radLabel_Title.SvgImage;
        set
        {
            radLabel_Title.SvgImage = value;
            SetShowTitlePanel();
        }
    }

    [DefaultValue(typeof(ValidationMode), "OnValidating")]
    public ValidationMode ValidationMode
    {
        get => ValidationProvider.ValidationMode;
        set => ValidationProvider.ValidationMode = value;
    }

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

    private void InitializeComponent()
    {
        // radButton_Cancel
        radButton_Cancel = new()
        {
            Name = "radButton_Cancel",
            Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
            ImageAlignment = ContentAlignment.MiddleRight,
            Size = new Size(110, 24),
            TabIndex = 1,
            Text = "Cancel",
            TextAlignment = ContentAlignment.MiddleLeft,
            TextImageRelation = TextImageRelation.ImageBeforeText,
            SvgImage = CancelSvg
        };
        radButton_Cancel.TabIndex = int.MaxValue;
        radButton_Cancel.Click += RadButton_Cancel_Click;

        // radButton_Confirm
        radButton_Confirm = new()
        {
            Name = "radButton_Confirm",
            Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
            ImageAlignment = ContentAlignment.MiddleRight,
            Size = new Size(110, 24),
            TabIndex = 0,
            Text = "Okay",
            TextAlignment = ContentAlignment.MiddleLeft,
            TextImageRelation = TextImageRelation.ImageBeforeText,
            SvgImage = ConfirmSvg
        };
        radButton_Confirm.TabIndex = int.MaxValue - 1;
        radButton_Confirm.Click += RadButton_Confirm_Click;

        // radLabel_Title
        radLabel_Title = new()
        {
            Name = "radLabel_Title",
            AutoSize = false,
            Dock = DockStyle.Fill,
            TabStop = false,
            TextImageRelation = TextImageRelation.ImageBeforeText
        };

        // tableLayoutPanel_ActionPanel
        tableLayoutPanel_ActionPanel = new()
        {
            Name = "tableLayoutPanel_ActionPanel",
            Dock = DockStyle.Bottom,
            Size = new Size(ClientSize.Width, 30),
            TabStop = false,
            ColumnCount = 3,
            RowCount = 1,
        };
        tableLayoutPanel_ActionPanel.SuspendLayout();
        tableLayoutPanel_ActionPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
        tableLayoutPanel_ActionPanel.ColumnStyles.Add(new ColumnStyle());
        tableLayoutPanel_ActionPanel.ColumnStyles.Add(new ColumnStyle());
        tableLayoutPanel_ActionPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
        tableLayoutPanel_ActionPanel.Controls.Add(radButton_Confirm, 1, 0);
        tableLayoutPanel_ActionPanel.Controls.Add(radButton_Cancel, 2, 0);

        // tableLayoutPanel_TitlePanel
        tableLayoutPanel_TitlePanel = new()
        {
            Name = "tableLayoutPanel_TitlePanel",
            Dock = DockStyle.Top,
            TabStop = false,
            Visible = false,
            ColumnCount = 1,
            RowCount = 1
        };
        tableLayoutPanel_TitlePanel.SuspendLayout();
        tableLayoutPanel_TitlePanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
        tableLayoutPanel_TitlePanel.Controls.Add(radLabel_Title, 0, 0);
        tableLayoutPanel_TitlePanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
        tableLayoutPanel_TitlePanel.Size = new Size(300, 30);

        // RadFlyoutBase
        AutoScaleDimensions = new SizeF(7F, 15F);
        AutoScaleMode = AutoScaleMode.Font;
        Size = new Size(300, 150);

        tableLayoutPanel_TitlePanel.ResumeLayout(false);
        tableLayoutPanel_ActionPanel.ResumeLayout(false);
    }

    protected override void OnLoad(EventArgs e)
    {
        if (!addedControlsToUi && !DesignMode)
        {
            SuspendLayout();
            Controls.Add(tableLayoutPanel_ActionPanel);
            tableLayoutPanel_ActionPanel.SendToBack();
            Controls.Add(tableLayoutPanel_TitlePanel);
            tableLayoutPanel_TitlePanel.SendToBack();
            ResumeLayout(false);
            PerformLayout();
            SetShowTitlePanel();
            addedControlsToUi = true;
        }
        base.OnLoad(e);
    }

    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;
                radButton_Cancel.DialogResult = DialogResult.None;
            }

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

            frm.Shown += Form_Shown;
        }
    }

    private void Form_Shown(object? sender, EventArgs e)
    {
        if (FindForm() is not Form frm)
            return;

        if (!AutoSize && !PreferredSize.IsEmpty)
            frm.ClientSize = PreferredSize;
    }

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

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

    protected virtual bool ValidateOK()
    {
        return ValidationProvider.ValidateAll();
    }

    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);
    }
}
