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

namespace Baza_Reklam
{
    public partial class AddZamowienieForm : Form
    {
        private REKLAMADataSetTableAdapters.KLIENCITableAdapter klienciTableAdapter =
                new REKLAMADataSetTableAdapters.KLIENCITableAdapter();

        public REKLAMADataSet.zamowieniaDataTable Zamowienia
        {
            get
            {
                return rEKLAMADataSet.zamowienia;
            }
        }

        public AddZamowienieForm(int idZam, int idKlienta)
        {
            InitializeComponent();

            zamowieniaTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            kONTATableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            aGENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            klienciTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;

            procentProwizjiTextBox.DataBindings[0].Format += DecimalToProcent;
            procentProwizjiTextBox.DataBindings[0].Parse += ProcentToDecimal;

            REKLAMADataSet.zamowieniaRow row;
            if (idZam == 0)
            {
                DataView table = (DataView)zamowieniaBindingSource.List;
                DataRowView r = table.AddNew();
                row = (REKLAMADataSet.zamowieniaRow)r.Row;

                row.nrZamowienia =  DbUtils.numerNowegoZamowienia(idKlienta, DateTime.Now.Year, User.Instance().Kod_agenta);
                row.rokZamowienia = DateTime.Now.Year;
                row.kodAgenta = User.Instance().Kod_agenta;
                row.idKlienta = idKlienta;

                row.EndEdit();
                zamowieniaBindingSource.EndEdit();
            }
            else
            {
                zamowieniaTableAdapter.FillByIdZamowienia(rEKLAMADataSet.zamowienia, idZam);
                row = rEKLAMADataSet.zamowienia[0];
            }

            REKLAMADataSet.KLIENCIRow k = klienciTableAdapter.GetDataByCustomerId(row.idKlienta)[0];
            numerZamowieniaLabel1.Text = k.kodKlienta + "/" + row.rokZamowienia + "/" + row.kodAgenta + "/" + row.nrZamowienia;

            kierownikGroupBox.Enabled = User.Instance().St_kierownik;

            czasPlatnosciListBox.DisplayMember = "DisplayValue";
            czasPlatnosciListBox.ValueMember = "IDEvalue1";
            czasPlatnosciListBox.DataSource = GetDaysList(row.czasPlatnosci);
        }
 
        private void AddZamowienieForm_Load(object sender, EventArgs e)
        {
            string agent = ((DataRowView)zamowieniaBindingSource.Current)["kodAgenta"].ToString();
            int idAgencji = DbUtils.idAgencjiWgKoduAgenta(agent);          
  
            kONTATableAdapter.FillByIdAgencji(sLOWNIKDataSet.KONTA, idAgencji);
            aGENCITableAdapter.FillByAktywny(sLOWNIKDataSet.AGENCI);

            zamowieniaBindingSource.ResetBindings(false);

            SetCzasPlatnosci(((REKLAMADataSet.zamowieniaRow)((DataRowView)zamowieniaBindingSource.Current).Row).czasPlatnosci);
        }

        private List<BoundItem> GetDaysList(int czasPlatnosci)
        {
            SortedList<int, int> days = new SortedList<int, int>();
            days.Add(7, 7);
            days.Add(14, 14);
            days.Add(21, 21);
            days.Add(30, 30);
            if (!days.ContainsKey(czasPlatnosci))
                days.Add(czasPlatnosci, czasPlatnosci);

            List<BoundItem> list = new List<BoundItem>();
            foreach (int key in days.Keys)
            {
                list.Add(new BoundItem(key, key + " dni"));
            }

            return list;
        }

        private void SetCzasPlatnosci(int czasPlatnosci)
        {
            txtCzasPlatnosci.Visible = User.Instance().St_kierownik;
            lblCzasPlatnosci.Visible = txtCzasPlatnosci.Visible;
            
            txtCzasPlatnosci.Text = czasPlatnosci.ToString();
            czasPlatnosciListBox.SelectedValue = czasPlatnosci;
        }

        private void anulujButton_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (kONTABindingSource.Current != null)
            {
                DataRowView row = (DataRowView)kONTABindingSource.Current;
                MessageBox.Show(row["konto"].ToString());
            }
        }

        private void zapiszButton_Click(object sender, EventArgs e)
        {
            zamowieniaBindingSource.EndEdit();
            zamowieniaTableAdapter.Update(rEKLAMADataSet.zamowienia);
        }

        private void procentProwizjiTextBox_TextChanged(object sender, EventArgs e)
        {
            zamowieniaBindingSource.EndEdit();
        }

        private void czasPlatnosciListBox_Click(object sender, EventArgs e)
        {
            txtCzasPlatnosci.Text = czasPlatnosciListBox.SelectedValue.ToString();
        }

        /// <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();
        }
    }
}