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 FTarjeta.
	/// </summary>
	public class FTarjeta : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox tbNumero;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.ListView lvTitulares;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button bHacerTitular;
		private System.Windows.Forms.Button bGuardar;
		/// <summary>
		/// Variable del diseñador requerida.
		/// </summary>
		private System.ComponentModel.Container components = null;
		protected Cuenta mCuenta;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox tbCredito;
		private System.Windows.Forms.RadioButton rbDebito;
		private System.Windows.Forms.RadioButton rbCrédito;
		private controlesMacario.NumeroDeCuenta datosTarjeta;
		private controlesMacario.NumeroDeCuenta cuentaAsociada;
		protected Tarjeta mTarjeta;
		protected Cliente mTitular;

		public FTarjeta(Cuenta c)
		{
			//
			// Necesario para admitir el Diseñador de Windows Forms
			//
			InitializeComponent();
			lvTitulares.Columns.Add("", 5, HorizontalAlignment.Left);
			lvTitulares.Columns.Add("Nombre", 50, HorizontalAlignment.Left);
			lvTitulares.Columns.Add("Apellidos", 150, HorizontalAlignment.Left);
			lvTitulares.Columns.Add("NIF", -2, HorizontalAlignment.Left);
			lvTitulares.View = View.Details;
			mCuenta=c;
			muestra();
		}

		/// <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 Código generado por el Diseñador de Windows Forms
		/// <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.tbNumero = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.bHacerTitular = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.lvTitulares = new System.Windows.Forms.ListView();
			this.bGuardar = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.tbCredito = new System.Windows.Forms.TextBox();
			this.rbDebito = new System.Windows.Forms.RadioButton();
			this.rbCrédito = new System.Windows.Forms.RadioButton();
			this.datosTarjeta = new controlesMacario.NumeroDeCuenta();
			this.cuentaAsociada = new controlesMacario.NumeroDeCuenta();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.datosTarjeta);
			this.groupBox1.Controls.Add(this.tbNumero);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(8, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(272, 96);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Identificación";
			// 
			// tbNumero
			// 
			this.tbNumero.Enabled = false;
			this.tbNumero.Location = new System.Drawing.Point(96, 56);
			this.tbNumero.Name = "tbNumero";
			this.tbNumero.Size = new System.Drawing.Size(160, 20);
			this.tbNumero.TabIndex = 2;
			this.tbNumero.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(32, 56);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 24);
			this.label1.TabIndex = 1;
			this.label1.Text = "Número:";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.cuentaAsociada);
			this.groupBox2.Controls.Add(this.bHacerTitular);
			this.groupBox2.Controls.Add(this.label3);
			this.groupBox2.Controls.Add(this.lvTitulares);
			this.groupBox2.Location = new System.Drawing.Point(288, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(328, 200);
			this.groupBox2.TabIndex = 4;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Cuenta asociada";
			// 
			// bHacerTitular
			// 
			this.bHacerTitular.Location = new System.Drawing.Point(8, 160);
			this.bHacerTitular.Name = "bHacerTitular";
			this.bHacerTitular.Size = new System.Drawing.Size(168, 32);
			this.bHacerTitular.TabIndex = 3;
			this.bHacerTitular.Text = "Convertir en titular de la tarjeta";
			this.bHacerTitular.Click += new System.EventHandler(this.bHacerTitular_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(152, 24);
			this.label3.TabIndex = 2;
			this.label3.Text = "Titulares de la cuenta";
			// 
			// lvTitulares
			// 
			this.lvTitulares.Location = new System.Drawing.Point(8, 80);
			this.lvTitulares.Name = "lvTitulares";
			this.lvTitulares.Size = new System.Drawing.Size(312, 72);
			this.lvTitulares.TabIndex = 1;
			// 
			// bGuardar
			// 
			this.bGuardar.Location = new System.Drawing.Point(104, 176);
			this.bGuardar.Name = "bGuardar";
			this.bGuardar.Size = new System.Drawing.Size(120, 32);
			this.bGuardar.TabIndex = 5;
			this.bGuardar.Text = "Crear tarjeta";
			this.bGuardar.Click += new System.EventHandler(this.bGuardar_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(40, 152);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(112, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Crédito concedido:";
			// 
			// tbCredito
			// 
			this.tbCredito.Enabled = false;
			this.tbCredito.Location = new System.Drawing.Point(144, 152);
			this.tbCredito.Name = "tbCredito";
			this.tbCredito.Size = new System.Drawing.Size(72, 20);
			this.tbCredito.TabIndex = 3;
			this.tbCredito.Text = "";
			// 
			// rbDebito
			// 
			this.rbDebito.Checked = true;
			this.rbDebito.Location = new System.Drawing.Point(64, 120);
			this.rbDebito.Name = "rbDebito";
			this.rbDebito.Size = new System.Drawing.Size(64, 24);
			this.rbDebito.TabIndex = 6;
			this.rbDebito.TabStop = true;
			this.rbDebito.Text = "Débito";
			// 
			// rbCrédito
			// 
			this.rbCrédito.Location = new System.Drawing.Point(136, 120);
			this.rbCrédito.Name = "rbCrédito";
			this.rbCrédito.Size = new System.Drawing.Size(72, 24);
			this.rbCrédito.TabIndex = 7;
			this.rbCrédito.Text = "Crédito";
			this.rbCrédito.CheckedChanged += new System.EventHandler(this.rbCrédito_CheckedChanged);
			// 
			// datosTarjeta
			// 
			this.datosTarjeta.ccc = "";
			this.datosTarjeta.dc = "";
			this.datosTarjeta.Enabled = false;
			this.datosTarjeta.entidad = "1000";
			this.datosTarjeta.Location = new System.Drawing.Point(8, 24);
			this.datosTarjeta.Name = "datosTarjeta";
			this.datosTarjeta.Size = new System.Drawing.Size(256, 24);
			this.datosTarjeta.sucursal = "";
			this.datosTarjeta.TabIndex = 3;
			// 
			// cuentaAsociada
			// 
			this.cuentaAsociada.ccc = "";
			this.cuentaAsociada.dc = "";
			this.cuentaAsociada.Enabled = false;
			this.cuentaAsociada.entidad = "1000";
			this.cuentaAsociada.Location = new System.Drawing.Point(8, 24);
			this.cuentaAsociada.Name = "cuentaAsociada";
			this.cuentaAsociada.Size = new System.Drawing.Size(312, 24);
			this.cuentaAsociada.sucursal = "";
			this.cuentaAsociada.TabIndex = 4;
			// 
			// FTarjeta
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(624, 334);
			this.Controls.Add(this.rbCrédito);
			this.Controls.Add(this.rbDebito);
			this.Controls.Add(this.bGuardar);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.tbCredito);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.groupBox1);
			this.Name = "FTarjeta";
			this.Text = "Gestión de tarjetas";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		protected void muestra() 
		{
			this.cuentaAsociada.sucursal=mCuenta.sucursal;
			this.cuentaAsociada.ccc=mCuenta.ccc;
			foreach (Cliente c in mCuenta.titulares) 
			{
				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 rbCrédito_CheckedChanged(object sender, System.EventArgs e)
		{
			if (rbCrédito.Checked) 
			{
				tbCredito.Enabled=true;
			} 
			else 
			{
				tbCredito.Enabled=false;
			}
		}

		private void bGuardar_Click(object sender, System.EventArgs e)
		{
			try 
			{
				guardar();
				muestraDatosDeTarjeta();
				this.bGuardar.Enabled=false;
			}
			catch (Exception ex) 
			{
				MessageBox.Show("Error al crear la tarjeta: " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}

		protected void guardar() {
			if (rbCrédito.Checked) 
			{
				mTarjeta=new Credito(mCuenta, double.Parse(this.tbCredito.Text), mTitular);
			} else
				mTarjeta=new Debito(mCuenta, mTitular);
			mTarjeta.add(mTitular);
		}

		protected void muestraDatosDeTarjeta() 
		{
			this.datosTarjeta.sucursal=mTarjeta.sucursal;
			this.datosTarjeta.ccc=mTarjeta.ccc;
			this.tbNumero.Text=mTarjeta.numeroDeTarjeta;
		}

		private void bHacerTitular_Click(object sender, System.EventArgs e)
		{
			String nif=this.lvTitulares.SelectedItems[0].SubItems[3].Text;
			mTitular=new Cliente(nif);
		}
	}
}
