/*public static void AddJournalUIItem(JournalUIItem journalItem,int expo){
     *      Dictionary<int, JournalUIItem> JournalItems = JournalSaver.LoadJournalUIItems (expo);
     *      if(JournalItems.ContainsKey(journalItem.index))
     *         JournalItems.Remove(journalItem.index);
     *      JournalItems.Add (journalItem.index
     *                        , journalItem);
     *      //SaveJournalUIItems (JournalItems,expo);
     * }*/

    //Saves the reactions from a dictionary to the text file

    /*public static void SaveJournalUIItems(Dictionary<int, JournalUIItem> JournalItems,int expo)
     * {
     *      text = new TextEdit("Assets/Resources/journalItems"+expo.ToString()+".txt");
     *      text.ClearFile ();
     *      text.SetInt ("numberOfJournalItems", JournalItems.Count);
     *
     *      int counter = 0;
     *      foreach (JournalUIItem JournalItem in JournalItems.Values)
     *      {
     *              text.SetInt("index" + counter.ToString(), counter);
     *              text.SetString("name" + counter.ToString(), JournalItem.name);
     *              text.SetBool("isDone" + counter.ToString(),JournalItem.isDone);
     *              text.SetInt("numberOfPrerequisites" + counter.ToString() , JournalItem.prerequisites.Count);
     *              for(int i = 0; i < JournalItem.prerequisites.Count;i++){
     *                      Debug.Log("indexPrerequisiteOf" + counter.ToString()+"_"+i.ToString()+"= "+JournalItem.prerequisites[i]);
     *                      text.SetInt("indexPrerequisiteOf" + counter.ToString()+"_"+i.ToString(), JournalItem.prerequisites[i]);
     *              }
     *              counter++;
     *      }
     * }*/
    //Loads the reactions from a file, and returns a dictionary

    public static Dictionary <int, JournalUIItem> LoadJournalUIItems(int expo)
    {
        json = new JSONEditor("journalItems" + expo.ToString());

        int numberOfJournalItems = json.NumberOfFields(0);

        Dictionary <int, JournalUIItem> journalUIItems = new Dictionary <int, JournalUIItem>();

        if (numberOfJournalItems > 0)
        {
            for (int i = 0; i < numberOfJournalItems; i++)
            {
                JournalUIItem journalItem = new JournalUIItem();

                journalItem.index = json.GetInt(i, "index");

                journalItem.name   = json.GetString(i, "name");
                journalItem.isDone = json.GetBool(i, "isDone");
                journalItem.prerequisites.Clear();
                for (int n = 0; n < json.GetInt(i, "numberOfPrerequisites"); n++)
                {
                    int indexOfPre = int.Parse(json.GetSubValue(i, "indexPrerequisiteOf", n));
                    journalItem.prerequisites.Add(indexOfPre);
                }
                journalItem.prerequisitesDone = false;

                if (!journalUIItems.ContainsKey(journalItem.index))
                {
                    journalUIItems.Add(journalItem.index, journalItem);
                }
            }
        }
        return(journalUIItems);
    }
    /* -------------PRODUCT------------- */

    //! Reads/Loads the dictionary from file.
    public static Dictionary <string, Compound> LoadBackgroundCompounds()
    {
        JSONEditor jsonEditor = new JSONEditor("products");

        Dictionary <string, Compound> products = new Dictionary <string, Compound>();

        for (int i = 0; i < jsonEditor.NumberOfObjects(); i++)
        {
            Compound productAcc = new Compound();

            productAcc.Name = jsonEditor.GetString(i, "name");

            productAcc.Formula        = jsonEditor.GetString(i, "formula");
            productAcc.IsSolid        = jsonEditor.GetBool(i, "isSolid" + i.ToString());
            productAcc.MolarMass      = jsonEditor.GetFloat(i, "molarMass");
            productAcc.Purity         = jsonEditor.GetFloat(i, "purity");
            productAcc.Density        = jsonEditor.GetFloat(i, "density");
            productAcc.Polarizability = jsonEditor.GetFloat(i, "polarizability");

            if (!string.IsNullOrEmpty(jsonEditor.GetString(i, "irSpecter")))
            {
                productAcc.irSpecter = Resources.Load <Texture2D>("specter/" + jsonEditor.GetString(i, "irSpecter"));
            }
            else
            {
                productAcc.irSpecter = null;
            }

            productAcc.Conductibility = jsonEditor.GetFloat(i, "conductibility");
            productAcc.Solubility     = jsonEditor.GetFloat(i, "solubility");
            productAcc.FumeHoodOnly   = jsonEditor.GetBool(i, "fumeHoodOnly");

            productAcc.compoundColor = new Color32((byte)jsonEditor.GetInt(i, "compoundColorR"),
                                                   (byte)jsonEditor.GetInt(i, "compoundColorG"),
                                                   (byte)jsonEditor.GetInt(i, "compoundColorB"),
                                                   (byte)255);

            //!Gets the liquid-related variables
            if (!jsonEditor.GetBool(i, "isSolid"))
            {
                productAcc.PH           = jsonEditor.GetFloat(i, "ph");
                productAcc.Turbidity    = jsonEditor.GetFloat(i, "turbidity");
                productAcc.Refratometer = jsonEditor.GetFloat(i, "refratometer");
                productAcc.FlameSpecter = jsonEditor.GetFloat(i, "flameSpecter");
                if (!string.IsNullOrEmpty(jsonEditor.GetString(i, "uvSpecter")))
                {
                    productAcc.uvSpecter = Resources.Load <Texture2D>("specter/" + jsonEditor.GetString(i, "uvSpecter"));
                }
                else
                {
                    productAcc.uvSpecter = null;
                }

                if (!string.IsNullOrEmpty(jsonEditor.GetString(i, "hplc")))
                {
                    productAcc.hplc = Resources.Load <Texture2D>("specter/" + jsonEditor.GetString(i, "hplc"));
                }
                else
                {
                    productAcc.hplc = null;
                }
            }

            productAcc.Molarity = ((productAcc.Purity * productAcc.Density) / productAcc.MolarMass); // number of mols / volume
            Debug.Log(productAcc.Formula + ".Molarity = " + productAcc.Molarity);
            products.Add(productAcc.Formula, productAcc);
        }

        Dictionary <string, Compound> cupboard = ComponentsSaver.LoadCupboardCompounds();

        foreach (string name in cupboard.Keys)
        {
            products.Add(cupboard[name].Formula, cupboard[name]);
        }

        return(products);
    }
    //! Reads/Loads the dictionary from .json file.
    public static Dictionary <string, Compound> LoadCupboardCompounds()
    {
        JSONEditor jsonEditor = new JSONEditor("compounds");

        Dictionary <string, Compound> reagents = new Dictionary <string, Compound>();

        for (int i = 0; i < jsonEditor.NumberOfObjects(); i++)
        {
            Compound reagentAcc = new Compound();

            reagentAcc.Name           = jsonEditor.GetString(i, "name");
            reagentAcc.Formula        = jsonEditor.GetString(i, "formula");
            reagentAcc.IsSolid        = jsonEditor.GetBool(i, "isSolid");
            reagentAcc.MolarMass      = jsonEditor.GetFloat(i, "molarMass");
            reagentAcc.Purity         = jsonEditor.GetFloat(i, "purity");
            reagentAcc.Density        = jsonEditor.GetFloat(i, "density");
            reagentAcc.Polarizability = jsonEditor.GetFloat(i, "polarizability");
            reagentAcc.compoundColor  = new Color32((byte)jsonEditor.GetInt(i, "compoundColorR"),
                                                    (byte)jsonEditor.GetInt(i, "compoundColorG"),
                                                    (byte)jsonEditor.GetInt(i, "compoundColorB"),
                                                    (byte)255);


            if (!string.IsNullOrEmpty(jsonEditor.GetString(i, "irSpecter")))
            {
                reagentAcc.irSpecter = Resources.Load <Texture2D> ("specter/" + jsonEditor.GetString(i, "irSpecter"));
            }
            else
            {
                reagentAcc.irSpecter = null;
            }

            reagentAcc.Conductibility = jsonEditor.GetFloat(i, "conductibility" + i.ToString());
            reagentAcc.Solubility     = jsonEditor.GetFloat(i, "solubility");
            reagentAcc.FumeHoodOnly   = jsonEditor.GetBool(i, "fumeHoodOnly");

            //reagentAcc.color = new Color (jsonEditor.GetFloat ("colorR"+ i.ToString ()), jsonEditor.GetFloat ("colorG"+ i.ToString ()), jsonEditor.GetFloat ("colorB"+ i.ToString ()), jsonEditor.GetFloat ("colorA"+ i.ToString ()));

            //!Gets the liquid-related variables
            if (!jsonEditor.GetBool(i, "isSolid"))
            {
                reagentAcc.PH           = jsonEditor.GetFloat(i, "ph");
                reagentAcc.Turbidity    = jsonEditor.GetFloat(i, "turbidity");
                reagentAcc.Refratometer = jsonEditor.GetFloat(i, "refratometer");
                reagentAcc.FlameSpecter = jsonEditor.GetFloat(i, "flameSpecter");
                if (!string.IsNullOrEmpty(jsonEditor.GetString(i, "uvSpecter")))
                {
                    reagentAcc.uvSpecter = Resources.Load <Texture2D> ("specter/" + jsonEditor.GetString(i, "uvSpecter"));
                }
                else
                {
                    reagentAcc.uvSpecter = null;
                }

                if (!string.IsNullOrEmpty(jsonEditor.GetString(i, "hplc")))
                {
                    reagentAcc.hplc = Resources.Load <Texture2D> ("specter/" + jsonEditor.GetString(i, "hplc"));
                }
                else
                {
                    reagentAcc.hplc = null;
                }
            }

            reagentAcc.Molarity = ((reagentAcc.Purity * reagentAcc.Density) / reagentAcc.MolarMass) * 1000;             // number of mols / volume
//			Debug.Log (reagentAcc.Formula + ".Molarity = " + reagentAcc.Molarity);
            reagents.Add(reagentAcc.Name, reagentAcc);
        }
        return(reagents);
    }