using System;
using System.Reflection;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace LLNet.GUI.FormBases
{
    public partial class frmToolbarBase : LLNet.GUI.FormBases.frmBase
	{
		
		public LLNet.Controls.LLToolbar dbToolbar;
		private Utils.QueryParams queryParams;
		private bool xmlExists;

		public frmToolbarBase()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
		}

		public Utils.QueryParams QueryParams
		{
			get { return queryParams; }
			set { queryParams = value; }
		}

		protected virtual void SetPageSize()
		{

			Utils.QueryParams qp = new LLNet.Utils.QueryParams();
			qp.AddReplaceParam("MODULE", GUI.CApp.ModulePrefix + ModuleName );
			object oSize = GUI.CApp.bll.GetSingleValue("PagingSize",qp,"General");
			
			if (oSize == null || oSize == System.DBNull.Value)
				oSize = GUI.CApp.GetParam("TPPP");


			dbToolbar.PageSize = (oSize == null || int.Parse( oSize.ToString() ) <= 0) ? 20 : int.Parse( oSize.ToString() );
		}

		public override void Init(object[] pArgs)
		{
			base.Init(pArgs);
			dbToolbar.User = GUI.CApp.usuario;
			dbToolbar.ModuleName = ModuleName;
			dbToolbar.ModulePrefix = GUI.CApp.ModulePrefix;
			SetPageSize();
			RestoreLayout();
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		

		protected virtual void RestoreLayout()
		{
			try
			{
				foreach(FieldInfo field in this.GetType().GetFields(BindingFlags.NonPublic|BindingFlags.Instance))
				{
					if (field.FieldType == typeof(DevExpress.XtraGrid.Views.Grid.GridView ))
					{
						string strFilename = GUI.CApp.ExeDir + "config.xml\\" + this.ModuleName + "." + field.Name  + ".xml";
						((DevExpress.XtraGrid.Views.Grid.GridView )field.GetValue(this)).RestoreLayoutFromXml(strFilename);
						xmlExists = true;
					}
					else if (field.FieldType == typeof(DevExpress.XtraGrid.Views.Card.CardView))
					{
						string strFilename = GUI.CApp.ExeDir + "config.xml\\" + this.ModuleName + "." + field.Name  + ".xml";
						((DevExpress.XtraGrid.Views.Card.CardView)field.GetValue(this)).RestoreLayoutFromXml(strFilename);
						xmlExists = true;
					}
				}			
			}
			catch (Exception ex)
			{
				xmlExists = false;
				GUI.CApp.EventLog.WriteLog(Utils.Types.ErrorTypes.MESSAGES,ex.Message);
			}

		}

		protected virtual void SaveLayout()
		{
			foreach(FieldInfo field in this.GetType().GetFields(BindingFlags.NonPublic|BindingFlags.Instance))
			{
				if (field.FieldType == typeof(DevExpress.XtraGrid.Views.Grid.GridView ))
				{
					string strFilename = GUI.CApp.ExeDir + "config.xml\\" + this.ModuleName + "." + field.Name  + ".xml";
					((DevExpress.XtraGrid.Views.Grid.GridView )field.GetValue(this)).SaveLayoutToXml(strFilename);
				}
				else if (field.FieldType == typeof(DevExpress.XtraGrid.Views.Card.CardView ))
				{
					string strFilename = GUI.CApp.ExeDir + "config.xml\\" + this.ModuleName + "." + field.Name  + ".xml";
					((DevExpress.XtraGrid.Views.Card.CardView )field.GetValue(this)).SaveLayoutToXml(strFilename);
				}
  
			}
		}

		/// <summary>
		/// Metodo virtual utilizado para la actualizacion de maestro / detalle y gridToolbarBase
		/// en maestro detalle se convierte en 2 metodos virtuales, NO BORRAR
		/// </summary>
		/// <param name="buttonClicked"></param>
		public virtual void dbToolbar_OnUpdated(DevExpress.XtraBars.BarItem buttonClicked)
		{
			return;
		}

		private void frmToolbarBase_Load(object sender, System.EventArgs e)
		{
			if (!xmlExists)
			{
				foreach(FieldInfo field in this.GetType().GetFields(BindingFlags.NonPublic|BindingFlags.Instance))
				{
					if (field.FieldType == typeof(DevExpress.XtraGrid.Views.Grid.GridView ))
					{				
						((DevExpress.XtraGrid.Views.Grid.GridView )field.GetValue(this)).BestFitColumns();
					}
				}
			}

		}
	}
}