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

namespace PG_Sim
{
    /// <summary>
    /// Class used to handling with files
    /// </summary>
    class File
    {
        /// <summary>
        /// Default value of path to source
        /// </summary>
        private string PATH = Support.settings.basePath;

        public string path
        {
            get { return PATH; }
        } 


        /// <summary>
        /// Raw data waiting for process
        /// </summary>
        private string rawData;
        public string RawData 
        {
            get { return rawData; }
            set { rawData = value; }
        }

        /// <summary>
        /// File name
        /// </summary>
        private string fileName;

        /// <summary>
        /// File extension
        /// </summary>
        private string extension;
        public string Extension 
        {
            get { return extension; }
            set { extension = value; }
        }

        /// <summary>
        /// Immediate loading of file
        /// </summary>
        /// <param name="FileName">Name of file to load</param>
        public File(string FileName) 
        {
            try
            {
                StreamReader handle = new StreamReader(PATH + FileName);
                rawData = handle.ReadToEnd();
                handle.Close();
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File " + FileName + " not found!");
            }
            catch
            {
                Console.WriteLine("Another problem with file " + FileName + " load!");
            }

            // save file name and extension
            this.fileName = FileName;
            this.extension = this.getExtension();
        }

        /// <summary>
        /// Get file extension
        /// </summary>
        /// <returns>Returns lowercase and trimed string</returns>
        private string getExtension() 
        {
            try
            {
                return System.IO.Path.GetExtension(this.fileName).ToLower().Trim('.');
            }
            catch (ArgumentException)
            {
                Console.WriteLine("Error in filename!");
                return "";
            }
        }

        public string getName()
        {
            return System.IO.Path.GetFileNameWithoutExtension(this.fileName);
        }

        static public void Save(string file,string text)
        {
            text = text.Replace(',', '.');
            try
            {
                System.IO.File.Delete("c:/Documents and Settings/dalibor/Dokumenty/MATLAB/"+file);
            }
            catch
            {
                Console.WriteLine("Nepodařilo se smazat soubor "+file);
            }
            // create a writer and open the file
            TextWriter tw = new StreamWriter("c:/Documents and Settings/dalibor/Dokumenty/MATLAB/" + file);

            // write a line of text to the file
            tw.Write(text);

            // close the stream
            tw.Close();
        }
    }
}
