using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace Pilz.UI.Dialogs;

public partial class FlyoutBase : UserControl
{
    private bool addedControlsToUi;

    protected TableLayoutPanel tableLayoutPanel_TitlePanel;
    protected Label label_Title;
    protected TableLayoutPanel tableLayoutPanel_ActionPanel;
    protected Button button_Cancel;
    protected Button button_Accept;

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

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

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

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

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

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

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

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

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

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

    public FlyoutBase()
    {
        InitializeComponent();
        ParentChanged += FlyoutBase_ParentChanged;
    }

    private void InitializeComponent()
    {
        // button_Accept
        button_Accept = new Button
        {
            Name = "button_Accept",
            Size = new Size(75, 23),
            TabIndex = int.MaxValue - 1,
            Text = "Accept",
            UseVisualStyleBackColor = true
        };
        button_Accept.Click += Button_Accept_Click;

        // button_Cancel
        button_Cancel = new Button
        {
            Name = "button_Cancel",
            Size = new Size(75, 23),
            TabIndex = int.MaxValue,
            Text = "Cancel",
            UseVisualStyleBackColor = true
        };
        button_Cancel.Click += Button_Cancel_Click;

        // label_Title
        label_Title = new Label
        {
            Name = "label_Title",
            Dock = DockStyle.Fill,
            TextAlign = ContentAlignment.MiddleLeft
        };

        // tableLayoutPanel_TitlePanel
        tableLayoutPanel_TitlePanel = new TableLayoutPanel
        {
            Name = "tableLayoutPanel_TitlePanel",
            Dock = DockStyle.Top,
            Size = new Size(ClientSize.Width, 29),
            Visible = false,
            ColumnCount = 1,
            RowCount = 1
        };
        tableLayoutPanel_TitlePanel.SuspendLayout();
        tableLayoutPanel_TitlePanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
        tableLayoutPanel_TitlePanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
        tableLayoutPanel_TitlePanel.Controls.Add(label_Title, 0, 0);

        // tableLayoutPanel_ActionPanel
        tableLayoutPanel_ActionPanel = new TableLayoutPanel
        {
            Name = "tableLayoutPanel_ActionPanel",
            AutoSize = true,
            AutoSizeMode = AutoSizeMode.GrowAndShrink,
            Dock = DockStyle.Bottom,
            Size = new Size(ClientSize.Width, 29),
            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());
        tableLayoutPanel_ActionPanel.Controls.Add(button_Cancel, 2, 0);
        tableLayoutPanel_ActionPanel.Controls.Add(button_Accept, 1, 0);
        
        // FlyoutBase
        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();
            addedControlsToUi = true;
        }
        base.OnLoad(e);
    }

    private void FlyoutBase_ParentChanged(object sender, EventArgs e)
    {
        var frm = FindForm();
        if (frm != null)
        {
            if (RegisterDialogAccept)
                frm.AcceptButton = button_Accept;

            if (RegisterDialogCancel)
            {
                frm.CancelButton = button_Cancel;
                button_Cancel.DialogResult = DialogResult.None;
            }
        }
    }

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

        if (FindForm() is DialogBase dialogForm)
            dialogForm.Close();
    }

    protected virtual bool ValidateOK()
    {
        return true;
    }

    protected virtual void SetShowTitlePanel()
    {
        tableLayoutPanel_TitlePanel.Visible = !string.IsNullOrWhiteSpace(label_Title.Text);
    }

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

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