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

namespace Ooganizer.Platform.Utilities
{
    public class FileM
    {
        private string m_sFileName;
        private string m_sFileType;
        private string m_sFileDirPath;

        //Flags                
        private bool m_bIsFileCreated = false;
        private bool m_bOverideExisting = true;
        private bool m_bDeleteOnDestruct = false;

        // Creates Temporary File by Default
        public FileM(string FileType)
        {
            m_sFileName = Path.GetRandomFileName();
            m_sFileType = FileType;
            m_sFileDirPath = Path.GetTempPath();
            m_bDeleteOnDestruct = true;
        }

        public FileM(string FileType, bool bDontDeleteOnDestruct)
        {
            m_sFileName = Path.GetRandomFileName();
            m_sFileType = FileType;
            m_sFileDirPath = Path.GetTempPath();
            m_bDeleteOnDestruct = !bDontDeleteOnDestruct;
        }

        public FileM(string FileName, string FileType, string DirectoryPath, bool OverideExisting)
        {            
            m_sFileName = FileName;
            m_sFileType = FileType;
            if (!Directory.Exists(DirectoryPath))
            {
                Directory.CreateDirectory(DirectoryPath);
            }
            m_sFileDirPath = DirectoryPath + "\\";
            m_bOverideExisting = OverideExisting;
        }

        ~FileM()
        {
            if (m_bDeleteOnDestruct)
            {
                File.Delete(this.PathNFile);
            }
        }

        public void WriteLineA(string line)
        {
            FileStream fs = CreateFile();
            StreamWriter sw = new StreamWriter(fs, Encoding.ASCII);
            sw.WriteLine(line);
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        public void WriteLineA(string[] lines)
        {
            FileStream fs = CreateFile();
            StreamWriter sw = new StreamWriter(fs, Encoding.ASCII);

            foreach (string line in lines)
            {
                sw.WriteLine(line);
            }

            sw.Flush();
            sw.Close();
            fs.Close();
        }

        public void WriteLineUTF8(string line)
        {
            FileStream fs = CreateFile();
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            sw.WriteLine(line);
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        public void WriteLineUTF8(string[] lines)
        {
            FileStream fs = CreateFile();
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

            foreach (string line in lines)
            {
                sw.WriteLine(line);
            }

            sw.Flush();
            sw.Close();
            fs.Close();
        }

        public void DeleteIfExists()
        {
            if (File.Exists(this.PathNFile))
            {
                File.Delete(this.PathNFile);
            }
        }

        private FileStream CreateFile()
        {
            if (!m_bIsFileCreated && m_bOverideExisting)
            {
                m_bIsFileCreated = true;
                return (new FileStream(this.PathNFile, FileMode.Create));
            }
            else
            {
                return (new FileStream(this.PathNFile, FileMode.Append));
            }
        }

        // public properties
        public string FileName
        {
            get
            {
                return (this.FileName);
            }
        }

        public string DirectoryPath
        {
            get
            {
                return (this.m_sFileDirPath);
            }
        }

        public string PathNFile
        {
            get
            {
                return (m_sFileDirPath + m_sFileName + "." + m_sFileType);
            }
        }

    }
}
