﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Diagnostics;

namespace REDIT
{
    public class LanguageManager
    {
        public Language getLanguage() {
            Console.WriteLine("Checking settings.xml");
            if (!File.Exists(Environment.CurrentDirectory + "/settings.xml")) {
                File.Create(Environment.CurrentDirectory + "/settings.xml");
                Console.WriteLine("Creating settings.xml");
            }

            Console.WriteLine("Load settings.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(Environment.CurrentDirectory + "/settings.xml");
            //Root Knoten in ein XmlElement laden
            Console.WriteLine("Load settings.xml > Root object");
            XmlElement root = doc.DocumentElement;

            foreach (XmlNode daten in root.ChildNodes)
            {
                if (root.SelectSingleNode("Language") == null)
                {
                    Console.WriteLine("Load settings.xml > Language = null");
                    setLanguage(Language.ENGLISH);
                }
                else {
                    return getLanguageByString(root.SelectSingleNode("Language").InnerText);
                } 
            }
            return Language.ENGLISH;

        }

        public Language getLanguageByString(string name)
        {
            switch (name.ToLower()) {
                case "german":
                    return Language.GERMAN;
                case "english":
                    return Language.ENGLISH;
            }

                    setLanguage(Language.ENGLISH);
            return Language.ENGLISH;
        }

        public string getStringByLanguage(Language lang)
        {
            switch (lang)
            {
                case Language.GERMAN:
                    return "german";
                case Language.ENGLISH:
                    return "english";
            }
            return "";
            
        }

        public void setLanguage(Language lang) {
            XmlDocument doc = new XmlDocument();
            Console.WriteLine("Load settings.xml");
            doc.Load(@"settings.xml");
            //Root Knoten in ein XmlElement laden
            Console.WriteLine("Load settings.xml > Root object");
            XmlElement root = doc.DocumentElement;
            root.SelectSingleNode("Language").InnerText = getStringByLanguage(lang);
            Console.WriteLine("Set language: " + getStringByLanguage(lang));

        }

        public string getString(Language lang, string id) {

            Console.WriteLine("Load Lang\\" + getStringByLanguage(lang) + ".xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(@"Lang\" + getStringByLanguage(lang) + ".xml");
            Console.WriteLine("Load Root object");
            XmlElement root = doc.DocumentElement;

            /*Console.WriteLine("Load Foreach XML Node");
            foreach (XmlNode daten in root.ChildNodes)
            {
                Console.WriteLine("Daten: " + daten.Name);
            }
                foreach (XmlNode daten in root.ChildNodes)
            {
                Console.WriteLine("Daten: " + daten.Name);
                if (daten[id] == null)
                {
                    Console.WriteLine("ID: " + id);
                    return id;
                }
                else
                {
                    Console.WriteLine(daten[id].InnerText);
                    return daten[id].InnerText;
                }
            }
            Console.WriteLine("ID...: " + id);
            */

            var node = root.SelectSingleNode(id);
            Console.WriteLine("Text: " + node.InnerText);
            return node.InnerText;
        }


    }
}
