using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;
using BazaZamowien.Classes;

namespace BazaZamowien
{
    public partial class FormBadVer : Form
    {
        private WebClient webClient;
        private bool isBusy = false;
        const string strUrl = "http://www.infocity.pl/baza_zamowien/Updater.exe";

        public FormBadVer()
        {
            InitializeComponent();
            
            webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string updater = Environment.CurrentDirectory + "\\Updater.exe";

           if (System.IO.File.Exists(updater))
            {
                startUpdater();
            }
            else
            {
                isBusy = true;
                webClient.DownloadFileAsync(new Uri(strUrl), Environment.CurrentDirectory + "\\Updater.exe");            
            }
        }

        private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            isBusy = false;
                     
            if (e.Error == null)
            {
                startUpdater();
            }
            else
            {
                MessageBox.Show("Błąd aktualizacji oprogramowania: " + e.Error.Message, "Aktualizacja oprogramowania", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }

        private void startUpdater()
        {
            string updater = Environment.CurrentDirectory + "\\Updater.exe";

            Close();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.Arguments = "2";
            startInfo.FileName = updater;

            try
            {
                Process.Start(startInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Extract(string zipFileName, string destinationPath)
        {
            Shell32.IShellDispatch4 sc = (Shell32.IShellDispatch4) new Shell32.ShellClass();
         //   Shell32.ShellClass sc = new Shell32.ShellClass();
            Shell32.Folder SrcFlder = sc.NameSpace(zipFileName);
            Shell32.Folder DestFlder = sc.NameSpace(destinationPath);
            Shell32.FolderItems items = SrcFlder.Items();
            DestFlder.CopyHere(items, 20);        
        }


    }
}