using SteamWare;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.site
{
    public partial class UpdMan : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                ((SiteContent)this.Master).showSearch = false;
            }
        }


        /// <summary>
        /// Eseguo download pacchetti secondo richeista
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtDownload_Click(object sender, EventArgs e)
        {
            // NOTA: da rivedere con basic AUTH x nexus... da cambiare metodi libreria steamwareLibs...
            // https://putridparrot.com/blog/downloading-a-file-from-url-using-basic-authentication/
            // https://www.codeproject.com/Articles/13485/Secure-File-Download-Using-Basic-Authentication
            // https://stackoverflow.com/questions/38798237/downloading-files-from-web-using-basic-url-authorization

            // dati da salvare in RESOURCE file...vedere progetto MHT Siemens
            // USER: SWDownloader
            // PWD: viaD@nte16


            LinkButton lnk = (LinkButton)sender;
            string caller = lnk.CommandArgument;
            string resultsMsg = "";
            // in base al caller scarico il file installazione richiesto...
            if (caller == null)
            {
                caller = "ALL";
            }
            // ora verifico COSA devo scaricare...
            if (!string.IsNullOrEmpty(updateUrl(caller)))
            {
                // se è ALL scarico tutti...
                if (caller == "ALL")
                {
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Start();
                    int done = 0;
                    // scarico TUTTI...
                    done += UpdateMan.obj.downloadLatest(updateUrl("NKC"), localDownloadPath("NKC"), "NKC");
                    done += UpdateMan.obj.downloadLatest(updateUrl("ZCODE"), localDownloadPath("ZCODE"), "ZCODE");
                    stopWatch.Stop();
                    // calcolo elapsed time come TimeSpan value.
                    TimeSpan ts = stopWatch.Elapsed;
                    resultsMsg = string.Format("OK: {0} Package downloaded, {1:0.000} sec", done, ts.TotalSeconds);
                }
                else
                {
                    // scarico singolo target...
                    UpdateMan.obj.downloadLatest(updateUrl(caller), localDownloadPath(caller), caller);
                    resultsMsg = string.Format("OK: {0} DOWNLOADED | {1} --> {2}", caller, updateUrl(caller), localDownloadPath(caller));
                }
            }
            // aggiorno messaggio
            lblOut.Text = resultsMsg;
        }

        public string Version(object package)
        {
            string answ = "a.b.c.d";
            UpdateInfoEventArgs updArgs = new UpdateInfoEventArgs();
            // Recupero info ADM...
            updArgs = UpdateMan.obj.getUpdateInfo(updateUrl(package.ToString()));
            if (updArgs.IsUpdateAvailable)
            {
                answ = updArgs.CurrentVersion.ToString();
            }
            return answ;
        }

        /// <summary>
        /// URLK stringa di UPDATE...
        /// </summary>
        protected string updateUrl(string package)
        {
            return string.Format("http://seriate.steamware.net:8083/SWS/{0}/{1}/manifest.xml", package, memLayer.ML.CRS("appVers"));
        }

        /// <summary>
        /// Path locale dove scaricare gli installer
        /// </summary>
        protected string localDownloadPath(string package)
        {
            return string.Format(@"{0}{1}\{2}", memLayer.ML.CRS("downloadPath"), package, memLayer.ML.CRS("appVers"));
        }
    }
}