using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;
using System.IO;

namespace Nastavení_PAO
{

    public class FilesInDirectoryArgs : EventArgs{
    
    private List<string> l = new List<string>();

    public FilesInDirectoryArgs(List<string> lX){
    
        this.l = lX;
    
    }

        public List<string> Lx{
        
        get{
        
            return l;
        
        }
        
        }
    
    
    }
    
    public class Profiler
    {

      string folder = string.Empty;

        public Profiler(string folderX) {

            folder = folderX;
        
        }

        public delegate void FilesInDirectory(object sender, FilesInDirectoryArgs e);
        public event FilesInDirectory FilesInDirectoryStatus;

        protected void FilesInDirectoryStatusMake(FilesInDirectoryArgs e) {

            if (FilesInDirectoryStatus != null) {

                FilesInDirectoryStatus(this, e);
            
            }
        
        }

        public string ConvertMemToXml(Dictionary<byte, byte> bb)
        {

            StringWriter sw = new StringWriter();
            XmlWriter xml = XmlWriter.Create(sw);

            xml.WriteStartDocument();
            xml.WriteStartElement("BytesMemory");

            foreach (KeyValuePair<byte, byte> e in bb)
            {

                xml.WriteElementString("Byte_" + e.Key.ToString(), e.Value.ToString());

            }

            xml.WriteEndElement();

            xml.WriteEndDocument();
            xml.Close();

            return sw.ToString();

        }

        public void ConvertXmlToMem(string xml, out Dictionary<byte, byte> reader_tree)
        {
            XmlTextReader reader = new XmlTextReader(new StringReader(xml));
            reader_tree = new Dictionary<byte, byte>();
            
            string key, value;
            key = value = string.Empty;


            while (reader.Read())
            {

                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        { // Tento uzel je prvek.
                            key = reader.Name;
                            if (key != "BytesMemory")
                            {
                                char[] keych = key.ToCharArray();
                                key = string.Empty;
                                for (int i = ("Byte_").Length; i < keych.Length; i++)
                                {

                                    key += keych[i].ToString();

                                }
                            }
                            break;
                        }
                    case XmlNodeType.Text:
                        { //Zobrazení textu v jednotlivých prvcích.

                            if (key != "BytesMemory")
                            {
                            value = reader.Value;
                            reader_tree.Add(Byte.Parse(key), Byte.Parse(value));
                            key = value = string.Empty;

                        }
                            break;
                        }
                    case XmlNodeType.EndElement:
                        {//Zobrazení konce prvku.
                            //Nevím co s tím;
                            break;
                        }
                    case XmlNodeType.Whitespace:
                        {

                            break;

                        }
                }

            }
        
        }

        public bool TestExistFile(string name){

            return File.Exists(folder +"\\"+ name + ".xml");
        
        }

        public void WriteXmlFile(string jmeno, string obsah) {

            if(!Directory.Exists(folder)){

                Directory.CreateDirectory(folder);
            
            }

      
                TextWriter tw = new StreamWriter(folder + "\\" + jmeno + ".xml", false);
                tw.Write(obsah);
                tw.Close();
            
        
        }

        public string ReadXmlFile(string jmeno)
        {

            TextReader tr = new StreamReader(folder + "\\" + jmeno + ".xml", false);
            string text = tr.ReadToEnd();
            tr.Close();
            return text;

        }

        public void SearchFiles() {

            if (!Directory.Exists(folder)) {

                Directory.CreateDirectory(folder);
            
            }

            List<string> l = new List<string>();

            string[] filePaths = Directory.GetFiles(folder, "*.xml", SearchOption.TopDirectoryOnly);

            if(filePaths.Length > 0){

            for (int i = 0; i < filePaths.Length; i++) {

                FileInfo fi = new FileInfo(filePaths[i]);
                
                string temp = fi.Name;
                string[] strs = temp.Split('.');
                l.Add(strs[0]);
            }

            }
            FilesInDirectoryStatusMake(new FilesInDirectoryArgs(l));
        }

        public void DeleteFile(string name) {

            if (File.Exists(folder + "\\" + name + ".xml"))
            {

                File.Delete(folder + "\\" + name + ".xml");
                
            }

            SearchFiles();
        }

        public void RenameFile(string name, string oldname) {

            if (File.Exists(folder + "\\" + oldname + ".xml"))
            {
                File.Copy(folder + "\\" + oldname + ".xml", folder + "/" + name + ".xml", true);
                File.Delete(folder + "\\" + oldname + ".xml");

            }

            SearchFiles();
        
        }

    }
}
