using System;
using UnityEngine;
using UnityEditor;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using System.IO;
using RothenburgAR.Common;


namespace RothenburgAR
{
    public class TextElementTest
    {
        [Test]
        public void AddEntriesInline()
        {
            string t_german = "Hallo Welt";
            string t_english = "Hello World";
            TextElement te = new TextElement();
            te.AddTextElement("de", TextEntryType.Inline, t_german);
            te.AddTextElement("en", TextEntryType.Inline, t_english);


            Assert.AreEqual(2, te.EntryAmount);
            Assert.AreEqual(t_german, te.GetTextByLanguage("de"));
            Assert.AreEqual(t_english, te.GetTextByLanguage("en"));
        }

        [Test]
        public void AddEntriesFile()
        {
            var testDirectory = TestContext.CurrentContext.TestDirectory;
            string path_german = Path.GetTempFileName();
            string path_english = Path.GetTempFileName();
            string path_japan = "undefined file.txt";


            string t_german = "Hallo Welt";
            string t_english = "Hello World";

            // Write test files
            // Do not create japan file - so we can test FileNotFoundException
            using (StreamWriter outputFile = new StreamWriter(path_german))
            {
                outputFile.Write(t_german);
                outputFile.Close();
            }
            using (StreamWriter outputFile = new StreamWriter(path_english))
            {
                outputFile.Write(t_english);
                outputFile.Close();
            }


            TextElement te = new TextElement();
            te.AddTextElement("de", TextEntryType.File, path_german);
            te.AddTextElement("en", TextEntryType.File, path_english);

            Assert.AreEqual(2, te.EntryAmount);
            Assert.AreEqual(t_german, te.GetTextByLanguage("de"));
            Assert.AreEqual(t_english, te.GetTextByLanguage("en"));

            // Test for Exception thrown if file does not exist
            te.AddTextElement("jp", TextEntryType.File, path_japan);
            Assert.AreEqual(3, te.EntryAmount);
            Assert.Throws<FileNotFoundException>(delegate { te.GetTextByLanguage("jp"); });
        }

        [Test]
        public void AddEntryInvalid()
        {
            TextElement te = new TextElement();
            te.AddTextElement("de", TextEntryType.Invalid, "TEST");

            Assert.AreEqual(0, te.EntryAmount);
            Assert.AreEqual(null, te.GetTextByLanguage("de"));
        }

        [Test]
        public void AddEntryErrors()
        {
            TextElement te = new TextElement();
            te.AddTextElement("de", TextEntryType.Inline, "TEST");
            Assert.AreEqual(1, te.EntryAmount);

            Assert.Throws<Exception>(delegate { te.AddTextElement("de", TextEntryType.Inline, "TEST"); });
        }

        [Test]
        public void GetTextEntryTypeFromString()
        {
            Assert.AreEqual(TextEntryType.Inline, TextElement.GetTextEntryTypeFromString("inline"));
            Assert.AreEqual(TextEntryType.Inline, TextElement.GetTextEntryTypeFromString("INLINE"));
            Assert.AreEqual(TextEntryType.Inline, TextElement.GetTextEntryTypeFromString("InLiNe"));

            Assert.AreEqual(TextEntryType.File, TextElement.GetTextEntryTypeFromString("file"));
            Assert.AreEqual(TextEntryType.File, TextElement.GetTextEntryTypeFromString("FILE"));
            Assert.AreEqual(TextEntryType.File, TextElement.GetTextEntryTypeFromString("FiLe"));

            Assert.AreEqual(TextEntryType.Invalid, TextElement.GetTextEntryTypeFromString("adasd"));
            Assert.AreEqual(TextEntryType.Invalid, TextElement.GetTextEntryTypeFromString("NIUH"));
            Assert.AreEqual(TextEntryType.Invalid, TextElement.GetTextEntryTypeFromString("%2ius78T"));
        }
    }
}