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

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

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

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

	public Gsom()
	{
	}

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

	public void LoadLibrary(string lib_file, Encoding encoding)
	{
		this.Phrases = new List<string>();
		this.URLs = new List<string>();
		this.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(new char[0]).EndsWith("}"))
					{
						this.MultiLineMessages.Add(stringBuilder.ToString());
						stringBuilder = null;
					}
					else
					{
						stringBuilder.AppendLine(text);
					}
				}
				else if (text.TrimStart(new char[0]).StartsWith("{"))
				{
					stringBuilder = new StringBuilder();
				}
				else
				{
					text = text.Trim();
					if (text.Length != 0)
					{
						if (text.StartsWith("http"))
						{
							this.URLs.Add(text);
						}
						else
						{
							this.Phrases.Add(text);
						}
					}
				}
			}
			while (!streamReader.EndOfStream);
		}
	}

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

	public string Generate(int capacity, GsomFormats format)
	{
		MemoryStream memoryStream = new MemoryStream(capacity);
		this.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 (this.Phrases.Count == 0)
		{
			return;
		}
		StreamWriter streamWriter = new StreamWriter(stream, encoding);
		int index = Gsom.rnd.Next(0, this.MultiLineMessages.Count);
		if ((format & GsomFormats.UseMultiLineMessages) == GsomFormats.UseMultiLineMessages && this.MultiLineMessages.Count > 0 && Gsom.rnd.Next(0, 2) == 0 && this.MultiLineMessages[index].Length <= capacity)
		{
			streamWriter.Write(this.MultiLineMessages[index]);
		}
		else
		{
			int num = 0;
			for (;;)
			{
				string text;
				if ((format & GsomFormats.AddURLs) == GsomFormats.AddURLs && this.URLs.Count > 0 && Gsom.rnd.Next(0, 7) == 0)
				{
					text = this.URLs[Gsom.rnd.Next(0, this.URLs.Count)];
					if ((format & GsomFormats.UseBBCodes) == GsomFormats.UseBBCodes)
					{
						if (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))
						{
							text = string.Format("[IMG]{0}[/IMG]", text);
						}
						else
						{
							text = string.Format("[URL={0}]{0}[/URL]", text);
						}
					}
				}
				else
				{
					text = this.Phrases[Gsom.rnd.Next(0, this.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 (Gsom.rnd.Next(0, 2) == 0 && char.IsLetter(c))
							{
								array[i] = (char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c));
							}
						}
						text = new string(array);
					}
					if ((format & GsomFormats.UseBBCodes) == GsomFormats.UseBBCodes)
					{
						string arg = Gsom.bbcolors[Gsom.rnd.Next(0, Gsom.bbcolors.Length)];
						text = string.Format("[COLOR={0}]{1}[/COLOR]", arg, text);
					}
				}
				num += text.Length + 1;
				if (num > capacity)
				{
					break;
				}
				streamWriter.Write(text);
				if ((format & GsomFormats.InsertExtraLines) == GsomFormats.InsertExtraLines)
				{
					streamWriter.Write("\r\n\r\n");
				}
				else
				{
					streamWriter.Write(" ");
				}
			}
		}
		streamWriter.Dispose();
	}

	private static string[] bbcolors = new string[]
	{
		"red",
		"blue",
		"yellow",
		"green",
		"purple"
	};

	private static volatile Random rnd = new Random();
}
