using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BazaZamowien.Classes;

namespace BazaZamowien
{
    public partial class PremieWspolczynniki : Form
    {
        public PremieWspolczynniki()
        {
            InitializeComponent();
            wspolczynnikiTableAdapter.Connection.ConnectionString = ConnString.getConnString().PremieConnStr;

            choroboweTextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
            choroboweTextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);

            emerytalneTextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
            emerytalneTextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);
            
            rentoweTextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
            rentoweTextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);
            
            zdrowotne1TextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
            zdrowotne1TextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);
            
            zdrowotne2TextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
            zdrowotne2TextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);
            
            podatekTextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
            podatekTextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);
        }

        private void wspolczynnikiBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.wspolczynnikiBindingSource.EndEdit();
            this.wspolczynnikiTableAdapter.Update(this.pREMIEDataSet.Wspolczynniki);
        }

        private void PremieWspolczynniki_Load(object sender, EventArgs e)
        {           
            this.wspolczynnikiTableAdapter.Fill(this.pREMIEDataSet.Wspolczynniki);
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            DataView table = (DataView)wspolczynnikiBindingSource.List;
            DataRowView row = table.AddNew();
            row["data"] = DateTime.Today;
            this.wspolczynnikiBindingSource.EndEdit();          
            this.wspolczynnikiBindingSource.MoveLast();
              
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            if (wspolczynnikiBindingSource.Current != null)
            {
                int id = (int)((DataRowView)wspolczynnikiBindingSource.Current)["IDE"];
                wspolczynnikiTableAdapter.Delete(id);

                pREMIEDataSet.Wspolczynniki.Clear();
                wspolczynnikiTableAdapter.Fill(pREMIEDataSet.Wspolczynniki);
            }
        }

        /// <summary>
        /// formatowanie do decimal -> %
        /// </summary>
        private void DecimalToProcent(object sender, ConvertEventArgs cevent)
        {

            if (Convert.ToDecimal(cevent.Value) > 1)
            {
                //  cevent.Value = Convert.ToDecimal(cevent.Value) / 100;
            }
            cevent.Value = String.Format("{0:P}", cevent.Value);

        }

        /// <summary>
        /// formatowanie do % -> decimal
        /// </summary>
        private void ProcentToDecimal(object sender, ConvertEventArgs cevent)
        {
            string pom = cevent.Value.ToString();
            if (cevent.Value.ToString().EndsWith("%"))
            {
                pom = cevent.Value.ToString().Substring(0, cevent.Value.ToString().Length - 1);
            }
            decimal p = Decimal.Parse(pom) / 100;
            cevent.Value = p.ToString();
        }

        
        private void procentBoxEndEdit(object sender, EventArgs e)
        {
            wspolczynnikiBindingSource.EndEdit();
        }

        private void wspolczynnikiBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            if (wspolczynnikiBindingSource.List.Count == 0)
            {
                panel1.Enabled = false;
            }
            else
            {
                panel1.Enabled = true;
            }
        }

    }
}