using System.IO;
using System.Collections.Generic;
using XMLParser.Contract;
using XMLParser.Model;
using System;
using System.Text;
using System.Linq;
using System.Diagnostics;

namespace XMLParser.Functions.ProtokollWriter 
{
    public class HTMLProtkollWriter : IProtokollWriter
    {
        FileStream handle = null;
        Dictionary<ECalculatedResult,string> headertitles = null;
        Dictionary<string,string> Entwaesserung = null;
        public HTMLProtkollWriter(Dictionary<ECalculatedResult, string> headertitles)
        {
            Entwaesserung = new Dictionary<string, string>();
            Entwaesserung.Add("KR","Regenwasser");
            Entwaesserung.Add("KS","Schmutzwasser");
            Entwaesserung.Add("KM","Mischwasser");

            handle = File.Create("./data.html");
            this.headertitles = headertitles;
        }
        internal void writeToFile(string content)
        {
            content += Environment.NewLine;
            byte[] bytes = Encoding.UTF8.GetBytes(content);
            handle.Write(bytes,0,bytes.Length);
            handle.Flush();
        }

        void writeHTMLHeader() 
        {
            writeToFile("<html>");
            writeToFile("<head>");
            writeToFile("<style>#subject { margin-top: 120px; margin-left: auto; margin-right: auto; width: 50%; }</style>");
            writeToFile("</head>");
            writeToFile("<body>");
            writeToFile("<div id='root'>");
            writeToFile("<div id='page'>");
            writeToFile("<div id='banner'><img src='briefkopf.png' width='100%' /></div>");
        }

        void writeHTMLFooter()
        {
            writeToFile("</div></div>");
            writeToFile("</body>");
            writeToFile("</html>");
        }

        public void WriteEntry(Dictionary<ECalculatedResult, decimal> calculated, List<KanalObjekt> inspektionenAmTag)
        {
            writeHTMLHeader();
            KanalObjekt objekt = inspektionenAmTag.Last();
            //Debugger.Break();
            writeToFile("<div id='subject'>");
            writeToFile("<h1>"+objekt.Inspektionsdaten.Lage.Strassename+"</h1>");
            writeToFile("<h1>"+Entwaesserung[objekt.Inspektionsdaten.OptischeInspektion.Rohrleitung.Grunddaten.Kanalart]+"</h1>");
            writeToFile("</div>");

            writeToFile("<table border='2'>");
            List<ECalculatedResult> avaibleHeaders = new List<ECalculatedResult>();
            string result = "<tr>";
            foreach(var s in calculated.Keys)
            {
                avaibleHeaders.Add(s);
                result += "<td>"+calculated[s]+ "</td>";
            }
            result += "</tr>";

            makeHeaderTitlesInHtml(avaibleHeaders);
            writeToFile(result);
            writeToFile("</table>");
            
            
        }

        private void makeHeaderTitlesInHtml(List<ECalculatedResult> avaibleHeaders)
        {
            writeToFile("<tr>");
            foreach(ECalculatedResult s in avaibleHeaders) 
            {
                writeToFile(string.Format("<th>{0}</th>",headertitles[s]));
            }
            writeToFile("</tr>");
        }

        public void WriteBaried(string content)
        {

            writeToFile(string.Format("<p>{0}</p>",content));
            writeHTMLFooter();
        }
    }
}