// For GUIDebugStepExe Debugging Only - Uncomment this #define
#define GUIDEBUGSTEPEXE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.EnterpriseServices;
using System.Reflection;

using Foo.Platform;
using Foo.Platform.Win32;
using System.Windows;

namespace Foo.ClientServices.GUIWPForms
{
    /// <summary>
    /// GUIWPForms - Com Callable Wrapper Class exposed to ButtonHook.
    /// This class is responsible for creating the ButtonWPForm (a form created in response to a ButtonHook W32 Click)
    /// </summary>    
    [Guid("481C24F8-D619-460b-955A-22055571430C")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Foo.ClientServices.GUIWPForms")]
    [ComVisible(true)]
    [ObjectPooling(Enabled = true, MinPoolSize = 1, MaxPoolSize = 2500, CreationTimeout = 5000)]
#if GUIDEBUGSTEPEXE
    public class GUIWPForms : IGUIPWPForms
#else
    public class GUIWPForms : ServicedComponent, IProcessInitializer, IGUIPWPForms
#endif
    {
        // Declare the Log4net Variable
        private static log4net.ILog Log = Logger.GetLog4NetInterface(MethodBase.GetCurrentMethod().DeclaringType);

        #region Construction / Destruction
        public GUIWPForms() {}
        ~GUIWPForms() { }
        #endregion

#if GUIDEBUGSTEPEXE
        #region GuiDebugStepCustomStarterStopers
        public void Start()
        {
            Log.Info(string.Format("{0}() - ComponentState Application Is being started", MethodBase.GetCurrentMethod().Name));
            ComponentState.ApplicationIsRunning = true;
        }

        public void Stop()
        {
            Log.Info(string.Format("{0}() - ComponentState Application Is being stopped", MethodBase.GetCurrentMethod().Name));
            ComponentState.ApplicationIsRunning = false;
        }
        #endregion
#else
        #region IProcessInitializer Members
        public void Startup(object punkProcessControl)
        {
            Log.Info(string.Format("{0}() - ComponentState Application Is being started", MethodBase.GetCurrentMethod().Name));
            ComponentState.ApplicationIsRunning = true;
        }

        public void Shutdown()
        {
            Log.Info(string.Format("{0}() - ComponentState Application Is being stopped", MethodBase.GetCurrentMethod().Name));
            ComponentState.ApplicationIsRunning = false;
        }
        #endregion
#endif

        #region IGUIPWPForms Members

        /// <summary>
        /// Launches the ArtifactWall WPForm (if one already exists, relaunches)
        /// </summary>
        /// <param name="hDeskbandButtonWnd">handle to the Button on the Deskband (used for positioning)</param>
        /// <returns>true if successful, false otherwise</returns>
        public bool LaunchArtifactWall(IntPtr hDeskbandButtonWnd)
        {
            try
            {
                RECT rect;
                bool bFillRect = false;
                if (hDeskbandButtonWnd != IntPtr.Zero)
                    bFillRect = Win32Functions.GetWindowRect(hDeskbandButtonWnd, out rect);

                // First Step - Create the WpfForm
                ComponentState.GUIFormsMgr.Create_WpfFormDISP(WPForms.ArtifactWall);

                // Second Step - If we have a Rect, set the location
                if (bFillRect)
                {
                    //GUIFormsMgr._Action action = new GUIFormsMgr._Action(delegate(Window wpfForm)
                    //{
                    //    wpfForm.Height = nHeight;
                    //    wpfForm.Width = nWidth;
                    //});
                    //ComponentState.GUIFormsMgr.RunAction_WpfFormDISP(WPForms.ArtifactWall, action);
                }
               
                // Third Step - Show the WpfForm
                ComponentState.GUIFormsMgr.Show_WpfFormDISP(WPForms.ArtifactWall);
            }
            catch (Exception e)
            {
                Log.Error(string.Format("{0}() - Error Occured", MethodBase.GetCurrentMethod().Name), e);
            }
            return true;
        }

        /// <summary>
        /// Closes the ArtifactWall if exists
        /// </summary>        
        /// <returns>true if closed, false if not existent</returns>
        public bool CloseArtifactWall()
        {           
            try
            {
                return ComponentState.GUIFormsMgr.Delete_WpfFormDISP(WPForms.ArtifactWall);
            }
            catch (Exception e)
            {
                Log.Error(string.Format("{0}() - Error Occured", MethodBase.GetCurrentMethod().Name), e);
            }
            return false;            
        }

        /// <summary>
        /// Launches the WorkspaceSelector WPForm (if one already exists, relaunches)
        /// </summary>
        /// <param name="hDeskbandButtonWnd">handle to the Button on the Deskband (used for positioning)</param>
        /// <returns>true if successful, false otherwise</returns>
        public bool LaunchWorkspaceSelector(IntPtr hDeskbandButtonWnd)
        {
            try
            {
                RECT rect = new RECT();
                rect.top = 0;
                rect.left = 0;
                bool bSuccess = false;
                bool bFillRect = false;
                if (hDeskbandButtonWnd != IntPtr.Zero)
                    bFillRect = Win32Functions.GetWindowRect(hDeskbandButtonWnd, out rect);

                // First Step - Create the WpfForm
                bSuccess = ComponentState.GUIFormsMgr.Create_WpfFormDISP(WPForms.WorkspaceSelector);
                if (!bSuccess)
                    return false;

                // Second Step - If we have a Rect, set the location of the Workspace Selector
                if (bFillRect)
                {
                    GUIFormsMgr._Action action = new GUIFormsMgr._Action(delegate(Window wpfForm)
                    {
                        wpfForm.Top = rect.top + rect.AsRectangle.Height + 2;
                        wpfForm.Left = rect.left - 200;
                    });
                   bSuccess = ComponentState.GUIFormsMgr.RunAction_WpfFormDISP(WPForms.WorkspaceSelector, action);
                   if (!bSuccess)
                       return false;
                }

                // Third Step - Show the WpfForm
                bSuccess = ComponentState.GUIFormsMgr.Show_WpfFormDISP(WPForms.WorkspaceSelector);
                if (!bSuccess)
                    return false;
            }
            catch (Exception e)
            {
                Log.Error(string.Format("{0}() - Error Occured", MethodBase.GetCurrentMethod().Name), e);
                return false;
            }            
            return true;
        }

        /// <summary>
        /// Closes the WorkspaceSelector if exists
        /// </summary>
        /// <param name="hDeskbandButtonWnd"></param>
        /// <returns>true if closed, false if not existent</returns>
        public bool CloseWorkspaceSelector()
        {
            try
            {
                return ComponentState.GUIFormsMgr.Delete_WpfFormDISP(WPForms.WorkspaceSelector);
            }
            catch (Exception e)
            {
                Log.Error(string.Format("{0}() - Error Occured", MethodBase.GetCurrentMethod().Name), e);                
            }
            return false;
        }

        /// <summary>
        /// Launches the Settings and Abouts WPForm (if one already exists, relaunches)
        /// </summary>
        /// <param name="hDeskbandButtonWnd">handle to the Button on the Deskband (used for positioning)</param>
        /// <returns>true if successful, false otherwise</returns>
        public bool LaunchSettingsNAboutUs(IntPtr hDeskbandButtonWnd)
        {
            try
            {
                // First Step - Create the WpfForm
                ComponentState.GUIFormsMgr.Create_WpfFormDISP(WPForms.SettingsNAboutUs);

                // Second Step - Show the WpfForm
                ComponentState.GUIFormsMgr.Show_WpfFormDISP(WPForms.SettingsNAboutUs);
            }
            catch (Exception e)
            {
                Log.Error(string.Format("{0}() - Error Occured", MethodBase.GetCurrentMethod().Name), e);
            }
            return true;
        }

        /// <summary>
        /// Closes the Settings And About us
        /// </summary>        
        /// <returns>true if closed, false if not existent</returns>
        public bool CloseSettingsNAboutUs()
        {
            try
            {
                return ComponentState.GUIFormsMgr.Delete_WpfFormDISP(WPForms.WorkspaceSelector);
            }
            catch (Exception e)
            {
                Log.Error(string.Format("{0}() - Error Occured", MethodBase.GetCurrentMethod().Name), e);
            }
            return false;
        }

        #endregion
    }
}
