using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
using Godot.Collections;
using OpenScadGraphEditor.Library.IO;
namespace OpenScadGraphEditor.Library.External
{
///
/// A reference to an external OpenSCAD file.
///
public class ExternalReference
{
///
/// Flag indicating whether the file has actually been loaded and the functions and modules and variables
/// are up to date.
///
public bool IsLoaded { get; set; }
///
/// Flag marking whether this reference was made from the main project or from another library.
///
public bool IsTransitive => !IncludedBy.Empty();
///
/// ID of the external reference which has included this. Only defined if this is a transitive reference.
///
public string IncludedBy { get; set; } = "";
///
/// Paths included by this file (only _include_ but not _use_.).
///
public List References { get; set; } = new List();
///
/// Id of this external reference.
///
public string Id { get; set; }
///
/// The include path that was used to get this reference.
///
public string IncludePath { get; set; }
///
/// The include mode used to get this reference. (see https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Include_Statement).
///
public IncludeMode Mode { get; set; }
///
/// All functions defined in the source file.
///
public List Functions { get; set; } = new List();
///
/// All modules defined in the source file.
///
public List Modules { get; set; } = new List();
///
/// All variables defined in the source file.
///
public List Variables { get; set; } = new List();
public void LoadFrom(SavedExternalReference savedExternalReference)
{
Id = savedExternalReference.Id;
IsLoaded = true;
IncludedBy = savedExternalReference.IncludedBy;
References = savedExternalReference.References.ToList();
IncludePath = savedExternalReference.IncludePath;
Mode = savedExternalReference.Mode;
Functions = savedExternalReference.Functions
.Select(it => (FunctionDescription) it.FromSavedState())
.ToList();
Modules = savedExternalReference.Modules
.Select(it => (ModuleDescription) it.FromSavedState())
.ToList();
Variables = savedExternalReference.Variables
.Select(it => it.FromSavedState())
.ToList();
}
public void SaveInto(SavedExternalReference savedExternalReference)
{
savedExternalReference.Id = Id;
savedExternalReference.IncludedBy = IncludedBy;
savedExternalReference.References = new Array(References);
savedExternalReference.IncludePath = IncludePath;
savedExternalReference.Mode = Mode;
foreach (var functionDescription in Functions)
{
savedExternalReference.Functions.Add((SavedFunctionDescription) functionDescription.ToSavedState());
}
foreach (var moduleDescription in Modules)
{
savedExternalReference.Modules.Add((SavedModuleDescription) moduleDescription.ToSavedState());
}
savedExternalReference.Variables = new Array();
foreach (var variableDescription in Variables)
{
savedExternalReference.Variables.Add(variableDescription.ToSavedState());
}
}
public string Render()
{
if (IsTransitive)
{
return "";
}
switch (Mode)
{
case IncludeMode.Include:
return $"include <{IncludePath}>;\n";
case IncludeMode.Use:
return $"use <{IncludePath}>;\n";
default:
throw new ArgumentOutOfRangeException();
}
}
}
}