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

namespace ZKuP
{
    internal static class FileHandler
    {
        private static ReaderWriterLockSlim _readWriteLock = new ReaderWriterLockSlim();

        public static void WriteToFileThreadSafe(string path, string text)
        {
            // Set Status to Locked
            _readWriteLock.EnterWriteLock();
            try
            {
                // Append text to the file
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.WriteLine(text);
                    sw.Close();
                }
            }
            finally
            {
                // Release lock
                _readWriteLock.ExitWriteLock();
            }
        }

        public static string ReadFromFileThreadSafe(string path)
        {
            var result = "";
            // Set Status to Locked
            _readWriteLock.EnterReadLock();
            try
            {
                // Append text to the file
                result = File.ReadAllText(path);
            }
            finally
            {
                // Release lock
                _readWriteLock.ExitReadLock();
            }

            return result;
        }
    }
}

