using Microsoft.Win32;
using System;
using System.IO;
using System.Windows.Forms;
using System;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO.Compression;

namespace QGIS_Installer
{
    public partial class Form1 : Form
    {
        List<RegistryKey> list = new List<RegistryKey>();
        RegistryKey key3_4;
        RegistryKey key3_0;
        RegistryKey key3_6;
        RegistryKey key3_10;
        RegistryKey key3_8;
        RegistryKey key3_12;
        public Form1()
        {
            InitializeComponent();
        }

       
        private void copyFiles(string SourcePath, string DestinationPath)
        {
            //  string dest = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //   string SourcePath = "C:\\Users\\Honza\\Documents\\testcopy\\folder1";
            //  string DestinationPath = "C:\\Users\\Honza\\Documents\\testcopy\\folder2";
            try
            {
                foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
                    SearchOption.AllDirectories))
                    Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

            //    Copy all the files &Replaces any files with the same name
            foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
                SearchOption.AllDirectories))
                    File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
            }
            catch (Exception ex)  //just for demonstration...it's always best to handle specific exceptions
            {
                //label1.ForeColor = System.Drawing.Color.Red;
                label1.Text = "You must run program as administrator!";
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            RegistryKey key ;
            String s = comboBox1.Text;
            if (s == "QGIS 3_4")
                key = key3_4;
            else if (s == "QGIS 3_10")
                key = key3_10;
            else if (s == "QGIS 3_0")
                key = key3_0;
            else if (s == "QGIS 3_8")
                key = key3_8;
            else if (s == "QGIS 3_12")
                key = key3_12;
            else
                key = null;
           
                label1.Text = "Installing";
            downloadPlugin(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
            //using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            //    //using (var key = hklm.OpenSubKey(@"Software\\QGIS 3.4"))
            //    foreach (RegistryKey key in list)
            //    {
                
                string o = key.GetValue("InstallPath").ToString();
             
                //o = o + "\\apps\\Python37\\Lib\\site-packages"
                string out1 = o + "\\apps\\Python37\\Lib\\site-packages";
                string out2 = o + "\\apps\\Python37\\Scripts\\flask.exe";
                string out3 = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas";
                string input = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\packages";
                copyFiles(input, out1.ToString());
                string input2 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\flask.exe";
                try
                {
                    File.Copy(input2, out2);
                }
                catch (Exception ex)
                {
                    label1.ForeColor = System.Drawing.Color.Red;
                    label1.Text = "You must run program as administrator!";
                }
                if (isPluginInstalled(o))
                    {
                  
                       
                    string input3 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\atlas";
                    copyFiles(input3, out3);
                }
                else
                {
                    if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins"))
                    {
                        label1.Text = " is ready!";
                        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins");
                     
                    }
                    string input3 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\atlas";
                    copyFiles(input3, out3);
                }


            //}
            RegistryKey qgisKey = null;
            var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            string selectedKey = comboBox1.SelectedItem.ToString();
            if (selectedKey == "QGIS 3_0")
            {
                qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.0");
            }
            if (selectedKey == "QGIS 3_4")
            {
                qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.4");
            }
            if (selectedKey == "QGIS 3_6")
            {
                qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.6");
            }
            if (selectedKey == "QGIS 3_8")
            {
                qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.8");
            }
            if (selectedKey == "QGIS 3_10")
            {
                qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.10");
            }
            if (selectedKey == "QGIS 3_12")
            {
                qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.12");
            }
            
            
           
            
            
            
            checkStatus(qgisKey);
            if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed")
            { 
                label1.ForeColor = System.Drawing.Color.Green;
                label1.Text = "Plugin is ready!";
            }
            else
            {
                label1.Text = "You must run program as administrator!";
            }
            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            getVersions();
            label1.Text = "";
            //   checkStatus();
            if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed")
            {
                //  label1.ForeColor = System.Drawing.Color.Green;
                label1.Text = "Plugin is already installed.";
            }
            else
            {
                label1.Text = "Plugin is not installed.";
            }
            if (list.Count == 0)
            {
                button3.Enabled = false;
                label1.Text = "QGIS is not installed.";
            }
        }
        private void downloadPlugin(string path)
        {
            WebClient Client = new WebClient();
            Client.DownloadFile("https://gitlab.com/Vrobel/atlas/-/archive/master/atlas-master.zip", path + "\\atlas.zip");
            try { Directory.Delete(path + "\\atlas", true); }
            catch { }
            
            ZipFile.ExtractToDirectory(path + "\\atlas.zip", path);
            Directory.Move(path + "\\atlas-master", path + "\\atlas");
            try { Directory.Delete(path + "\\atlas-master", true); }
            catch { }
        }
        private bool isPluginInstalled(string o)
        {
            string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
            string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
            if (File.Exists(plugin) || File.Exists(plugin2))
            {
                return true;
            }
            else
            {
                return false;
            }
            
        }
        private void label9_Click(object sender, EventArgs e)
        {

        }
        private void checkStatus()
        {
            using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            using (var key = hklm.OpenSubKey(@"Software\\QGIS 3.4"))

            {
                string o = key.GetValue("InstallPath").ToString();

                string wergzeug = o + @"\apps\Python37\Lib\site-packages\werkzeug\test.py";
                if (File.Exists(wergzeug))
                {
                    label2.Text = "Installed";
                }
                else
                {
                    label2.Text = "Not Installed";
                }
                string flask = o + @"\apps\Python37\Lib\site-packages\flask\__init__.py";
                string flask2 = o + @"\apps\Python37\Scripts\flask.exe";
                if (File.Exists(flask) && File.Exists(flask2))
                {
                    label3.Text = "Installed";
                }
                else
                {
                    label3.Text = "Not Installed";
                }
                string click = o + @"\apps\Python37\Lib\site-packages\click\__init__.py";
                if (File.Exists(click))
                {
                    label4.Text = "Installed";
                }
                else
                {
                    label4.Text = "Not Installed";
                }
                string dangerous = o + @"\apps\Python37\Lib\site-packages\itsdangerous\__init__.py";
                if (File.Exists(dangerous))
                {
                    label5.Text = "Installed";
                }
                else
                {
                    label5.Text = "Not Installed";
                }

                string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
                string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
                if (File.Exists(plugin) || File.Exists(plugin2))
                {
                    label6.Text = "Installed";
                }
                else
                {
                    label6.Text = "Not Installed";
                }

            }
        }
        private void checkStatus(RegistryKey key)
        {
            
                string o = key.GetValue("InstallPath").ToString();

                string wergzeug = o + @"\apps\Python37\Lib\site-packages\werkzeug\test.py";
                if (File.Exists(wergzeug))
                {
                    label2.Text = "Installed";
                }
                else
                {
                    label2.Text = "Not Installed";
                }
                string flask = o + @"\apps\Python37\Lib\site-packages\flask\__init__.py";
                string flask2 = o + @"\apps\Python37\Scripts\flask.exe";
                if (File.Exists(flask) && File.Exists(flask2))
                {
                    label3.Text = "Installed";
                }
                else
                {
                    label3.Text = "Not Installed";
                }
                string click = o + @"\apps\Python37\Lib\site-packages\click\__init__.py";
                if (File.Exists(click))
                {
                    label4.Text = "Installed";
                }
                else
                {
                    label4.Text = "Not Installed";
                }
                string dangerous = o + @"\apps\Python37\Lib\site-packages\itsdangerous\__init__.py";
                if (File.Exists(dangerous))
                {
                    label5.Text = "Installed";
                }
                else
                {
                    label5.Text = "Not Installed";
                }

                string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
                string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
                if (File.Exists(plugin) || File.Exists(plugin2))
                {
                    label6.Text = "Installed";
                }
                else
                {
                    label6.Text = "Not Installed";
                }

            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            downloadPlugin(path);
        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

      //  private void button2_Click(object sender, EventArgs e)
        private void getVersions()
        {
            using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) {
                
                key3_4 = hklm.OpenSubKey(@"Software\\QGIS 3.4");                
                key3_0 = hklm.OpenSubKey(@"Software\\QGIS 3.0");
                key3_6 = hklm.OpenSubKey(@"Software\\QGIS 3.6");
                key3_10 = hklm.OpenSubKey(@"Software\\QGIS 3.10");
                key3_8 = hklm.OpenSubKey(@"Software\\QGIS 3.8");
                key3_12 = hklm.OpenSubKey(@"Software\\QGIS 3.12");
                if (key3_4 != null) { 
                    list.Add(key3_4);
                    label13.Text = "QGIS 3_4 detected \n";
                    comboBox1.Items.Add("QGIS 3_4");
                    comboBox1.Text = "QGIS 3_4";
// checkStatus(key3_4);
                }
                if (key3_0 != null)
                {
                    list.Add(key3_0);
                    label13.Text = label13.Text + "QGIS 3_0 detected \n";
                    comboBox1.Items.Add("QGIS 3_0");
                    comboBox1.Text = "QGIS 3_0";
                 //   checkStatus(key3_0);
                }
                if (key3_6 != null)
                    {
                        list.Add(key3_6);
                    label13.Text = label13.Text + "QGIS 3_6 detected \n";
                    comboBox1.Items.Add("QGIS 3_6");
                    comboBox1.Text = "QGIS 3_6";
               //     checkStatus(key3_6);
                }
                if (key3_10 != null)
                        {
                            list.Add(key3_10);
                    label13.Text = label13.Text + "QGIS 3_10 detected \n";
                    comboBox1.Items.Add("QGIS 3_10");
                    comboBox1.Text = "QGIS 3_10";
             //       checkStatus(key3_10);
                }
                if (key3_8 != null) { 
                    list.Add(key3_8);
                    label13.Text = label13.Text + "QGIS 3_8 detected \n";
                    comboBox1.Items.Add("QGIS 3_8");
                    comboBox1.Text = "QGIS 3_8";
                 //   checkStatus(key3_8);
                }
                if (key3_12 != null)
                {
                    list.Add(key3_8);
                    label13.Text = label13.Text + "QGIS 3_12 detected \n";
                    comboBox1.Items.Add("QGIS 3_12");
                    comboBox1.Text = "QGIS 3_12";
                 //   checkStatus(key3_12);
                }
                



            }


    }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String s = comboBox1.Text;
            if (s == "QGIS 3_4")
                checkStatus(key3_4);
            if (s == "QGIS 3_10")
                checkStatus(key3_10);
            if (s == "QGIS 3_0")
                checkStatus(key3_0);
            if (s == "QGIS 3_8")
                checkStatus(key3_8);
            if (s == "QGIS 3_12")
                checkStatus(key3_12);
            if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed") { 
                //  label1.ForeColor = System.Drawing.Color.Green;
                label1.Text = "Plugin is already installed.";
            }
            else {
                label1.Text = "Plugin is not installed.";
            }
        }
    }
    

}
