using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using BankTVUpdater.Consts;
using BankTVUpdater.Logger;

namespace BankTVUpdater.Helpers
{
    class ProcessHelper
    {

        private static Process GetProcessByName(string processName)
        {
            if (String.IsNullOrWhiteSpace(processName))
            {
                MyLogger.Log("Process name to find empty...");
                return null;
            }   //if
            try
            {
                foreach (Process process in Process.GetProcessesByName(processName))
                {
                    if (String.IsNullOrWhiteSpace(process.ProcessName))
                    {
                        continue;
                    }   //if
                    if (process.ProcessName.ToLower().Equals(processName.ToLower()))
                    {
                        MyLogger.Log(String.Format("Process found: {0}, PID: {1}", process.ProcessName, process.Id));
                        return process;
                    }   //if
                }   //for
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);                
            }   //catch
            MyLogger.Log(String.Format("Unable to find process: {0}", processName));
            return null;
        }

        private static bool KillProcess(string processName)
        {
            if (String.IsNullOrWhiteSpace(processName))
            {
                MyLogger.Log("Process name to kill empty...");
                return false;
            }   //if
            try
            {
                Process process = ProcessHelper.GetProcessByName(processName);
                if (process == null)
                {
                    MyLogger.Log(String.Format("Unable to find working process: {0}, assuming process killed...", processName));
                    return true;
                }   //if
                process.Kill();
                MyLogger.Log(String.Format("Process kiled: {0}", processName));
                return true;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            MyLogger.Log(String.Format("Unable to kill process: {0}", processName));
            return false;
        }

        private static bool StartProcess(string processLocation)
        {
            if (String.IsNullOrWhiteSpace(processLocation))
            {
                MyLogger.Log("Process name to start empty...");
                return false;
            }   //if
            if (!File.Exists(processLocation))
            {
                MyLogger.Log(String.Format("Unable to find application: {0}", processLocation));
                return false;
            }   //if
            try
            {
                Process.Start(processLocation);
                MyLogger.Log(String.Format("Application started: {0}", processLocation));
                return true;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            MyLogger.Log(String.Format("Unable to start application: {0}", processLocation));
            return false;
        }

        private static string GetProcessName()
        {
            string processName = GlobalHelper.GetValueFromAppConfig(GlobalConsts.APP_PARAM_PROCESS_NAME_KEY);
            if (String.IsNullOrWhiteSpace(processName))
            {
                return GlobalConsts.APP_PARAM_PROCESS_NAME_DEFAULT;
            }   //if
            return processName;
        }

        private static string GetProcessFullPath()
        {
            string processFullPath = GlobalHelper.GetValueFromAppConfig(GlobalConsts.APP_PARAM_PROCESS_LOCATION_KEY);
            if (String.IsNullOrWhiteSpace(processFullPath))
            {
                return GlobalConsts.APP_PARAM_PROCESS_LOCATION_DEFAULT;
            }   //if
            return processFullPath;
        }

        public static bool CleanApp()
        {
            try
            {
                MyLogger.Log("Cleaning app started...");
                string processName = ProcessHelper.GetProcessName();
                if (String.IsNullOrWhiteSpace(processName))
                {
                    MyLogger.Log("Process name unknown, cleaning app terminated...");
                    return false;
                }   //if
                string processLocation = ProcessHelper.GetProcessFullPath();
                if (String.IsNullOrWhiteSpace(processLocation))
                {
                    MyLogger.Log("Process full path unknown, cleaning app terminated...");
                    return false;
                }   //if
                if (!ProcessHelper.KillProcess(processName))
                {
                    MyLogger.Log(String.Format("Cannot kill process {0} , cleaning app terminated...", processName));
                    return false;
                }   //if
                if (!UpdateHelper.UpdateApp())
                {
                    MyLogger.Log("Unable to update app, cleaning app terminated...");
                    return false;
                }   //if
                //if (!UpdateHelper.UpdateApp())
                //{
                //    MyLogger.Log("Unable to update app, cleaning app terminated...");
                //    return false;
                //}   //if
                if (!ProcessHelper.StartProcess(processLocation))
                {
                    MyLogger.Log(String.Format("Unable to start process {0} , cleaning app terminated...", processLocation));
                    return false;
                }   //if
                MyLogger.Log("Cleaning app finished ok...");
                return true;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }
    }

}
