using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NLog;
using System.IO;                // FileStream

namespace ProcessFile
{
    public class Files
    {
        static Logger log = LogManager.GetLogger("agent_logger");
        // create trc file
        public void CreateFile(String path, byte[] data_buffer)
        {
            try
            {
                using (var fs = new FileStream(path, FileMode.Create))
                {
                    fs.Write(data_buffer, 0, data_buffer.Length);
                    Console.WriteLine("trace file create success");
                    Console.WriteLine();
                }
            } catch (Exception e)
            {
                log.Error(e.ToString());
                Console.WriteLine("trc file create fail");
            }
        }

        // create dat file
        public void CreateFile(String path, String data_buffer)
        {
            try
            {
                using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    StreamWriter writer = new StreamWriter(fs);
                    writer.WriteLine(data_buffer);
                    writer.Flush();
                    writer.Close();
                    Console.WriteLine("data file create success");
                    Console.WriteLine();
                }
            }
            catch (Exception e)
            {
                log.Error(e.ToString());
                Console.WriteLine("data file create fail");
            }
        }

        // open recv trc file and read contents
        public byte[] OpenTrcFile(String recv_path)
        {
            byte[] data_buffer;
            using (var fs = File.Open(recv_path, FileMode.Open, FileAccess.Read)) {
                using (BinaryReader reader = new BinaryReader(fs))
                {
                    data_buffer = new byte[fs.Length];
                    fs.Read(data_buffer, 0, (int)fs.Length);
                }
            }

            return data_buffer;
        }
    }
}
