using System;
using System.Drawing;
using System.Windows.Forms;
using FastReport.Utils;

namespace FastReport.Forms
{
    /// <summary>
    /// Base class for all forms. 
    /// </summary>
    public partial class BaseForm : Form
    {
        private int oldDpi;

        protected int NewDpi { get; private set; }

        /// <summary>
        /// The event occurs on form's dpi change.
        /// </summary>
        public event EventHandler DpiChanged;

        #region Dpi convenience methods
        /// <summary>
        /// Convenience method returns an image with specified index for this form's dpi.
        /// </summary>
        /// <param name="index">Image index.</param>
        /// <returns>The image.</returns>
        public Bitmap GetImage(int index)
        {
            return Res.GetImage(index, this.Dpi());
        }

        /// <summary>
        /// Convenience method returns an image with specified name for this form's dpi.
        /// </summary>
        /// <param name="resource">Image resource name.</param>
        /// <returns>The image.</returns>
        public Bitmap GetImage(string resource)
        {
            return Res.GetImage(resource, this.Dpi());
        }

        /// <summary>
        /// Convenience method returns an imagelist for this form's dpi.
        /// </summary>
        /// <returns>The imagelist.</returns>
        public ImageList GetImages()
        {
            return Res.GetImages(this.Dpi());
        }

        #endregion

        /// <summary>
        /// Localizes the dialog controls.
        /// </summary>
        /// <remarks>
        /// Use this method to set control's captions specific to the current locale.
        /// </remarks>
        public virtual void Localize()
        {
        }

#if !MONO
        /// <inheritdoc/>
        protected override void WndProc(ref Message m)
        {
            const int WM_DPICHANGED = 0x02E0;
            if (m.Msg == WM_DPICHANGED)
            {
                // these properties if set to non-zero will lead to weird scaling on dpi change.
                // if you need them set them in the UpdateDpiDependencies method.
                MinimumSize = new Size(0, 0);
                MaximumSize = new Size(0, 0);
            }

            base.WndProc(ref m);
            if (m.Msg == WM_DPICHANGED)
            {
                NewDpi = this.Dpi();
                if (NewDpi != oldDpi)
                {
                    UpdateDpiDependencies();
                    if (DpiChanged != null)
                        DpiChanged(this, EventArgs.Empty);
                    oldDpi = NewDpi;
                }
            }
        }
#endif

        private void FixCheckBoxesAndRadioButtons(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                ButtonBase b = c as ButtonBase;
                if (b != null && b.AutoSize)
                {
                    // it fixes the problem with AutoSize set incorrectly during autoscale
                    if (b.Text != "" && !b.Text.EndsWith("  "))
                        b.Text += "  ";
                    b.Width++;
                    b.Width--;
                }
                FixCheckBoxesAndRadioButtons(c);
            }
        }

        /// <summary>
        /// Update controls on dpi change. 
        /// </summary>
        /// <remarks>This method is called when the form's dpi is changed. Write custom logic to update
        /// some controls (such as ListBox.ItemHeight) here.
        /// </remarks>
        public virtual void UpdateDpiDependencies()
        {
#if !MONO
            // Checkboxes and radiobuttons need special care. Sometimes they are not scaled properly
            // when a form moved to another monitor.
            FixCheckBoxesAndRadioButtons(this);
#endif
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseForm"/> class.
        /// </summary>
        public BaseForm()
        {
            InitializeComponent();
            this.Font = DrawUtils.DefaultFont;
        }
    }
}