using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using RothenburgAR.Common;
using RothenburgAR.Exhibition;
using UnityEngine;

namespace RothenburgAR
{
    public class ExhibitionXmlPreloaderTest
    {
        [Test]
        public void CanLoadExhibitionDirectory()
        {
            // TODO: Implement test
        }


        [Test]
        public void PreloadExhibition()
        {
            string t_german = "Hallo Welt";
            string t_english = "Hello World";

            ExhibitionXmlPreloader exhibitionXmlPreloader = new ExhibitionXmlPreloader();

            var testDirectory = PathHelper.GetTemporaryDirectoryPath();
            Assert.IsFalse(exhibitionXmlPreloader.CanLoadExhibitionDirectory(testDirectory));

            string xmlFilePath = PathHelper.GetXmlPathFromDirectoryPath(testDirectory);
            File.WriteAllText(xmlFilePath, "<exhibition id='test-exhibition01'>" +
                                           "    <exhibit id='test-exhibit01'>" +
                                           "        <title>" +
                                           "            <dimensions width='200' height='45'/>" +
                                           "            <font size='40'/>" +
                                           "            <position x='0' y='0' z='0'/>" +
                                           "            <rotation x='0' y='0' z='0'/>" +
                                           "            <text lang='de' type='inline' value='" + t_german + "'/>" +
                                           "            <text lang='en' type='inline' value='" + t_english + "'/>" +
                                           "        </title>" +
                                           "        <description>" +
                                           "            <text lang='de' type='inline' value='" + t_german + "'/>" +
                                           "            <text lang='en' type='inline' value='" + t_english + "'/>" +
                                           "        </description>" +
                                           "        <poiList>" +
                                           "            <poi ref-id='malchus01'/>" +
                                           "            <poi ref-id='malchus02'>" +
                                           "                <position x='0' y='0' z='0'/>" +
                                           "            </poi>" +
                                           "        </poiList>" +
                                           "    </exhibit>" +
                                           "</exhibition>");
            // Write dummy tracker data
            File.WriteAllText(Path.Combine(testDirectory, "tracker.xml"), "dummy");
            File.WriteAllText(Path.Combine(testDirectory, "tracker.dat"), "dummy");

            Assert.IsTrue(exhibitionXmlPreloader.CanLoadExhibitionDirectory(testDirectory));

            PreloadedExhibition p = exhibitionXmlPreloader.PreloadExhibition(testDirectory);
            Assert.AreEqual("test-exhibition01", p.ID);

            Assert.AreEqual(1, p.ContainedExhibits.Count);
            var testExhibit = p.ContainedExhibits[0];
            Assert.AreEqual("test-exhibit01", testExhibit.ID);


            Assert.AreEqual(2, p.ContainedExhibits[0].ReferencedPoiEntries.Count);
            Assert.AreEqual(testExhibit.ReferencedPoiEntries[0].ReferencedId, "malchus01");
            Assert.IsFalse(testExhibit.ReferencedPoiEntries[0].Position.HasValue);
            Assert.IsFalse(testExhibit.ReferencedPoiEntries[0].Rotation.HasValue);
            Assert.IsFalse(testExhibit.ReferencedPoiEntries[0].Scale.HasValue);

            Assert.IsTrue(testExhibit.ReferencedPoiEntries[1].Position.HasValue);
            Assert.IsFalse(testExhibit.ReferencedPoiEntries[1].Rotation.HasValue);
            Assert.IsFalse(testExhibit.ReferencedPoiEntries[1].Scale.HasValue);

            Assert.NotNull(testExhibit.Description);
            Assert.IsTrue(testExhibit.Title.HasValue);

            Assert.AreEqual(40f, testExhibit.Title.Value.FontSize);
            Assert.AreEqual(45f, testExhibit.Title.Value.BoxHeight);
            Assert.AreEqual(200f, testExhibit.Title.Value.BoxWidth);
        }
    }
}