using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace pcd_g1
{
	/// <summary>
	/// 画像の読み込み
	/// </summary>
	static class ImageManager
	{
		static Dictionary<string, Bitmap> buf;
		static string IMG_DIR;

		public static void Init()
		{
			IMG_DIR = System.Windows.Forms.Application.StartupPath + @"\imgs";
			buf = new Dictionary<string, Bitmap>();

			if(System.IO.Directory.Exists(IMG_DIR))
			{
				foreach (string f in System.IO.Directory.GetFiles(IMG_DIR))
				{
					try
					{
						var name = f.Substring(f.LastIndexOf(@"\") + 1);
						var b = new Bitmap(f);
						buf.Add(name, b);
					}
					catch (Exception)
					{
#if TRACE
						Debug.WriteLine("画像ファイル読み込みエラー:" + f);
#endif
					}
				}
			}
		}

		public static Bitmap Search(string name)
		{
			string word;
			if (buf.ContainsKey(name))
			{
				word = name;
			}
			else if (buf.ContainsKey(name + ".gif"))
			{
				word = name + ".gif";
			}
			else if (buf.ContainsKey(name + ".bmp"))
			{
				word = name + ".bmp";
			}
			else
			{
				throw new ApplicationException("データが見つかりません: " + name);
			}
			return buf[word];
		}
	}
}
