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

namespace XMLParser.Functions
{
    [DebuggerDisplay("{" + nameof(GetDebuggerDisplay) + "(),nq}")]
    public class StrassenUmsatzCSVWriter : CSVWriterBase
    {
        
        public StrassenUmsatzCSVWriter()
        {
            handle = File.Create("./data.csv");
        }

        public void Save()
        {
            handle.Close();
        }

        private string GetDebuggerDisplay()
        {
            return ToString();
        }

        
        public override void WriteEntry(Dictionary<ECalculatedResult, decimal> calculated, List<KanalObjekt> inspektionenAmTag)
        {
            int anzahlStraßenablaufe = (int)calculated[ECalculatedResult.STRASSENABLAUFANZAHL];
            int sonstigeLeitungen = (int)calculated[ECalculatedResult.SONSTIGEANZAHL];
            decimal Strassenablaufzulage = calculated[ECalculatedResult.STRASSENABLAUFLAENGEZULAGEMETER];
            decimal Sonstigezulage = calculated[ECalculatedResult.SONSTIGELAENGEZULAGEMETER];
            
            decimal HauptkanalLänge = calculated[ECalculatedResult.DN150DN250];
            
            string entry = "";
            bool firstentry = true;
            List<ECalculatedResult> AvaibleHeaders = new List<ECalculatedResult>();
            foreach(var s in calculated.Keys) 
            {
                if(firstentry)
                {
                    entry += calculated[s];
                    firstentry = false;
                }
                else
                {
                    entry += "#"+calculated[s];
                }
                AvaibleHeaders.Add(s);
            }
            makeHeader(AvaibleHeaders);
            Debugger.Log(0,"",entry);
            //Debugger.Break();

            KanalObjekt last = inspektionenAmTag.Last();
            
            
            writeToFile(entry);
        }

        private void makeHeader(List<ECalculatedResult> avaibleHeaders)
        {
            Dictionary<ECalculatedResult,string> dict = new Dictionary<ECalculatedResult, string>();
            dict.Add(ECalculatedResult.DN150DN250, "DN150 - DN250 [m]");
            dict.Add(ECalculatedResult.DN300DN400, "DN300 - DN400 [m]");
            dict.Add(ECalculatedResult.DN450DN600, "DN450 - DN600 [m]");
            dict.Add(ECalculatedResult.DN650DN800, "DN650 - DN800 [m]");
            dict.Add(ECalculatedResult.DN850DN1000,"DN850 - DN1000 [m]");
            dict.Add(ECalculatedResult.GESAMTHAUPTKANAL,"Hauptkanal Gesamt [m]");
            dict.Add(ECalculatedResult.STRASSENABLAUFANZAHL, "Straßenablauf [Stk]");
            dict.Add(ECalculatedResult.STRASSENABLAUFLAENGEZULAGEMETER,"Straßenablauf > 5m [m]");
            dict.Add(ECalculatedResult.SONSTIGEANZAHL,"Anschlussleitung [Stk]");
            dict.Add(ECalculatedResult.SONSTIGELAENGEZULAGEMETER,"Anschlussleitung > 7m [m]");

            string header = "";
            bool firstentry = true;
            foreach(ECalculatedResult s in avaibleHeaders)
            {
                if(firstentry)
                {
                    firstentry = false;
                } 
                else 
                {
                    header += "#";
                }
                header += dict[s];
            }
            Debugger.Log(0,"CSVWriter",header);
            writeToFile(header);
        }
    }
}
