using Egw.Window.Data;
using Microsoft.AspNetCore.Components;
using Newtonsoft.Json;
using NLog;
using WebWindowComplex.Compo;
using WebWindowComplex.DTO;
using WebWindowComplex.Json;
using WebWindowComplex.Models;
using static WebWindowComplex.Json.WindowConst;
using static WebWindowComplex.LayoutConst;
namespace WebWindowComplex
{
public partial class TableComp : IDisposable
{
#region Public Enums
public enum DataReq
{
///
/// Nessuna richiesta
///
None = 0,
///
/// Richiesta hardware opzioni
///
ReqHwOpt,
///
/// Richiesta svg senza hw
///
ReqSvgNoHw,
///
/// Richiesta svg con hw
///
ReqSvgWithHw,
///
/// Richiesta shape sash group
///
ReqShape,
///
/// Richiesta element
///
ReqElement
}
#endregion Public Enums
#region Public Properties
///
/// Livello di accesso (utente base)
///
[Parameter]
public bool baseUser { get; set; } = false!;
///
/// Classe override css x SVG (x rescale)
///
[Parameter]
public string CssSvg { get; set; } = "responsive-svg";
///
/// Richiesta azione al parent
///
[Parameter]
public EventCallback EC_ActionReq { get; set; }
///
/// Richiesta update da JWD (SVG, calcoli vari...)
///
[Parameter]
public EventCallback> EC_DoUpdate { get; set; }
///
/// Richiesta chiusura JWD (+ refresh)
/// se torna true --> richiesta salvataggio
/// se torna false --> richiesta revert
///
[Parameter]
public EventCallback EC_OnClose { get; set; }
///
/// Sollevo evento errore validazione con una lista di errori rilevati
///
[Parameter]
public EventCallback> EC_ValidError { get; set; }
///
/// Richiesta azione per le richieste pendenti
///
[Parameter]
public EventCallback> EC_UpdateReqPend { get; set; }
///
/// Elenco anagrafiche di base
///
[Parameter]
public BaseListPayload ListPayload { get; set; } = null!;
///
/// Dati live controllo (JWD, SVG, ...)
///
[Parameter]
public LivePayload LiveData { get; set; } = null!;
///
/// Preselezione valori
///
[Parameter]
public Dictionary? PendReq { get; set; } = null;
#endregion Public Properties
#region Public Methods
public void Dispose()
{
if (m_CurrWindow != null)
{
m_CurrWindow = null;
}
}
#endregion Public Methods
#region Protected Methods
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (forceSvgUpdate && prevReq != (int)DataReq.ReqSvgNoHw && prevReq != (int)DataReq.ReqElement)
{
firstDisplay = false;
forceSvgUpdate = false;
// preview SVG senza HW
if (prevReq != (int)DataReq.ReqSvgWithHw)
await DoPreviewSvg(true, true);
if (LiveData.ProfElementList.Count == 0)
await UpdateElement(FrameWindow);
}
isRendered = true;
}
///
/// Primo init componente
///
protected override void OnInitialized()
{
// reset editLock...
editLock = false;
listErrPre = new Dictionary();
listErrLink = new Dictionary();
listWarnings = new Dictionary();
}
///
/// Gestione update post ricezione parametri da controllo chiamante
///
protected override async Task OnParametersSetAsync()
{
isLoading = true;
// SOLO SE sono presenti...
listErrLink = new Dictionary();
okParams = ListPayload.IsPopulated();
okJwd = LiveData.IsValid();
if (okParams && okJwd)
{
bool updRequested = false;
listErrPre = new Dictionary();
listWarnings = new Dictionary();
// controllo elenchi BasePayload siano validi...
if (ListPayload.IsValid())
{
bool calculateSvg = false;
// SOLO SE modificato live data...
if (prevLiveData == null || !prevLiveData.Equals(LiveData))
{
bool needDeser = prevLiveData == null || !prevLiveData.JwdEqual(LiveData);
calculateSvg = prevLiveData == null || !prevLiveData.DictShape.Equals(LiveData.DictShape);
updRequested = true;
prevLiveData = new LivePayload()
{
CurrJwd = LiveData.CurrJwd,
DictOptionsXml = LiveData.DictOptionsXml,
DictShape = LiveData.DictShape,
SvgPreview = LiveData.SvgPreview,
ProfElementList = LiveData.ProfElementList
};
// provo a deserializzare SE necessario
if (needDeser)
{
JsonWindow WindowFromJson = new JsonWindow("", "", "", "");
try
{
WindowFromJson = JsonConvert.DeserializeObject(LiveData.CurrJwd, new PolymorphicJsonConverter()) ?? new JsonWindow("", "", "", "");
if (WindowFromJson == null || WindowFromJson.AreaList == null || WindowFromJson.AreaList.Count == 0)
{
WindowFromJson = BuildWindowDefault();
Log.Info("Ricostruito jwd");
}
UpdInputListJson(WindowFromJson);
setCurrWindow(WindowFromJson);
//SOLO SE non sono in edit...
if (!editLock)
{
currStep = CompileStep.Tree;
}
}
// altrimenti errore!
catch (Exception ex)
{
listErrLink.Add("Window", $"Deserializing Error:{Environment.NewLine}{ex}");
Log.Error($"Errore nel deserializzare jwd: {ex.Message}");
// costruisco una finestra base
WindowFromJson = BuildWindowDefault();
await DoPreviewSvg(false);
}
}
else
{
// usando m_currWindow (preesistente) impostare i parametri...
UpdateDict();
}
}
if (m_CurrWindow != null && m_CurrWindow.AreaList != null && m_CurrWindow.AreaList.Count > 0)
{
checkWarnings();
if (SashGroupList.Count > 0)
currAntaIndex = 0;
if (updRequested)
{
// se mancasse dizionario forme chiamo quello
if (LiveData.DictShape.Count == 0 && listErrLink.Count == 0 && !waitShape)
{
waitShape = true;
if (firstDisplay && isRendered && prevReq != (int)DataReq.ReqSvgNoHw)
{
firstDisplay = false;
// preview SVG senza HW
await DoPreviewSvg(true, true);
if (LiveData.ProfElementList.Count == 0)
await UpdateElement(FrameWindow);
}
else
{
forceSvgUpdate = true;
}
List reqList = SashGroupList.Select(x => x.GroupId).ToList();
if (reqList.Count > 0)
{
await DoReqShape(reqList);
}
}
else if (LiveData.DictShape.Count > 0 && calculateSvg)
{
await DoPreviewSvg();
}
else
{
if (prevReq != (int)DataReq.ReqSvgWithHw && SashGroupList.Count > 0)
await DoPreviewSvg();
}
}
}
else
{
JsonWindow WindowFromJson = new JsonWindow("", "", "", "");
WindowFromJson = BuildWindowDefault();
await DoPreviewSvg(true);
}
}
else
{
checkErrorPre();
}
}
isLoading = false;
// Controllo se ci sono domande pendenti
if (PendReq != null && PendReq.Count > 0)
{
if (!string.IsNullOrEmpty(PendReq.GetValueOrDefault("Profile")) && m_CurrWindow != null)
{
reqBool = true;
currStep = CompileStep.General;
}
//PendReq.Remove("Profile");
//await EC_PendantAction.InvokeAsync(PendReq);
}
}
#endregion Protected Methods
#region Private Fields
private bool reqBool = false;
private bool waitShape = false;
private static Logger Log = LogManager.GetCurrentClassLogger();
private int currAntaIndex = 0;
private int currFillIndex = -1;
private List currLoading = new List();
private int currSashIndex = -1;
private int currSplitIndex = -1;
private int currInglesinaIndex = -1;
private CompileStep currStep;
private bool editLock = false;
private bool firstDisplay = true;
private bool forceSvgUpdate = false;
private bool groupIdOK = false;
///
/// Booleana fase loading
///
private bool isLoading = false;
private bool isRendered = false;
///
/// ELenco errori di coerenza/link dati (vanno risolti per disegnare/procedere)
///
private Dictionary listErrLink = new Dictionary();
///
/// Elenco errori preliminari (mancano elementi di base di validazione modello dati))
///
private Dictionary listErrPre = new Dictionary();
///
/// Elenco warnings non bloccanti
///
private Dictionary listWarnings = new Dictionary();
private List m_FillList = new List();
private Frame? m_Frame;
private List m_ItemTableList = new List();
private int m_maxCol = 0;
private int m_maxRow = 0;
private List m_SashGroupList = new List();
private List m_SplitList = new List();
private List m_SplittedList = new List();
private List m_InglesinaList = new List();
private bool okJwd = false;
private bool okParams = false;
///
/// Dati live precedenti x comparazione
///
private LivePayload? prevLiveData = null;
#endregion Private Fields
#region Private Properties
private Dictionary Child { get; set; } = new Dictionary();
private List FillList
{
get => m_FillList;
}
private Frame FrameWindow
{
get => m_Frame!;
set => m_Frame = value;
}
private List ItemTableList
{
get => m_ItemTableList;
}
private Window? m_CurrWindow { get; set; } = null;
private Window? m_PreviousWindow { get; set; } = null;
///
/// Componente SVG da mostrare
///
private MarkupString outSvg
{
get
{
// aggiunta gestione classe svg per posizionamento con costraints
var newSvg = LiveData.SvgPreview.Replace("