using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using Ionic.Zip;
using System.IO;
using FTD2XX_NET;
using IniParser;

namespace Nastavení_PAO
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string RuntimeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

            if (File.Exists(RuntimeDir + "\\" + "scheme.zip") && File.Exists(RuntimeDir + "\\" + "Ionic.Zip.dll") && File.Exists(RuntimeDir + "\\" + "INIFileParser.xml") && File.Exists(RuntimeDir + "\\" + "INIFileParser.dll") && File.Exists(RuntimeDir + "\\" + "FTD2XX_NET.dll"))
            {

                try
                {
                    ZipFile z = ZipFile.Read(RuntimeDir + "\\" + "scheme.zip");

                    if (z.ContainsEntry("slv_p2x.png") && z.ContainsEntry("slv_p2.png") && z.ContainsEntry("slv_p1x.png") && z.ContainsEntry("slv_p1.png") && z.ContainsEntry("scheme.vsd") && z.ContainsEntry("scheme.png") && z.ContainsEntry("out_ovz.png") && z.ContainsEntry("out_ovo.png") && z.ContainsEntry("out_ohz.png") && z.ContainsEntry("out_oho.png") && z.ContainsEntry("mux_svz.png") && z.ContainsEntry("mux_svo.png") && z.ContainsEntry("mux_shz.png") && z.ContainsEntry("mux_sho.png"))
                    {

                        z.Dispose();
                       try
                       {
                            Application.Run(new Form1());
                       }
                              
                       catch (Exception e) {
                      
                                   TextWriter t = File.CreateText("log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
                                   t.WriteLine("______________________________1______________________________________");
                                   t.WriteLine("Zdroj: " + e.Source.ToString());
                                   t.WriteLine("Metoda: "+e.TargetSite.ToString());
                                   t.WriteLine("Zpráva: "+e.Message.ToString());
                                   t.WriteLine("Zásobník: "+e.StackTrace.ToString());
                                   t.WriteLine("Data: "+e.Data.ToString());
                                   t.WriteLine("_____________________________________________________________________");

                                   
      
                                   t.Close();
            
                                  DialogResult x = MessageBox.Show("V průběhu programu se vyskytla neočekávaná chyba, detaily v souboru log.txt.", "Chyba programu", MessageBoxButtons.OK, MessageBoxIcon.Error);

                                    if ((x == DialogResult.OK) || (x == DialogResult.None))
                                    {

                                        Application.ExitThread();

                                    }
                        
                               }
                        
                    }
                    else
                    {

                        z.Dispose();

                        DialogResult x = MessageBox.Show("Program nemohl být spuštěn, protože archiv fragmentů pro renderer je poškozený.", "Chyba programu", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        if ((x == DialogResult.OK) || (x == DialogResult.None))
                        {

                            Application.ExitThread();

                        }


                    }

                }
                catch (ZipException e) {

                    DialogResult x = MessageBox.Show(e.Message, "Chyba programu", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if ((x == DialogResult.OK) || (x == DialogResult.None))
                    {

                        Application.ExitThread();

                    }


                }
                catch (Exception e)
                {

                    DialogResult x = MessageBox.Show(e.Message, "Chyba programu", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if ((x == DialogResult.OK) || (x == DialogResult.None))
                    {

                        Application.ExitThread();

                    }


                }

                

                

            }
            else {

                DialogResult x = MessageBox.Show("Program nemohl být spuštěn, protože nebyla nalezena některá jeho část:" + Environment.NewLine + Environment.NewLine + "scheme.zip" + Environment.NewLine + "Ionic.Zip.dll" + Environment.NewLine + "FTD2XX_NET.dll" + Environment.NewLine + "INIFileParser.xml" + Environment.NewLine + "INIFileParser.dll", "Chyba programu", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if ((x == DialogResult.OK) || (x == DialogResult.None))
                {

                Application.ExitThread();
                
                }
            }
        }
    }
}
