﻿using System;
using System.Collections.Generic;

namespace GenderPayGap.Extensions
{
    public static class ColorConsole
    {
        private static object _ConsoleLock = new object();

        public static void WriteLine(IEnumerable<string> lines, ConsoleColor? foregroundColor, ConsoleColor? backgroundColor = null)
        {
            lock (_ConsoleLock)
            {
                if (foregroundColor != null) System.Console.ForegroundColor = foregroundColor.Value;
                if (backgroundColor != null) System.Console.BackgroundColor = backgroundColor.Value;

                foreach (var line in lines)
                    System.Console.WriteLine(line);

                if (foregroundColor != null || backgroundColor!=null) System.Console.ResetColor();
            }
        }
        public static void WriteLine(string line, ConsoleColor? foregroundColor=null, ConsoleColor? backgroundColor = null)
        {
            lock (_ConsoleLock)
            {
                if (foregroundColor != null) System.Console.ForegroundColor = foregroundColor.Value;
                if (backgroundColor != null) System.Console.BackgroundColor = backgroundColor.Value;

                System.Console.WriteLine(line);

                if (foregroundColor != null || backgroundColor != null) System.Console.ResetColor();
            }
        }

        public static void WriteError(IEnumerable<string> lines, ConsoleColor? foregroundColor=null, ConsoleColor? backgroundColor = null)
        {
            lock (_ConsoleLock)
            {
                if (foregroundColor == null) System.Console.ForegroundColor = ConsoleColor.Red;
                if (backgroundColor == null) System.Console.BackgroundColor = ConsoleColor.Black;

                foreach (var line in lines)
                    System.Console.Error.WriteLine(line);

                if (foregroundColor != null || backgroundColor != null) System.Console.ResetColor();
            }
        }
        public static void WriteError(string line, ConsoleColor? foregroundColor=null, ConsoleColor? backgroundColor = null)
        {
            lock (_ConsoleLock)
            {
                if (foregroundColor == null) System.Console.ForegroundColor = ConsoleColor.Red;
                if (backgroundColor == null) System.Console.BackgroundColor = ConsoleColor.Black;

                System.Console.Error.WriteLine(line);

                if (foregroundColor != null || backgroundColor != null) System.Console.ResetColor();
            }
        }

        public static void Write(string line, ConsoleColor? foregroundColor, ConsoleColor? backgroundColor = null)
        {
            lock (_ConsoleLock)
            {
                if (foregroundColor != null) System.Console.ForegroundColor = foregroundColor.Value;
                if (backgroundColor != null) System.Console.BackgroundColor = backgroundColor.Value;

                System.Console.Write(line);

                if (foregroundColor != null || backgroundColor != null) System.Console.ResetColor();
            }
        }

        public static void SetColor(ConsoleColor foregroundColor, ConsoleColor? backgroundColor=null)
        {
            lock (_ConsoleLock)
            {
                System.Console.ForegroundColor = foregroundColor;
                if (backgroundColor!=null) System.Console.BackgroundColor = backgroundColor.Value;
            }
        }

        public static void SetForegroundColor(ConsoleColor foregroundColor)
        {
            lock (_ConsoleLock)
            {
                System.Console.ForegroundColor = foregroundColor;
            }
        }

        public static void SetBackgroundColor(ConsoleColor backgroundColor)
        {
            lock (_ConsoleLock)
            {
                System.Console.BackgroundColor = backgroundColor;
            }
        }

        public static void ResetColor()
        {
            lock (_ConsoleLock)
            {
                System.Console.ResetColor();
            }
        }
    }
}
