/// <summary>
 /// Loads the selected dialog and returns the text from it.
 /// </summary>
 /// <returns></returns>
 public static string GetDialog()
 {
     if (selectedDialog != null)
     {
         Debug.Log(dialogsList[0].name + " -- " + SelectedDialog.text);
         selectedDialog = dialogsList.Find(dialog => dialog.name == SelectedDialog.text);
         Debug.Log(selectedDialog);
         Debug.Log("abc " + selectedDialog.Content(selectedLanguage));
         return(selectedDialog.Content(selectedLanguage));
     }
     return("");
 }
        //public static List<string> languageOptions = new List<string>();
        //public static List<string> dialogOptions = new List<string>();

        public static void Init()
        {
            if (!AssetDatabase.IsValidFolder(languagesPath))
            {
                AssetDatabase.CreateFolder(languagesPath.Replace("/Languages", ""), "Languages");
            }
            if (!AssetDatabase.IsValidFolder(dialogsPath))
            {
                AssetDatabase.CreateFolder(dialogsPath.Replace("/Dialogs", ""), "Dialogs");
            }
            SelectedDialog.index   = 0;
            SelectedDialog.text    = "";
            SelectedLanguage.index = 0;
            SelectedLanguage.text  = "";
            dialogsList            = new List <BaseDialog>();
            languagesList          = new List <Language>();
            string[] assetsGUIDs = AssetDatabase.FindAssets("t:BaseDialog", new[] { "Assets/LocalizationTool/Dialogs" });
            foreach (string st in assetsGUIDs)
            {
                dialogsList.Add((BaseDialog)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(st), typeof(BaseDialog)));
            }

            string[] langGUIDs = AssetDatabase.FindAssets("t:Language", new[] { "Assets/LocalizationTool/Languages" });
            foreach (string st in langGUIDs)
            {
                languagesList.Add((Language)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(st), typeof(Language)));
            }

            if (languagesList.Count > 0)
            {
                selectedLanguage      = languagesList[0];
                SelectedLanguage.text = selectedLanguage.LanguageName;
            }
            else
            {
                Debug.LogWarning("Localization Manager: No languages available yet, please add one or multiple");
            }

            if (dialogsList.Count > 0)
            {
                selectedDialog      = dialogsList[0];
                SelectedDialog.text = selectedDialog.name;
            }
            else
            {
                Debug.LogWarning("Localization Manager: No dialogs available yet");
            }

            Debug.Log("Localization Manager: Initialized");
            Debug.Log("Localization Manager: Text Object Found.");

            translatedText = GetDialog();
        }
        /// <summary>
        /// Adding a language with the given string parameter as name.
        /// </summary>
        /// <param name="languageName"></param>
        public static void AddDialog(string dialogName, string dialogText)
        {
            if (translatedText != "" || dialogsList.Count == 0)
            {
                BaseDialog newDialog = (BaseDialog)ScriptableObject.CreateInstance("BaseDialog");
                if (newDialog.dialogWrapper == null)
                {
                    newDialog.dialogWrapper = new List <BaseDialogWrapper>();
                }
                foreach (Language lang in languagesList)
                {
                    if (lang.LanguageName == selectedLanguage.LanguageName)
                    {
                        BaseDialogWrapper newDialogWrapper = new BaseDialogWrapper
                        {
                            content  = dialogText,
                            language = lang
                        };
                        Debug.Log(dialogText + " -- " + newDialogWrapper);
                        newDialog.dialogWrapper.Add(newDialogWrapper);
                    }
                    else
                    {
                        BaseDialogWrapper newDialogWrapper = new BaseDialogWrapper
                        {
                            content  = "",
                            language = lang
                        };
                        Debug.Log(lang + " -- " + selectedLanguage);
                        newDialog.dialogWrapper.Add(newDialogWrapper);
                    }
                }

                dialogsList.Add(newDialog);
                AssetDatabase.CreateAsset(newDialog, dialogsPath + "/Dialog_" + (dialogsList.Count - 1) + ".asset");
                Debug.Log(dialogText + " -- " + newDialog);

                dialogName     = "Dialog_" + (dialogsList.Count - 1);
                selectedDialog = newDialog;
                //dialogOptions.Add(dialogName);
                SelectedDialog.text  = dialogName;
                SelectedDialog.index = dialogsList.Count - 1;
                //totalDialog += 1;
                Debug.Log("Localization Manager: Added new dialog: " + dialogName + " - " + SelectedDialog.index);
            }
        }
 /// <summary>
 /// Load the next text dialog. If the current selected dialog is the last element, the first text dialog will be selected.
 /// </summary>
 public static void NextDialog()
 {
     SetDialog();
     SelectedDialog.index += 1;
     Debug.Log(SelectedDialog.index + " -- " + (dialogsList.Count - 1));
     if (SelectedDialog.index > dialogsList.Count - 1)
     {
         AddDialog("Dialog-" + dialogsList.Count, "");
         //SelectedDialog.index -= 1;
     }
     else
     {
         SelectedDialog.text = dialogsList[SelectedDialog.index].name;
         selectedDialog      = dialogsList[SelectedDialog.index];
     }
     translatedText = GetDialog();
     Debug.Log("Localization Manager: Next Dialog Selected: " + SelectedDialog.text);
 }
        /// <summary>
        /// Loading the text from a JSON file.
        /// </summary>
        public static void LoadText()
        {
            string[] optionsGUIDs = AssetDatabase.FindAssets("t:SystemOptions", new[] { "Assets/LocalizationTool" });

            dialogsList = JSONSaver.LoadDictionary((SystemOptions)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(optionsGUIDs[0]), typeof(SystemOptions)));
            foreach (BaseDialog dia in dialogsList)
            {
                Debug.LogWarning(dia.name + " -- " + dia.dialogWrapper[0].language.LanguageName + " // " + dia.dialogWrapper[0].content);
            }

            languagesList = new List <Language>();
            foreach (BaseDialogWrapper lang in dialogsList[0].dialogWrapper)
            {
                //string[] langGUIDs = AssetDatabase.FindAssets(lang.language.name + " t:Language", new[] { "Assets/LocalizationTool/Languages" });
                //if (langGUIDs.Length == 0)
                //{
                //    AssetDatabase.CreateAsset(lang.language, languagesPath + "/" + lang.language.name + ".asset");
                //    //savedDialog = (BaseDialog)AssetDatabase.LoadAssetAtPath(dialogsPath + "/" + dia.name + ".asset", typeof(BaseDialog));
                //} else
                //{
                //    //load
                //}
                //languagesList = (Language)AssetDatabase.LoadAssetAtPath(languagesPath + "/" + w.language.name + ".asset", typeof(Language));
                Language language = ScriptableObject.CreateInstance("Language") as Language;
                language.name         = "Language_" + lang.language.LanguageName;
                language.LanguageName = lang.language.LanguageName;
                languagesList.Add(language);
            }
            foreach (Language l in languagesList)
            {
                Debug.LogWarning(l.name + " || " + l.LanguageName);
            }

            selectedLanguage       = languagesList[0];
            SelectedLanguage.text  = languagesList[0].LanguageName;
            SelectedLanguage.index = 0; //SelectedLanguage.index = languageOptions.Count - 1;

            selectedDialog       = dialogsList[0];
            SelectedDialog.index = 0;
            SelectedDialog.text  = dialogsList[0].name;
            translatedText       = GetDialog();
            //AddDialog("", "");
            //Debug.Log("Localization Manager: Added new language: " + languageName);
        }
        public static List <BaseDialog> LoadDictionary(SystemOptions options)
        {
            //DataContainer languages = new DataContainer();

            //foreach (BaseDialogWrapper language in dialogsList[0].dialogWrapper)
            //{
            //    DoubleDictionaryContainer languageData = new DoubleDictionaryContainer
            //    {
            //        Language = language.language.LanguageName
            //    };


            //    foreach (BaseDialog dialog in dialogsList)
            //    {
            //        DictionaryContainer dialogData = new DictionaryContainer
            //        {
            //            DialogName = dialog.name,
            //            DialogContent = dialog.dialogWrapper.Find(l => l.language.LanguageName == language.language.LanguageName).content
            //        };
            //        languageData.Dialogs.Add(dialogData);
            //    }
            //    languages.AvailableLanguages.Add(languageData);
            //}



            //Finally write the json string to a file
            string filePath = Application.dataPath + "/Resources/Localization/LocalizationData.json";
            string jsonData = File.ReadAllText(filePath);
            //Convert object to JSON
            DataContainer languages = JsonUtility.FromJson <DataContainer>(jsonData);

            List <BaseDialog> dialogsList = new List <BaseDialog>();

            //foreach(DictionaryContainer dia in languages.AvailableLanguages[0].Dialogs)
            //{
            //    BaseDialog dialog = new BaseDialog();
            //    foreach (DoubleDictionaryContainer lang in languages.AvailableLanguages)
            //    {
            //        BaseDialogWrapper wrapper = new BaseDialogWrapper();
            //        wrapper.language.LanguageName = lang.Language;
            //        wrapper.content = lang.Dialogs[0].DialogContent;
            //    }
            //    dialogsList.Add(dialog);
            //}

            for (int i = 0; i < languages.AvailableLanguages[0].Dialogs.Count; i++)
            {
                BaseDialog dialog = (BaseDialog)ScriptableObject.CreateInstance("BaseDialog");
                dialog.Init(options);
                //Debug.LogWarning("Options is: " + dialog.gameOptions);
                dialog.name          = languages.AvailableLanguages[0].Dialogs[i].DialogName;
                dialog.dialogWrapper = new List <BaseDialogWrapper>();
                foreach (DoubleDictionaryContainer lang in languages.AvailableLanguages)
                {
                    BaseDialogWrapper wrapper = new BaseDialogWrapper();
                    wrapper.language              = (Language)ScriptableObject.CreateInstance("Language");
                    wrapper.language.name         = "Language_" + lang.Language;
                    wrapper.language.LanguageName = lang.Language;
                    wrapper.content = lang.Dialogs[i].DialogContent;
                    dialog.dialogWrapper.Add(wrapper);
                    Debug.LogWarning("Languages is: " + wrapper.language);
                    Debug.LogWarning("Dialog is: " + wrapper.content);
                }
                dialogsList.Add(dialog);
            }
            Debug.LogError("Languages is: " + dialogsList[0].dialogWrapper[0].language);
            Debug.LogError("Dialog is: " + dialogsList[0].dialogWrapper[0].content);
            Debug.LogError(dialogsList[0].Content());
            Debug.Log("JSONSaver: Text loaded from JSON!");

            return(dialogsList);
        }