// ApplicationIcon.cs
// Copyright (c) 2016 Bentley Systems, Incorporated. All Rights Reserved.

using System;
using System.Drawing;
using System.Windows.Forms;
using Haestad.Support.Win32Api;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class embodies an application icon that appears on a form title bar.
    /// </summary>
    internal class ApplicationIcon : PictureBox
    {
        #region Constructor
        internal ApplicationIcon(Image image, ISystemMenuOpener systemMenuOpener, Form parentForm)
        {
            Image = image;
            // Note: <whar> If we don't stretch to available size, does not scale on higher DPI and looks wrong.
            SizeMode = PictureBoxSizeMode.StretchImage;
            SystemMenuOpener = systemMenuOpener;
            ParentForm = parentForm;

            MouseDown += ApplicationIcon_MouseDown;

            // Note: <whar> Important: To allow keyboard navigation of the ribbon (prevents the keyboard giving focus to this window).
            TabStop = false;
        }
        #endregion

        #region Event Handlers
        private void ApplicationIcon_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                const int CLICKS_TO_CLOSE = 2;
                if (e.Clicks >= CLICKS_TO_CLOSE)
                    ParentForm.Close();
                else
                    SystemMenuOpener.OpenSystemMenu(PointToScreen(new Point(Left - EnvironmentLibrary.ApplicationIconButtonPadding, Top + Height)));
            }
            else if (e.Button == MouseButtons.Right)
                SystemMenuOpener.OpenSystemMenu();

        }
        #endregion

        #region Private Properties
        private ISystemMenuOpener SystemMenuOpener
        {
            get;
            set;
        }

        private Form ParentForm
        {
            get;
            set;
        }
        #endregion
    }
}
