using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Foo.Platform.Win32;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;

namespace Foo.WorkspaceMgr.Launchers
{
    internal class Launcher_Excel : ILaunch
    {
        #region ILaunch Members

        /// <summary>
        /// Checks to see if the file exists in the system i.e. can be reached over
        /// the network. if FileExists fails, we shouldn't be able to launch it.
        /// </summary>
        /// <param name="strArtifactLocation">location of the path + file to launch</param>
        /// <returns></returns>
        public FuncDepBoolType IQueryLaunch(string strArtifactLocation)
        {
            if (!String.IsNullOrEmpty(strArtifactLocation) && (strArtifactLocation.Length > 3))
            {
                if (File.Exists(strArtifactLocation))
                    return FuncDepBoolType.Success;
                else
                    return FuncDepBoolType.ArtifactLocationUnavailable;
            }
            else
            {
                return FuncDepBoolType.ParametersInvalid;
            }
        }

        public const string Excel_ProgId = "Excel.Application";

        /// <summary>        
        /// Generic Launcher should be able to handle any file type. Launched the .Net Way.
        /// ~this is the same as if the User clicks on the file.
        /// </summary>
        /// <param name="strArtifactLocation">location of the path + file to launch</param>
        /// <returns></returns>
        public FuncDepBoolType ILaunch(string strArtifactLocation)
        {
            Microsoft.Office.Interop.Excel.Application app = null;
            app = new Microsoft.Office.Interop.Excel.Application();

            // Mark the Application as visible
            app.Visible = true;

            app.Workbooks.Open(strArtifactLocation, Missing.Value, Missing.Value, Missing.Value, Missing.Value, 
                                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, 
                                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value );

            // Keep Track of all our Excel Instances
            ///WorkspaceState.Launched_ExcelInstances.Add(app);

            return FuncDepBoolType.Success;

            //if (!String.IsNullOrEmpty(strArtifactLocation) && (strArtifactLocation.Length > 3))
            //{                
            //    Process.Start(strArtifactLocation);                               
            //}
            //return FuncDepBoolType.ParametersInvalid;            
        }

        #endregion




        

        ///// <summary>
        ///// To Do : Test This -  Let's us know if the user has unsaved data
        ///// </summary>
        ///// <param name="strArtifactLocation"></param>
        ///// <returns></returns>
        //public FuncDepBoolType QueryClose_MSExcel(string strArtifactLocation)
        //{
        //    try
        //    {
               

        //        foreach (Microsoft.Office.Interop.Excel.Workbook book in app.Workbooks)
        //        {
        //            if (book.FullName.ToLower() == strArtifactLocation.ToLower())
        //            {
        //                if (book.Saved)
        //                    return FuncDepBoolType.Success;
        //                else
        //                    return FuncDepBoolType.Failed;
        //            }
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        string message = e.Message;
        //        message = message + "";
        //        // TODO: Log Something here
        //        return FuncDepBoolType.ErrorThrown;
        //    }

        //    return FuncDepBoolType.Failed;
        //}

        ///// <summary>
        ///// *** Technically ** Here we want to close the Excel Workbook (Not Prompt the user at all)
        ///// **** we could close it with a WM_CLOSE or WM_SCLOSE or with the DOM.  ~for now it's the
        ///// DOM, however more testing to be done here
        ///// </summary>
        ///// <param name="strArtifactLocation"></param>
        ///// <returns></returns>
        //public FuncDepBoolType Close_MSExcel(string strArtifactLocation)
        //{
        //    try
        //    {
        //        Microsoft.Office.Interop.Excel.Application app = null;
        //        app = Win32Functions.GetCOMObject(Excel_ProgId) as Microsoft.Office.Interop.Excel.Application;

        //        // For Debugging
        //        //int nCount = app.Workbooks.Count;

        //        foreach (Microsoft.Office.Interop.Excel.Workbook book in app.Workbooks)
        //        {
        //            if (book.FullName.ToLower() == strArtifactLocation.ToLower())
        //            {
        //                book.Close(true, strArtifactLocation, false);

        //                // Close Excel if this is the last Workbook
        //                if (app.Workbooks.Count == 0)
        //                    app.Quit();

        //                return FuncDepBoolType.Success;
        //            }
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        string message = e.Message;
        //        message = message + "";
        //        // TODO: Log Something here
        //        return FuncDepBoolType.ErrorThrown;
        //    }

        //    return FuncDepBoolType.Failed;
        //}


    }
}
