using System;
using System.Diagnostics;
using System.IO;
using FirstFloor.ModernUI.Windows.Controls;
using System.Windows.Controls;

namespace HistoryWallEditTool
{
    public partial class HistoryWall : UserControl
    {
        #region Launcher Function
        public Exception OpenMainProgram(string exePath)
        {
            if (File.Exists(exePath))
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = exePath;
                try
                {
                    Process startProcess = Process.Start(startInfo);
                    return null;
                }
                catch (Exception ex)
                {
                    return ex;
                }
            }
            else
            {
                return new Exception("找不到前台執行程式~所以關不掉");
            }
        }

        private Exception CloseProcess(string processName)
        {
            try
            {
                bool isKill = false;
                System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
                foreach (System.Diagnostics.Process myProcess in myProcesses)
                {
                    if (myProcess.ProcessName == processName)
                    {
                        Debug.WriteLine(myProcess.ProcessName + " " + myProcess.MainWindowHandle.ToString());
                        myProcess.Kill();
                        isKill = true;
                    }
                }
                if (isKill)
                {
                    return null;
                }
                else
                {
                    return new Exception("Cannot find " + processName);
                }
            }
            catch (Exception ex)
            {
                return ex;
            }
        }
        #endregion
    }
}
