using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using Baza_Reklam.Classes.Helpers;

namespace Baza_Reklam
{
    public partial class AddClient : Form
    {
        private readonly int _clientId;
        private REKLAMADataSet.KLIENCIRow _client;

        protected REKLAMADataSet.KLIENCIRow Client
        {
            get { return _client; }
        }

        public AddClient(BindingSource bindingSource)
        {
            InitializeComponent();
            rEKLAMADataSet = (REKLAMADataSet)bindingSource.DataSource;
            clientsBindingSource = bindingSource;
        }

        public AddClient(int clientId, BindingSource bindingSource)
        {
            InitializeComponent();
            rEKLAMADataSet = (REKLAMADataSet)bindingSource.DataSource;
            clientsBindingSource = bindingSource;
            _clientId = clientId;
        }

        private void AddClient_Load(object sender, EventArgs e)
        {
            bool update = false;
            // rebind controls to use passed binding source
            ReBindControls(this, clientsBindingSource);
            LoadTitleList();

            agenciTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            clientsAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            clientTitlesAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            //TODO: try to display only active agents...
            agenciTableAdapter.Fill(sLOWNIKDataSet.AGENCI);
            //agenciTableAdapter.FillByAktywny(sLOWNIKDataSet.AGENCI);

            if (_clientId == 0) // add new client
            {
                _client = rEKLAMADataSet.KLIENCI.NewKLIENCIRow();
                _client.Last_Modify = DateTime.Now;
                _client.data = DateTime.Now;
                _client.UserName = User.Instance().Login;
                _client.AgentExpo = User.Instance().Login;
                rEKLAMADataSet.KLIENCI.AddKLIENCIRow(_client);
                clientsBindingSource.MoveLast();
                cbCountry.SelectedIndex = -1;
                cbState.SelectedIndex = -1;
                cbActive.Checked = true;
            }
            else // update selected client
            {
                update = true;
                _client = (REKLAMADataSet.KLIENCIRow)((DataRowView)clientsBindingSource.Current).Row;

                InitClientTitles();
            }

            SetUserNameAndAgentExpo(_client);

            SetFormCaption(update);

        }

        private void InitClientTitles()
        {
            clientTitlesAdapter.FillByCustomerId(rEKLAMADataSet.KLIENCI_TYTUL, _client.CustomerID);

            lbClientTitles.DataSource = rEKLAMADataSet.KLIENCI_TYTUL;
            lbClientTitles.DisplayMember = rEKLAMADataSet.KLIENCI_TYTUL.TYTULColumn.ColumnName;
            lbClientTitles.ValueMember = rEKLAMADataSet.KLIENCI_TYTUL.ID_KLIENCI_TYTULColumn.ColumnName;
        }

        private void SetFormCaption(bool update)
        {
            Text = update ? "Edytuj dane klienta" : "Dodaj klienta";
        }

        private void SetUserNameAndAgentExpo(REKLAMADataSet.KLIENCIRow client)
        {
            cbUserName.SelectedIndex = cbUserName.FindString(client.UserName);
            cbAgentExpo.SelectedIndex = cbAgentExpo.FindString(client.AgentExpo);

            //TODO: try to display only active agents...
            //if (cbUserName.SelectedIndex == -1)
            //{
            //    int index = cbUserName.Items.Add(client.UserName);
            //    cbUserName.SelectedIndex = index;
            //}

            //if (cbAgentExpo.SelectedIndex == -1)
            //{
            //    int index = cbAgentExpo.Items.Add(client.AgentExpo);
            //    cbAgentExpo.SelectedIndex = index;
            //}

            //if (User.Instance().St_kierownik)
            //{
            //    cbUserName.Enabled = User.Instance().UserAgencyList.Contains(DbUtils.idAgencji(client.UserName));
            //    cbAgentExpo.Enabled = User.Instance().UserAgencyList.Contains(DbUtils.idAgencji(client.AgentExpo));
            //}
            //else
            //{
            //    cbUserName.Enabled = client.UserName.Trim().ToLower() == User.Instance().Symbol_agenta.Trim().ToLower();
            //    cbAgentExpo.Enabled = client.AgentExpo.Trim().ToLower() == User.Instance().Symbol_agenta.Trim().ToLower();
            //}

            cbUserName.Enabled = (DbUtils.idAgencji(client.UserName) == User.Instance().IdAgencji);
            cbAgentExpo.Enabled = (DbUtils.idAgencji(client.AgentExpo) == User.Instance().IdAgencji);
            if (!User.Instance().St_kierownik)
            {
                cbUserName.Enabled = client.UserName.Trim().ToLower() == User.Instance().Symbol_agenta.Trim().ToLower();
                cbAgentExpo.Enabled = client.AgentExpo.Trim().ToLower() == User.Instance().Symbol_agenta.Trim().ToLower();
            }
        }

        private static void ReBindControls(Control control, BindingSource bindingSource)
        {
            foreach (Control ctrl in control.Controls)
            {
                if (ctrl.DataBindings.Count == 1)
                {
                    string property = ctrl.DataBindings[0].PropertyName;
                    string dataMember = ctrl.DataBindings[0].BindingMemberInfo.BindingField;

                    ctrl.DataBindings.Clear();
                    ctrl.DataBindings.Add(property, bindingSource, dataMember, true);
                    Debug.WriteLine(ctrl.Name + ": " + ctrl.DataBindings[0].PropertyName + "; " + ctrl.DataBindings[0].BindingMemberInfo.BindingField);
                }

                ReBindControls(ctrl, bindingSource);
            }
        }

        private void LoadTitleList()
        {
            SLOWNIKDataSetTableAdapters.LISTA_TYTULOWTableAdapter titleListAdapter = new SLOWNIKDataSetTableAdapters.LISTA_TYTULOWTableAdapter();
            titleListAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            lbTitles.DataSource = titleListAdapter.GetData();
            lbTitles.DisplayMember = "SYMB";
            lbTitles.ValueMember = "SYMB";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateChildren())
            {
                Cursor = Cursors.WaitCursor;

                Client.Last_Modify = DateTime.Now;
                Client.Modify_User = User.Instance().Login;
                clientsBindingSource.EndEdit();
                clientsAdapter.Update(rEKLAMADataSet.KLIENCI);
                Cursor = Cursors.Default;
                Close();
                return;
            }

            MessageBox.Show("Proszę wprowadzić wszystkie wymagane dane.");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            rEKLAMADataSet.KLIENCI.RejectChanges();
            Close();
        }

        private void btnCopyAddress_Click(object sender, EventArgs e)
        {
            List<string> address = new List<string>();
            address.Add(txtName1.Text);
            address.Add(txtAddress.Text);
            address.Add(txtPostCode.Text + " " + txtCity.Text);
            if (!string.IsNullOrEmpty(cbState.Text.Trim()))
                address.Add(cbState.Text);

            address.Add(cbCountry.Text);

            txtInvoiceAddress.Lines = address.ToArray();
            txtCorespondenceAddress.Lines = address.ToArray();
        }

        private void btnAddTitle_Click(object sender, EventArgs e)
        {
            if (lbTitles.SelectedValue == null)
            {
                MessageBox.Show("Proszę wybrać tytuł do dodania");
                return;
            }

            clientTitlesAdapter.Insert(lbTitles.SelectedValue.ToString(), _client.CustomerID, User.Instance().Login, DateTime.Now);
            clientTitlesAdapter.FillByCustomerId(rEKLAMADataSet.KLIENCI_TYTUL, _client.CustomerID);

        }

        private void btnRemoveTitle_Click(object sender, EventArgs e)
        {
            if (lbClientTitles.SelectedItem == null) return;

            DataRow row = rEKLAMADataSet.KLIENCI_TYTUL.Select(rEKLAMADataSet.KLIENCI_TYTUL.ID_KLIENCI_TYTULColumn.ColumnName + "=" + lbClientTitles.SelectedValue)[0];
            row.Delete();
            clientTitlesAdapter.Update(rEKLAMADataSet.KLIENCI_TYTUL);
        }

        private void txtName1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ValidationHelper.IsTextBoxEmpty(txtName1, "Proszę podać nazwę klienta.", e, errorProvider);
        }

        private void cbCountry_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ValidationHelper.IsComboValueSelected(cbCountry, "Proszę wybrać kraj klienta.", e, errorProvider);
        }

        private void txtCity_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ValidationHelper.IsTextBoxEmpty(txtCity, "Proszę podać miasto klienta.", e, errorProvider);
        }

        private void txtClientCode_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtClientCode.Text)) return;

            StringBuilder sb = new StringBuilder();

            if (!DbUtils.kodKlientaUnikalny(txtClientCode.Text, Client.UserName, Client.CustomerID))
            {
                e.Cancel = true;
                sb.Append("Proszę podać inny kod klienta. Podany kod już istnieje dla wybranego agenta.");
            }
            else
                e.Cancel = false;

            errorProvider.SetError(txtClientCode, sb.ToString());
        }
    }
}