using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using banco1.domain;

namespace banco1.presentacion
{
	/// <summary>
	/// Descripción breve de FCuenta.
	/// </summary>
	public class FCuenta : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button bBuscar;
		private System.Windows.Forms.TextBox tbCCC;
		private System.Windows.Forms.TextBox tbDC;
		private System.Windows.Forms.TextBox tbSucursal;
		private System.Windows.Forms.TextBox tbEntidad;
		private System.Windows.Forms.Button bCrear;
		private System.Windows.Forms.Button bCancelar;
		private System.Windows.Forms.Button bBloquear;
		/// <summary>
		/// Variable del diseñador requerida.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.ListView lvTitulares;
		private System.Windows.Forms.Button bTerminarCreacion;
		private System.Windows.Forms.Button bAddTitular;
		private System.Windows.Forms.Button bAbrirFicha;
		private System.Windows.Forms.CheckBox chBloqueada;
		private System.Windows.Forms.CheckBox chCancelada;
		private System.Windows.Forms.Button bDesbloquear;

		//***
		private Cuenta mCuenta;

		public FCuenta(FMenu padre)
		{
			//
			// Necesario para admitir el Diseñador de Windows Forms
			//
			InitializeComponent();

			//***
			this.MdiParent=padre;
			this.tbEntidad.Text=Cuenta.entidad;
			this.tbDC.Text=Cuenta.getDigitoDeControl(this.tbSucursal.Text, this.tbCCC.Text);
			lvTitulares.Columns.Add("", 0, HorizontalAlignment.Left);
			lvTitulares.Columns.Add("Nombre", 50, HorizontalAlignment.Left);
			lvTitulares.Columns.Add("Apellidos", 150, HorizontalAlignment.Left);
			lvTitulares.Columns.Add("NIF", 40, HorizontalAlignment.Left);
			lvTitulares.View = View.Details;
		}

		/// <summary>
		/// Limpiar los recursos que se estén utilizando.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Método necesario para admitir el Diseñador, no se puede modificar
		/// el contenido del método con el editor de código.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.bBloquear = new System.Windows.Forms.Button();
			this.bCancelar = new System.Windows.Forms.Button();
			this.bCrear = new System.Windows.Forms.Button();
			this.bBuscar = new System.Windows.Forms.Button();
			this.tbCCC = new System.Windows.Forms.TextBox();
			this.tbDC = new System.Windows.Forms.TextBox();
			this.tbSucursal = new System.Windows.Forms.TextBox();
			this.tbEntidad = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.lvTitulares = new System.Windows.Forms.ListView();
			this.bTerminarCreacion = new System.Windows.Forms.Button();
			this.bAddTitular = new System.Windows.Forms.Button();
			this.bAbrirFicha = new System.Windows.Forms.Button();
			this.chBloqueada = new System.Windows.Forms.CheckBox();
			this.chCancelada = new System.Windows.Forms.CheckBox();
			this.bDesbloquear = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.bDesbloquear,
																					this.chCancelada,
																					this.chBloqueada,
																					this.bBloquear,
																					this.bCancelar,
																					this.bCrear,
																					this.bBuscar,
																					this.tbCCC,
																					this.tbDC,
																					this.tbSucursal,
																					this.tbEntidad});
			this.groupBox1.Location = new System.Drawing.Point(16, 24);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(264, 136);
			this.groupBox1.TabIndex = 6;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Número de cuenta";
			// 
			// bBloquear
			// 
			this.bBloquear.Location = new System.Drawing.Point(200, 56);
			this.bBloquear.Name = "bBloquear";
			this.bBloquear.Size = new System.Drawing.Size(58, 24);
			this.bBloquear.TabIndex = 13;
			this.bBloquear.Text = "Bloquear";
			this.bBloquear.Click += new System.EventHandler(this.bBloquear_Click);
			// 
			// bCancelar
			// 
			this.bCancelar.Location = new System.Drawing.Point(136, 56);
			this.bCancelar.Name = "bCancelar";
			this.bCancelar.Size = new System.Drawing.Size(58, 24);
			this.bCancelar.TabIndex = 12;
			this.bCancelar.Text = "Cancelar";
			this.bCancelar.Click += new System.EventHandler(this.bCancelar_Click);
			// 
			// bCrear
			// 
			this.bCrear.Location = new System.Drawing.Point(72, 56);
			this.bCrear.Name = "bCrear";
			this.bCrear.Size = new System.Drawing.Size(58, 24);
			this.bCrear.TabIndex = 11;
			this.bCrear.Text = "Crear";
			this.bCrear.Click += new System.EventHandler(this.bCrear_Click);
			// 
			// bBuscar
			// 
			this.bBuscar.Enabled = false;
			this.bBuscar.Location = new System.Drawing.Point(8, 56);
			this.bBuscar.Name = "bBuscar";
			this.bBuscar.Size = new System.Drawing.Size(58, 24);
			this.bBuscar.TabIndex = 10;
			this.bBuscar.Text = "Buscar";
			this.bBuscar.Click += new System.EventHandler(this.bBuscar_Click);
			// 
			// tbCCC
			// 
			this.tbCCC.Location = new System.Drawing.Point(164, 24);
			this.tbCCC.MaxLength = 10;
			this.tbCCC.Name = "tbCCC";
			this.tbCCC.Size = new System.Drawing.Size(92, 20);
			this.tbCCC.TabIndex = 9;
			this.tbCCC.Text = "";
			this.tbCCC.TextChanged += new System.EventHandler(this.tbCCC_TextChanged);
			// 
			// tbDC
			// 
			this.tbDC.Enabled = false;
			this.tbDC.Location = new System.Drawing.Point(136, 24);
			this.tbDC.MaxLength = 2;
			this.tbDC.Name = "tbDC";
			this.tbDC.Size = new System.Drawing.Size(28, 20);
			this.tbDC.TabIndex = 8;
			this.tbDC.Text = "";
			// 
			// tbSucursal
			// 
			this.tbSucursal.Location = new System.Drawing.Point(72, 24);
			this.tbSucursal.MaxLength = 4;
			this.tbSucursal.Name = "tbSucursal";
			this.tbSucursal.Size = new System.Drawing.Size(64, 20);
			this.tbSucursal.TabIndex = 7;
			this.tbSucursal.Text = "";
			this.tbSucursal.TextChanged += new System.EventHandler(this.tbSucursal_TextChanged);
			// 
			// tbEntidad
			// 
			this.tbEntidad.Enabled = false;
			this.tbEntidad.Location = new System.Drawing.Point(8, 24);
			this.tbEntidad.MaxLength = 4;
			this.tbEntidad.Name = "tbEntidad";
			this.tbEntidad.Size = new System.Drawing.Size(64, 20);
			this.tbEntidad.TabIndex = 6;
			this.tbEntidad.Text = "";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.bAbrirFicha,
																					this.bAddTitular,
																					this.bTerminarCreacion,
																					this.lvTitulares});
			this.groupBox2.Location = new System.Drawing.Point(288, 24);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(328, 136);
			this.groupBox2.TabIndex = 7;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Titulares";
			// 
			// lvTitulares
			// 
			this.lvTitulares.Location = new System.Drawing.Point(8, 24);
			this.lvTitulares.Name = "lvTitulares";
			this.lvTitulares.Size = new System.Drawing.Size(312, 72);
			this.lvTitulares.TabIndex = 0;
			// 
			// bTerminarCreacion
			// 
			this.bTerminarCreacion.Location = new System.Drawing.Point(216, 104);
			this.bTerminarCreacion.Name = "bTerminarCreacion";
			this.bTerminarCreacion.Size = new System.Drawing.Size(104, 24);
			this.bTerminarCreacion.TabIndex = 1;
			this.bTerminarCreacion.Text = "Terminar creación";
			this.bTerminarCreacion.Visible = false;
			this.bTerminarCreacion.Click += new System.EventHandler(this.bTerminarCreacion_Click);
			// 
			// bAddTitular
			// 
			this.bAddTitular.Location = new System.Drawing.Point(8, 104);
			this.bAddTitular.Name = "bAddTitular";
			this.bAddTitular.Size = new System.Drawing.Size(96, 24);
			this.bAddTitular.TabIndex = 2;
			this.bAddTitular.Text = "Añadir titular";
			this.bAddTitular.Visible = false;
			this.bAddTitular.Click += new System.EventHandler(this.bAddTitular_Click);
			// 
			// bAbrirFicha
			// 
			this.bAbrirFicha.Location = new System.Drawing.Point(112, 104);
			this.bAbrirFicha.Name = "bAbrirFicha";
			this.bAbrirFicha.Size = new System.Drawing.Size(96, 24);
			this.bAbrirFicha.TabIndex = 3;
			this.bAbrirFicha.Text = "Abrir ficha";
			this.bAbrirFicha.Visible = false;
			this.bAbrirFicha.Click += new System.EventHandler(this.bAbrirFicha_Click);
			// 
			// chBloqueada
			// 
			this.chBloqueada.Enabled = false;
			this.chBloqueada.Location = new System.Drawing.Point(8, 96);
			this.chBloqueada.Name = "chBloqueada";
			this.chBloqueada.Size = new System.Drawing.Size(80, 16);
			this.chBloqueada.TabIndex = 14;
			this.chBloqueada.Text = "Bloqueada";
			// 
			// chCancelada
			// 
			this.chCancelada.Enabled = false;
			this.chCancelada.Location = new System.Drawing.Point(104, 96);
			this.chCancelada.Name = "chCancelada";
			this.chCancelada.Size = new System.Drawing.Size(80, 16);
			this.chCancelada.TabIndex = 15;
			this.chCancelada.Text = "Cancelada";
			// 
			// bDesbloquear
			// 
			this.bDesbloquear.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.bDesbloquear.Location = new System.Drawing.Point(195, 56);
			this.bDesbloquear.Name = "bDesbloquear";
			this.bDesbloquear.Size = new System.Drawing.Size(64, 24);
			this.bDesbloquear.TabIndex = 16;
			this.bDesbloquear.Text = "Desbloquear";
			this.bDesbloquear.Visible = false;
			this.bDesbloquear.Click += new System.EventHandler(this.bDesbloquear_Click);
			// 
			// FCuenta
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(624, 341);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox2,
																		  this.groupBox1});
			this.Name = "FCuenta";
			this.Text = "Gestión de cuentas corrientes";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void tbSucursal_TextChanged(object sender, System.EventArgs e)
		{
			this.tbDC.Text=Cuenta.getDigitoDeControl(this.tbSucursal.Text, this.tbCCC.Text);
		}

		private void tbCCC_TextChanged(object sender, System.EventArgs e)
		{
			this.tbDC.Text=Cuenta.getDigitoDeControl(this.tbSucursal.Text, this.tbCCC.Text);
		}

		private void bCrear_Click(object sender, System.EventArgs e)
		{
			activaBotones(true);
			if (mCuenta==null)
				mCuenta=new Cuenta(this.tbSucursal.Text, this.tbCCC.Text);
			addTitular();
		}

		private void activaBotones(bool accion) 
		{
			this.bCrear.Enabled=!accion;
			this.bBloquear.Enabled=!accion;
			this.bBuscar.Enabled=!accion;
			this.bCancelar.Enabled=!accion;
			this.bAbrirFicha.Visible=accion;
			this.bAddTitular.Visible=accion;
			this.bTerminarCreacion.Visible=accion;
		}

		private void bCancelar_Click(object sender, System.EventArgs e)
		{
			if (mCuenta==null) 
			{
				MessageBox.Show("No hay ninguna cuenta seleccionada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			if (mCuenta.esCancelable())
				mCuenta.cancelar();
			else 
			{
				MessageBox.Show("La cuenta no puede ser cancelada", "Error",
					MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
			carga();
		}

		private void bBloquear_Click(object sender, System.EventArgs e)
		{
			if (mCuenta==null) 
			{
				MessageBox.Show("No hay ninguna cuenta seleccionada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			mCuenta.bloquear();
			carga();
		}

		private void bBuscar_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("Resolveremos esto en el siguiente caso", "Información", 
				MessageBoxButtons.OK, MessageBoxIcon.Asterisk);		
		}

		private void bAddTitular_Click(object sender, System.EventArgs e)
		{
			addTitular();
		}

		private void addTitular() 
		{
			FCliente f=new FCliente();
			f.ShowDialog();
			Cliente c=f.cliente;
			addCliente(c);
		}

		private void bAbrirFicha_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("Escribe el código para que aparezca una ficha del cliente con todos los" +
				"productos que tiene contratados", "Ejercicio", 
				MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private void bTerminarCreacion_Click(object sender, System.EventArgs e)
		{
			this.mCuenta.desbloquear();
			this.activaBotones(false);
		}

		private void carga() 
		{
			this.tbEntidad.Text=Cuenta.entidad;
			this.tbSucursal.Text=mCuenta.sucursal;
			this.tbDC.Text=mCuenta.digitoDeControl;
			this.tbCCC.Text=mCuenta.ccc;
			this.chBloqueada.Checked=!mCuenta.estaActiva();
			this.chCancelada.Checked=mCuenta.cancelada;
			cargaTitulares();
			if (mCuenta.estaActiva()) 
			{
				this.bBloquear.Visible=true;
				this.bDesbloquear.Visible=false;
			} 
			else 
			{
				this.bBloquear.Visible=false;
				this.bDesbloquear.Visible=true;
			}
		}

		private void cargaTitulares() 
		{
			lvTitulares.Items.Clear();
			for (int i=0; i<mCuenta.titulares.Count; i++) 
			{
				Cliente c=(Cliente) this.mCuenta.titulares[i];
				addCliente(c);
			}
		}

		private void addCliente(Cliente c) 
		{
			ListViewItem fila = new ListViewItem();
			fila.SubItems.Add(c.nombre);
			fila.SubItems.Add(c.apellidos);
			fila.SubItems.Add(c.nif);
			lvTitulares.Items.Add(fila);
		}

		private void bDesbloquear_Click(object sender, System.EventArgs e)
		{
			if (mCuenta==null) 
			{
				MessageBox.Show("No hay ninguna cuenta seleccionada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.mCuenta.desbloquear();
			carga();
		}
	}
}
