using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using System.Data;

namespace ResourceService
{
	/// <summary>
	/// Directory.
	/// </summary>
	public class Resources
	{
		/// <summary>
		/// CurrentDirectory
		/// </summary>
		static private string currentDirectory;

		/// <summary>
		/// Control resources
		/// </summary>
		static private Dictionary<string, object> controlResources = new Dictionary<string, object>();

		/// <summary>
		/// Control resources
		/// </summary>
		static private Dictionary<string, object> resources = new Dictionary<string, object>();

		/// <summary>
		/// Current directory
		/// </summary>
		public static string CurrentDirectory
		{
			get
			{
				return currentDirectory;
			}
			set
			{
				currentDirectory = value;
			}
		}

        /// <summary>
        /// Recursively updates UI of control and all its subcontrols
        /// </summary>
        /// <param name="control">The control of UI updating</param>
        public static void UpdateUI(Control control)
        {
            if (control is IUpdatableUI)
            {
                IUpdatableUI up = control as IUpdatableUI;
                up.UpdateUI();
            }
            foreach (Control c in control.Controls)
            {
                UpdateUI(c);
            }
        }



		/// <summary>
		/// Gets parent form
		/// </summary>
		/// <param name="control">Control</param>
		/// <returns>Parent form</returns>
		public static Form GetParentForm(Control control)
		{
			Control c = control;
			while (true)
			{
				if (c is Form)
				{
					return c as Form;
				}
				c = c.Parent;
			}
		}

		/// <summary>
		/// Loads resources from file
		/// </summary>
		/// <param name="filename">Filename</param>
		public static void LoadControlResourcesFromFile(string filename)
		{
			Stream stream = File.OpenRead(filename);
			ResourceReader r = new ResourceReader(stream);
			ResourceSet s = new ResourceSet(r);
			IDictionaryEnumerator d = s.GetEnumerator();
			while (d.MoveNext())
			{
				object k = d.Key;
				object v = d.Value;
				controlResources[k + ""] = v;
			}
			stream.Close();
		}

		/// <summary>
		/// Loads resources from file
		/// </summary>
		/// <param name="filename">Filename</param>
		public static void LoadResources(string filename)
		{
			Stream stream = File.OpenRead(filename);
			ResourceReader r = new ResourceReader(stream);
			ResourceSet s = new ResourceSet(r);
			IDictionaryEnumerator d = s.GetEnumerator();
			while (d.MoveNext())
			{
				object k = d.Key;
				object v = d.Value;
				resources[k + ""] = v;
			}
			stream.Close();
		}

		/// <summary>
		/// Control resources
		/// </summary>
		public static Dictionary<string, object> ControlResources
		{
			get
			{
				return controlResources;
			}
		}

		/// <summary>
		/// Gets control resource
		/// </summary>
		/// <param name="key">Key string</param>
		/// <returns>The resource</returns>
		public static string GetControlResource(string key)
		{
			if (controlResources.ContainsKey(key))
			{
				return controlResources[key] as string;
			}
			return key;
		}

		/// <summary>
		/// Gets resource string
		/// </summary>
		/// <param name="key">Key string</param>
		/// <returns>The resource</returns>
		public static string GetResourceString(string key)
		{
			if (resources.ContainsKey(key))
			{
				return resources[key] as string;
			}
			return key;
		}

		/// <summary>
		/// Loads resources strings to control
		/// </summary>
		/// <param name="control">The control</param>
		public static void LoadControlResources(Control control)
		{
			LoadControlResources(control, controlResources);
		}

		/// <summary>
		/// Loads resources strings to control
		/// </summary>
		/// <param name="control">The control</param>
		/// <param name="resources">The resources</param>
		public static void LoadControlResources(Control control, Dictionary<string, object> resources)
		{
			if (resources == null)
			{
				return;
			}
			if (!(control is TextBox) & !(control is WebBrowser))
			{
				control.Text = ResourceService.Resources.GetControlResource(control.Text);
			}
			foreach (Control c in control.Controls)
			{
				LoadControlResources(c, resources);
			}
			if (control is DataGrid)
			{
				DataGrid grid = control as DataGrid;
				if (grid.DataSource is DataTable)
				{
					DataTable table = grid.DataSource as DataTable;
					foreach (DataColumn col in table.Columns)
					{
						col.ColumnName = ResourceService.Resources.GetControlResource(col.ColumnName);
					}
				}
			}
			if (control is ListView)
			{
				ListView lv = control as ListView;
				foreach (ColumnHeader ch in lv.Columns)
				{
					ch.Text = ResourceService.Resources.GetControlResource(ch.Text);
				}
			}
			if (control is Form)
			{
				Form form = control as Form;
				Menu menu = form.Menu;
				if (menu != null)
				{
					foreach (MenuItem item in menu.MenuItems)
					{
						loadMenuResources(item, resources);
					}
				}
			}
			if (control is TabControl)
			{
				TabControl tc = control as TabControl;
				foreach (TabPage page in tc.TabPages)
				{
					page.Text = ResourceService.Resources.GetControlResource(page.Text);
				}
			}

            if (control is TabControl)
            {
                TabControl tc = control as TabControl;
                foreach (TabPage page in tc.TabPages)
                {
                    page.Text = GetControlResource(page.Text, resources);
                }
            }
            if (control is MenuStrip)
            {
                MenuStrip ms = control as MenuStrip;
                foreach (ToolStripItem it in ms.Items)
                {
                    loadToolMenuItem(it, resources);
                }
            }
            if (control is ToolStrip)
            {
                ToolStrip ts = control as ToolStrip;
                ToolStripItemCollection coll = ts.Items;
                foreach (ToolStripItem c in coll)
                {
                    c.Text = GetControlResource(c.Text, resources);
                }
            }

		}

        /// <summary>
        /// Gets resource string
        /// </summary>
        /// <param name="key">Key string</param>
        /// <param name="resources">Resources</param>
        /// <returns>The resource</returns>
        public static string GetControlResource(string key, Dictionary<string, object> resources)
        {
            if (resources.ContainsKey(key))
            {
                return resources[key] as string;
            }
            return key;
        }

		
		/// <summary>
		/// Loads resources to menu items
		/// </summary>
		/// <param name="item">Parent item</param>
		/// <param name="resources">Resources</param>
		private static void loadMenuResources(MenuItem item, Dictionary<string, object> resources)
		{
			item.Text = ResourceService.Resources.GetControlResource(item.Text);
			foreach (MenuItem it in item.MenuItems)
			{
				loadMenuResources(it, resources);
			}
		}

        static void loadToolMenuItem(ToolStripItem item, Dictionary<string, object> resources)
        {
            item.Text = GetControlResource(item.Text, resources);
            if (item is ToolStripMenuItem)
            {
                ToolStripMenuItem mi = item as ToolStripMenuItem;
                foreach (ToolStripItem it in mi.DropDown.Items)
                {
                    loadToolMenuItem(it, resources);
                }
            }
        }

	}
}
