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

namespace Pepyaka
{
	public class Gsom
	{
		private static string[] bbcolors;

		private static Random rnd;

		private static string join_lines;

		public List<string> Phrases { get; set; }

		public List<string> URLs { get; set; }

		public List<string> MultiLineMessages { get; set; }

		static Gsom()
		{
			bbcolors = new string[5] { "red", "blue", "yellow", "green", "purple" };
			rnd = new Random();
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < 15; i++)
			{
				stringBuilder.Append("\r\n");
			}
			join_lines = stringBuilder.ToString();
		}

		public Gsom()
		{
		}

		public Gsom(string lib_file, Encoding encoding)
		{
			LoadLibrary(lib_file, encoding);
		}

		public void LoadLibrary(string lib_file, Encoding encoding)
		{
			Phrases = new List<string>();
			URLs = new List<string>();
			MultiLineMessages = new List<string>();
			using StreamReader streamReader = new StreamReader(lib_file, encoding);
			StringBuilder stringBuilder = null;
			do
			{
				string text = streamReader.ReadLine();
				if (stringBuilder != null)
				{
					if (text.TrimEnd().EndsWith("}"))
					{
						MultiLineMessages.Add(stringBuilder.ToString());
						stringBuilder = null;
					}
					else
					{
						stringBuilder.AppendLine(text);
					}
					continue;
				}
				if (text.TrimStart().StartsWith("{"))
				{
					stringBuilder = new StringBuilder();
					continue;
				}
				text = text.Trim();
				if (text.Length != 0)
				{
					if (text.StartsWith("http"))
					{
						URLs.Add(text);
					}
					else
					{
						Phrases.Add(text);
					}
				}
			}
			while (!streamReader.EndOfStream);
		}

		public string Generate(int capacity)
		{
			return Generate(capacity, GsomFormats.Default);
		}

		public string Generate(int capacity, GsomFormats format)
		{
			MemoryStream memoryStream = new MemoryStream(capacity);
			Generate(capacity, format, memoryStream, Encoding.Default);
			string @string = Encoding.Default.GetString(memoryStream.ToArray());
			memoryStream.Close();
			return @string;
		}

		public void Generate(int capacity, GsomFormats format, Stream stream, Encoding encoding)
		{
			if (Phrases.Count == 0)
			{
				return;
			}
			StreamWriter streamWriter = new StreamWriter(stream, encoding);
			if ((format & GsomFormats.UseMultiLineMessages) == GsomFormats.UseMultiLineMessages && MultiLineMessages.Count > 0 && rnd.Next(0, 2) == 0)
			{
				int index = rnd.Next(0, MultiLineMessages.Count);
				if (MultiLineMessages[index].Length <= capacity)
				{
					streamWriter.Write(MultiLineMessages[index]);
				}
			}
			else
			{
				int num = 0;
				while (true)
				{
					string text;
					if ((format & GsomFormats.AddURLs) == GsomFormats.AddURLs && URLs.Count > 0 && rnd.Next(0, 7) == 0)
					{
						text = URLs[rnd.Next(0, URLs.Count)];
						if ((format & GsomFormats.UseBBCodes) == GsomFormats.UseBBCodes)
						{
							text = ((!text.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) && !text.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase) && !text.EndsWith(".gif", StringComparison.InvariantCultureIgnoreCase) && !text.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) && !text.EndsWith(".bmp", StringComparison.InvariantCultureIgnoreCase) && !text.EndsWith(".wmf", StringComparison.InvariantCultureIgnoreCase)) ? string.Format("[URL={0}]{0}[/URL]", text) : $"[IMG]{text}[/IMG]");
						}
					}
					else
					{
						text = Phrases[rnd.Next(0, Phrases.Count)];
						if ((format & GsomFormats.RandomCase) == GsomFormats.RandomCase)
						{
							char[] array = text.ToCharArray();
							for (int i = 0; i < array.Length; i++)
							{
								char c = array[i];
								if (rnd.Next(0, 2) == 0 && char.IsLetter(c))
								{
									array[i] = (char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c));
								}
							}
							text = new string(array);
							array = null;
						}
						if ((format & GsomFormats.UseBBCodes) == GsomFormats.UseBBCodes)
						{
							string arg = bbcolors[rnd.Next(0, bbcolors.Length)];
							text = $"[COLOR={arg}]{text}[/COLOR]";
						}
					}
					num += text.Length + 1;
					if (num > capacity)
					{
						break;
					}
					streamWriter.Write(text);
					if ((format & GsomFormats.InsertExtraLines) == GsomFormats.InsertExtraLines)
					{
						streamWriter.Write(join_lines);
					}
					else
					{
						streamWriter.Write(" ");
					}
				}
			}
			streamWriter.Dispose();
		}
	}
}
