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 Baza_Reklam.Classes;

namespace Baza_Reklam
{
    public partial class FormBadVer : Form
    {
        private WebClient webClient;
        private bool isBusy = false;
        const string strUrl = "http://www.infocity.pl/baza_reklam/files/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 = "1";
            startInfo.FileName = updater;
            Process.Start(startInfo);
        }

        private void Extract(string zipFileName, string destinationPath)
        {
            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);        
        }


    }
}