using AppData;
using SteamWare;
using System;
using System.IO;
using System.Web.UI;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_MU_svgViewer : BaseUserControl
    {
        /// <summary>
        /// BatchId corrente...
        /// </summary>
        public int BatchId
        {
            set
            {
                if (hfBatchId.Value != value.ToString())
                {
                    hfBatchId.Value = value.ToString();
                }
            }
            get
            {
                int answ = 0;
                int.TryParse(hfBatchId.Value, out answ);
                return answ;
            }
        }
        /// <summary>
        /// Foglio corrente...
        /// </summary>
        public int SheetId
        {
            set
            {
                if (hfSheetId.Value != value.ToString())
                {
                    hfSheetId.Value = value.ToString();
                    doUpdate();
                }
            }
            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 doUpdate()
        {
            // recupero ID del foglio corrente
            string answ = "";
            string baseOrig = memLayer.ML.CRS("nestBasePath").ToLower();
            string baseCurr = memLayer.ML.CRS("servBasePath").ToLower();
            try
            {
                var tabSheets = DLMan.taSHL.getBySheetId(SheetId);
                if (tabSheets.Count > 0)
                {
                    filename = tabSheets[0].DrawFilePath.ToLower().Replace(baseOrig, baseCurr);
                    if (memLayer.ML.CRB("userVirtDir"))
                    {
                        filename = Server.MapPath(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");
                answ = File.ReadAllText(filename);
            }
            // update componente svg!
            svgTable.InnerHtml = answ;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // scrivo valore revisione batch!
                hfCurrBunkRev.Value = ComLib.getSheetRevByBunk(BatchId).ToString();
            }
        }
    }
}