using AppData;
using SteamWare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_svgFull : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                filename = "../images/test.svg";
            }
        }

        /// <summary>
        /// Foglio corrente...
        /// </summary>
        public int SheetId
        {
            set
            {
                hfSheetId.Value = value.ToString();
                updateSvg();
            }
            get
            {
                int answ = 0;
                int.TryParse(hfSheetId.Value, out answ);
                return answ;
            }
        }
        /// <summary>
        /// Percorso file calcolato
        /// </summary>
        public string filename
        {
            get
            {
                return hfFilename.Value;
            }
            set
            {
                hfFilename.Value = value;
            }
        }
        public void updateSvg()
        {
            // recupero ID del foglio corrente
            string answ = "";
            string baseOrig = memLayer.ML.CRS("nestBasePath").ToLower();
            string baseCurr = memLayer.ML.CRS("servBasePath").ToLower();
            try
            {
                if (SheetId > 0)
                {
                    var tabSheets = DLMan.taSHL.getBySheetId(SheetId);
                    if (tabSheets.Count > 0)
                    {
                        filename = tabSheets[0].DrawFilePath.ToLower().Replace(baseOrig, baseCurr);
                        // la versioen da usare è la "_qr.svg", x cui faccio replace... tolgo ultimi 4 char e sostituisco...
                        int repPos = filename.LastIndexOf(".svg");
                        filename = filename.Remove(repPos) + "_qr.svg";
                        if (memLayer.ML.CRB("userVirtDir"))
                        {
                            filename = Server.MapPath(filename);
                        }
                        answ = File.ReadAllText(filename);
                    }
                }
                else
                {
                    if (File.Exists(filename))
                    {
                        answ = File.ReadAllText(filename);
                    }
                }
            }
            catch
            { }
            if (string.IsNullOrEmpty(answ))
            {
                // loggo...
                logger.lg.scriviLog($"SVG not found | filename: {filename} | baseOrig: {baseOrig} | baseCurr: {baseCurr} | used default SVG");
                // leggo SVG DI DEFAULT che indica NON PRESENTE...
                filename = Server.MapPath("~/Images/NoData.svg");
                //filename = Server.MapPath("~/Images/NoData.svg");
                answ = File.ReadAllText(filename);
            }
            // update componente svg!
            svgTable.InnerHtml = answ;
        }
    }
}