using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataUploader
{
    public partial class FullSync : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // verifico se vado in modalità demo o meno...
            bool demoMode = false;
            try
            {
                demoMode = Convert.ToBoolean(Request.QueryString["DemoMode"]);
            }
            catch
            { }
            // recupero ultimo numero doc caricato...
            int lastNum = 0;
            try
            {
                var client = new WebClient();
                var content = client.DownloadString(ConfigurationManager.AppSettings["remoteDocUrl"]); //  http://webscr.steamware.net/Rigamonti/Upload/Document
                // converto a int!
                lastNum = Convert.ToInt32(content);
            }
            catch
            { }
            // recupero i dati da caricare...
            string[] urlsUpdate = GestData.proc.getNewDocsUrls(lastNum);
            if (demoMode)
            {
                lblOut.Text = string.Format("DEMO MODE!<br/>ultimo NUMERO: {0}", lastNum);
                for (int i = 0; i < urlsUpdate.Length; i++)
                {
                    lblOut.Text += string.Format("<br/>{0:000}) {1}", i + 1, urlsUpdate[i]);
                }
            }
            else
            {
                lblOut.Text = string.Format("Normal MODE!<br/>{0}", lastNum);
                var client = new WebClient();
                for (int i = 0; i < urlsUpdate.Length; i++)
                {
                    try
                    {
                        var content = client.DownloadString(urlsUpdate[i]);
                    }
                    catch
                    { }
                }
            }
        }
    }
}