using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace SHMD
{
    public class SHMDFile
    {
        public String LoadPath;

        public String FileHeader;
        public List<String> Sky = new List<String>();
        public List<String> Water = new List<String>();
        public List<String> GroundObject = new List<String>();
        public List<String> GlobalLight = new List<String>();
        public List<String> Fog = new List<String>();
        public List<String> BackGroundColor = new List<String>();
        public String Frustum;
        public Dictionary<String, List<String>> Blocks = new Dictionary<String, List<String>>();
        public List<String> DirectionLightAmbient = new List<String>();
        public List<String> DirectionLightDiffuse = new List<String>();

        public SHMDFile(String LP) { LoadPath = LP; }

        public void Read()
        {
            StreamReader SHMDReader = new StreamReader(File.OpenRead(LoadPath));

            FileHeader = SHMDReader.ReadLine();

            Int32 TotalSky = Convert.ToInt32(SHMDReader.ReadLine().Split(' ')[1]);

            for (Int32 Counter = 0; Counter < TotalSky; Counter++) { Sky.Add(SHMDReader.ReadLine()); }

            Int32 TotalWater = Convert.ToInt32(SHMDReader.ReadLine().Split(' ')[1]);

            for (Int32 Counter = 0; Counter < TotalWater; Counter++) { Water.Add(SHMDReader.ReadLine()); }

            Int32 TotalGroundObject = Convert.ToInt32(SHMDReader.ReadLine().Split(' ')[1]);

            for (Int32 Counter = 0; Counter < TotalGroundObject; Counter++) { GroundObject.Add(SHMDReader.ReadLine()); }

            String[] GlobalLightLine = SHMDReader.ReadLine().Split(' ');

            for (Int32 Counter = 1; Counter < GlobalLightLine.Length; Counter++) { GlobalLight.Add(GlobalLightLine[Counter]); }

            String[] FogLine = SHMDReader.ReadLine().Split(' ');

            for (Int32 Counter = 1; Counter < FogLine.Length; Counter++) { Fog.Add(FogLine[Counter]); }

            String[] BackGroundColorLine = SHMDReader.ReadLine().Split(' ');

            for (Int32 Counter = 1; Counter < BackGroundColorLine.Length; Counter++) { BackGroundColor.Add(BackGroundColorLine[Counter]); }

            Frustum = SHMDReader.ReadLine().Split(' ')[1];

            String CurrentLine;

            while ((CurrentLine = SHMDReader.ReadLine()) != "DataObjectLoadingEnd")
            {
                List<String> Coordinates = new List<String>();

                String BlockIdentifier = CurrentLine.Split(' ')[0];

                Int32 TotalCoordinates = Convert.ToInt32(CurrentLine.Split(' ')[1]);

                for (Int32 Counter = 0; Counter < TotalCoordinates; Counter++)
                {
                    CurrentLine = SHMDReader.ReadLine();

                    Coordinates.Add(CurrentLine);
                }

                Blocks.Add(BlockIdentifier, Coordinates);
            }

            String[] DirectionLightAmbientLine = SHMDReader.ReadLine().Split(' ');

            for (Int32 Counter = 1; Counter < DirectionLightAmbientLine.Length; Counter++) { DirectionLightAmbient.Add(DirectionLightAmbientLine[Counter]); }

            String[] DirectionLightDiffuseLine = SHMDReader.ReadLine().Split(' ');

            for (Int32 Counter = 1; Counter < DirectionLightDiffuseLine.Length; Counter++) { DirectionLightDiffuse.Add(DirectionLightDiffuseLine[Counter]); }

            SHMDReader.Dispose();
        }

        public void Write(String WritePath)
        {
            if (WritePath == LoadPath)
            {
                if (File.Exists(LoadPath))
                {
                    if (File.Exists(String.Format("{0}.bak", LoadPath))) { File.Delete(String.Format("{0}.bak", LoadPath)); }

                    File.Move(LoadPath, String.Format("{0}.bak", LoadPath));
                }
            }
            else if (File.Exists(WritePath))
            {
                if (File.Exists(WritePath))
                {
                    if (File.Exists(String.Format("{0}.bak", WritePath))) { File.Delete(String.Format("{0}.bak", WritePath)); }

                    File.Move(WritePath, String.Format("{0}.bak", WritePath));
                }
            }

            StreamWriter SHMDWriter = new StreamWriter(File.Create(WritePath));
            SHMDWriter.WriteLine(FileHeader);
            SHMDWriter.WriteLine(String.Format("Sky {0} ", Sky.Count));

            foreach (String SkyNif in Sky) { SHMDWriter.WriteLine(SkyNif); }

            SHMDWriter.WriteLine(String.Format("Water {0} ", Water.Count));

            foreach (String WaterNif in Water) { SHMDWriter.WriteLine(WaterNif); }

            SHMDWriter.WriteLine(String.Format("GroundObject {0} ", GroundObject.Count));

            foreach (String GroundObjectNif in GroundObject) { SHMDWriter.WriteLine(GroundObjectNif); }

            SHMDWriter.WriteLine(String.Format("GlobalLight {0} {1} {2} ", GlobalLight[0], GlobalLight[1], GlobalLight[2]));
            SHMDWriter.WriteLine(String.Format("Fog {0} {1} {2} {3} ", Fog[0], Fog[1], Fog[2], Fog[3]));
            SHMDWriter.WriteLine(String.Format("BackGroundColor {0} {1} {2} ", BackGroundColor[0], BackGroundColor[1], BackGroundColor[2]));
            SHMDWriter.WriteLine(String.Format("Frustum {0} ", Frustum));

            foreach (String Block in Blocks.Keys)
            {
                SHMDWriter.WriteLine(String.Format("{0} {1} ", Block, Blocks[Block].Count));

                foreach (String Coordinate in Blocks[Block]) { SHMDWriter.WriteLine(Coordinate); }
            }

            SHMDWriter.WriteLine("DataObjectLoadingEnd");
            SHMDWriter.WriteLine(String.Format("DirectionLightAmbient {0} {1} {2} ", DirectionLightAmbient[0], DirectionLightAmbient[1], DirectionLightAmbient[2]));
            SHMDWriter.WriteLine(String.Format("DirectionLightDiffuse {0} {1} {2} ", DirectionLightDiffuse[0], DirectionLightDiffuse[1], DirectionLightDiffuse[2]));
            SHMDWriter.Flush();
            SHMDWriter.Dispose();

            LoadPath = WritePath;
        }
    }
}
