/// <summary>
        /// Generates a name for the TreeNode.
        /// </summary>
        /// <returns></returns>
        protected override string GenerateName()
        {
            // Generate a name based on the current structure type.
            BlueprintStructure ownerStructure = (BlueprintStructure)OwnerObject;

            return(ownerStructure.SceneID.GetEnumDescription() ?? "Unspecified Structure");
        }
        /// <summary>
        /// Refreshes the node's name.
        /// </summary>
        /// <param name="includeSubTrees"></param>
        protected override void RefreshName()
        {
            BlueprintDockingPort dockingPort = (BlueprintDockingPort)OwnerObject;
            BlueprintStructure   structure   = dockingPort.OwnerStructure;

            //Debug.Print("structure {0}, ({1}), ID {2}", (int)structure?.SceneID, structure?.SceneName, structure?.StructureID);
            //Debug.Print("docking port {0}, ({1})", dockingPort?.OrderID, dockingPort?.PortName);

            if (structure == null || dockingPort == null || structure.SceneID == null ||
                dockingPort.PortName == null || !AutoGenerateName)
            {
                /*
                 * Debug.Print("RefreshName() skipped.");
                 * if (structure == null) Debug.Print("structure == null");
                 * else if (structure.SceneID == null) Debug.Print("structure.SceneID == null");
                 *
                 * if (dockingPort == null) Debug.Print("dockingPort == null");
                 * else if (dockingPort.PortName == null) Debug.Print("dockingPort.PortName == null");
                 *
                 * if (!AutoGenerateName) Debug.Print("AutoGenerate == false");
                 */
            }
            else
            {
                Name = GenerateName();
                // Debug.Print("RefreshName() generated [{0}].", Name);
            }
        }
        public void BuildStationBlueprintFromJData()
        {
            Debug.Print("------------------------------------------------------------");

            Debug.Print("BuildStationBlueprintFromJData: Starting.");

            //// Check it's for the correct __ObjectType.
            //JToken testToken = JData["__ObjectType"];
            //if (testToken == null || (string)testToken != "StationBlueprint")
            //{
            //    Debug.Print("BuildStationBlueprintFromJData: ObjectType not StationBlueprint");
            //    return;
            //}

            JToken testToken;

            // Create a new StationBlueprint.
            BlueprintObject = new StationBlueprint
            {
                OwnerObject = this,
                // __ObjectType = BlueprintObjectType.StationBlueprint
            };


            // Set the blueprint name;
            testToken = JData["Name"];
            if (testToken != null)
            {
                BlueprintObject.Name = (string)testToken;
            }
            // Set the blueprint Link Uri;
            testToken = JData["LinkURI"];
            if (testToken != null)
            {
                BlueprintObject.LinkURI = (Uri)testToken;
            }

            // Set blueprint level properties.
            testToken = JData["Invulnerable"];
            if (testToken != null)
            {
                BlueprintObject.Invulnerable = (bool)testToken;
            }
            testToken = JData["SystemsOnline"];
            if (testToken != null)
            {
                BlueprintObject.SystemsOnline = (bool)testToken;
            }
            testToken = JData["DoorsLocked"];
            if (testToken != null)
            {
                BlueprintObject.DoorsLocked = (bool)testToken;
            }
            Debug.Print("------------------------------------------------------------");
            Debug.Print("Creating new blueprint structures (" + JData["Structures"].Count() + ")");
            foreach (JObject structure in JData["Structures"])
            {
                Debug.Print("------------------------------");
                StructureSceneID sceneID = StructureSceneID.Unspecified;

                int structureID = (int)structure["StructureID"];
                Debug.Print("structureID " + structureID);


                // Attempt to get a StructureType - this is a mix between the in-game three letter
                // names and the SceneNames.
                testToken = structure["StructureType"];
                if (testToken != null)
                {
                    string parsedStructureType = (string)structure["StructureType"];
                    Debug.Print("parsedStructureType " + parsedStructureType);
                    sceneID = parsedStructureType.ParseToEnumDescriptionOrEnumerator <StructureSceneID>();
                    Debug.Print("sceneID " + sceneID);
                }

                //// Attempt to get a SceneID - this is for newer format blueprints.
                //testToken = structure["SceneID"];
                //if (testToken != null)
                //{
                //    int parsedSceneID = (int)structure["SceneID"];
                //    sceneID = parsedSceneID.ParseToEnumDescriptionOrEnumerator<StructureSceneID>();
                //}

                // Call the BlueprintObject's AddStructure method with the sceneID and structureID.
                BlueprintStructure newStructure = BlueprintObject.AddStructure(sceneID, structureID);

                // A StationBlueprint file should have only one docking tree and StructureID=0 is the root.
                if (newStructure.StructureID != 0)
                {
                    newStructure.IsStructureHierarchyRoot = false;
                }


                Debug.Print(" newStructure ports (" + newStructure.DockingPorts.Count + ")");
                foreach (BlueprintDockingPort tmpPort in newStructure.DockingPorts)
                {
                    Debug.Print("  PortName " + tmpPort.PortName);
                    Debug.Print("  OrderID " + tmpPort.OrderID);
                    Debug.Print("  Locked " + tmpPort.Locked);
                }
            }

            Debug.Print("------------------------------------------------------------");
            Debug.Print("Setting docking port data from loaded data.");
            foreach (JObject structure in JData["Structures"])
            {
                Debug.Print("------------------------------");
                int structureID = (int)structure["StructureID"];
                Debug.Print("structureID " + structureID);

                BlueprintStructure newStructure = BlueprintObject.GetStructure(structureID);
                if (newStructure == null)
                {
                    throw new Exception();
                }


                foreach (JObject dockingPort in structure["DockingPorts"])
                {
                    Debug.Print("-------------------------");

                    Debug.Print(" DockingPort");
                    int orderID = (int)dockingPort["OrderID"];
                    Debug.Print(" orderID " + orderID);


                    int?dockedStructureID = (int?)dockingPort["DockedStructureID"];
                    Debug.Print(" dockedStructureID " + dockedStructureID);


                    bool locked = dockingPort["Locked"] != null ? (bool)dockingPort["Locked"] : false;
                    Debug.Print(" locked " + locked);

                    BlueprintDockingPort newPort = newStructure.GetDockingPortByOrderID(orderID);
                    if (newPort == null)
                    {
                        throw new Exception();
                    }

                    // If we got here we should have the port in the new structure that corresponds
                    // to the port in this JObject.

                    newPort.DockedStructureID = dockedStructureID;

                    newPort.Locked = locked;
                }
            }
        }
        /// <summary>
        /// Generates a new StructureDefinitions.json file.
        /// </summary>
        /// <param name="passedFileInfo"></param>
        /// <param name="structuresJsonFile"></param>
        public void GenerateAndSaveNewStructureDefinitionsFile(FileInfo passedFileInfo, Json_File_UI structuresJsonFile)
        {
            // BlueprintObject.__ObjectType = BlueprintObjectType.BlueprintStructureDefinitions;
            BlueprintObject.Version = StationBlueprintFormatVersion;
            BlueprintObject.Name    = string.Format("Hellion Station Blueprint File Format - Structure Definitions Template Version {0} Generated {1}",
                                                    StationBlueprintFormatVersion, DateTime.Now);
            BlueprintObject.LinkURI = new Uri(@"https://github.com/CheeseJedi/Hellion-Station-Blueprint-Format");

            //BlueprintObject.AuxData = new BlueprintStructure_AuxData(null);

            // Loop through all the structures in the Structures.Json file
            foreach (JToken jtStructure in structuresJsonFile.JData)
            {
                // Create a new Structure definition
                BlueprintStructure nsd = new BlueprintStructure
                {
                    SceneID = (StructureSceneID)Enum
                              .Parse(typeof(StructureSceneID), (string)jtStructure["ItemID"]),

                    //AuxData = new BlueprintStructure_AuxData(null),

                    // Calculate the total (nominal) air volume.
                    //NominalAirVolume = (float)jtStructure["Rooms"].Sum(v => (float)v.SelectToken("Volume")),

                    // Look up the Power requirement for this module.
                    // Select subsystem type 13 (VesselBasePowerConsumer) usually with RoomID of -1
                    //StandbyPowerRequirement = (float)jtStructure.SelectToken(
                    //    "$.SubSystems.[?(@.Type == 13)].ResourceRequirements[0].Standby"),

                    // NominalPowerRequirement = (float)jtStructure.SelectToken(
                    //  "$.SubSystems.[?(@.Type == 13)].ResourceRequirements[0].Nominal"),

                    // Need to locate the info probably from the generators system.
                    // Not currently set.
                    //NominalPowerContribution = null;
                };

                // Loop through the jtStructure's DockingPort collection.
                foreach (JToken jtDockingPort in jtStructure["DockingPorts"])
                {
                    BlueprintDockingPort newDockingPortDefinition = new BlueprintDockingPort
                    {
                        // OrderID is critical as this is what the game uses as the key to match
                        // the ports in-game.
                        OrderID = (int)jtDockingPort["OrderID"],

                        // Look up the correct port name for this structure and orderID
                        PortName = GetDockingPortType((StructureSceneID)nsd.SceneID,
                                                      orderID: (int)jtDockingPort["OrderID"]),

                        // Default locked/unlocked status is preserved.
                        Locked = (bool)jtDockingPort["Locked"],
                    };

                    nsd.DockingPorts.Add(newDockingPortDefinition);
                }

                BlueprintObject.Structures.Add(nsd);
            }

            Serialise();

            SaveFile(createBackup: true);
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="passedParent"></param>
 public BlueprintDockingPort(BlueprintStructure passedParent) : this()
 {
     RootNode.AutoGenerateName = true;
     OwnerStructure            = passedParent;
 }