using Logger.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;

namespace Logger.Services
{
    public class MonLogger : IMonLogger 
    {
        public  MonLogger()
        {

        }

        public async void Write(Queue<string> queueAction) 
        {
            if (Monitor.TryEnter(queueAction))
            {
                try
                {
                    while (queueAction.Count > 0) 
                    { 
                        string docPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
                        using StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "Log_aoao.txt"), true);
                        await outputFile.WriteAsync(queueAction.Dequeue() + Environment.NewLine);
                    }
                }
                finally
                {
                    Monitor.Exit(queueAction);
                }
            }
        }

        public void AddAction(string action, ref Queue<string> queueAction) 
        {
                if (Monitor.TryEnter(queueAction))
                {
                    try
                    {
                       queueAction.Enqueue(action);
                    }
                    finally
                    {
                        Monitor.Exit(queueAction);
                    }
                }
        }
    }
}
