using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SigmaERP
{
    static class Program
    {
        /// <summary>
        /// Punto de entrada principal para la aplicación.
        /// </summary>
        /*[STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new P_Principal(""));
        }*/

        /// <summary>
        /// Punto de entrada principal para la aplicación.
        /// </summary>
        static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}"); 
        [STAThread]
        static void Main(string[] _arr_Argumentos)
        {
            if (_arr_Argumentos==null || _arr_Argumentos.Count()==0 || mutex.WaitOne(TimeSpan.Zero, true))
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new P_Principal(_arr_Argumentos));
                mutex.ReleaseMutex();
            }
            else
            {
                try
                {
                    //Argumentos
                    if (_arr_Argumentos != null && _arr_Argumentos.Count() > 0)
                    {
                        //Grabar Fichero con los datos encripados de los argumentos
                        string str_RutaTemporal = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\GT.txt";

                        if (File.Exists(str_RutaTemporal))
                        {
                            File.Delete(str_RutaTemporal);
                        }

                        File.AppendAllText(str_RutaTemporal,_arr_Argumentos[0]);
                    }

                    // send our Win32 message to make the currently running instance 
                    // jump on top of all the other windows 
                    NativeMethods.PostMessage(
                                (IntPtr)NativeMethods.HWND_BROADCAST,
                                NativeMethods.WM_SHOWME,
                                IntPtr.Zero,
                                IntPtr.Zero);
                }
                catch 
                {}
            }
        }
    }
}
