using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using EnterpriseDesktop.Contacts.Proxy.Web;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace EnterpriseDesktop.Contacts.WinForm
{
    public partial class Contacts : UserControl
    {
        public Contacts()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
        }

        private void Contacts_Load(object sender, EventArgs e)
        {
            GetContacts();
        }

        /// <summary>
        /// Calls the web service and gets all contacts
        /// </summary>
        private void GetContacts()
        {
            // Go and get the contacts asynchronously
            EnterpriseDesktop.Contacts.Proxy.Web.Service proxy = new EnterpriseDesktop.Contacts.Proxy.Web.Service();
            proxy.GetAllContactsCompleted += new GetAllContactsCompletedEventHandler(proxy_GetAllContactsCompleted);
            proxy.GetAllContactsAsync(Guid.NewGuid());
        }

        void proxy_GetAllContactsCompleted(object sender, GetAllContactsCompletedEventArgs e)
        {
            if (e.Cancelled == false)
            {
                this.contactDataSet.Merge(e.Result);
                
                // Update Title Drop Down
                UpdateTitleComboBox();
            }
        }

        /// <summary>
        ///  Adds unique titles to combo box.
        /// </summary>
        private void UpdateTitleComboBox()
        {
            _cmbxTitle.Items.Add(String.Empty); // give us a blank one to start with
            foreach (ContactDataSet.ContactRow row in this.contactDataSet.Contact.Rows)
            {
                if (row.IsTitleNull())
                {
                    continue;
                }
                if (_cmbxTitle.Items.Contains(row.Title) == false)
                {
                    _cmbxTitle.Items.Add(row.Title);
                }
            }
        }
        private void _txtFirstName_TextChanged(object sender, EventArgs e)
        {
            Filter();
        }

        /// <summary>
        /// Fired on text change and index changed.
        /// </summary>
        private void Filter()
        {
            string filter = String.Empty;
            string connector = " AND ";

            if (!String.IsNullOrEmpty(_txtFirstName.Text))
            {
                filter = "FirstName LIKE '%" + _txtFirstName.Text.Replace("'", "''") + "%'" + connector;
            }
            if (!String.IsNullOrEmpty(_txtLastName.Text))
            {
                filter += "LastName LIKE '%" + _txtLastName.Text.Replace("'", "''") + "%'" + connector;
            }
            if (!String.IsNullOrEmpty(_cmbxTitle.Text))
            {
                filter += "Title='" + _cmbxTitle.Text + "'" + connector;
            }
            if (filter.Length > 0)
            {
                contactBindingSource.Filter = filter.Substring(0, filter.Length - 5);
            }
            else
            {
                contactBindingSource.Filter = String.Empty;
            }

        }

        private void _txtLastName_TextChanged(object sender, EventArgs e)
        {
            Filter();
        }

        private void _cmbxTitle_SelectedIndexChanged(object sender, EventArgs e)
        {
            Filter();
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            GetContacts();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SaveContact newContact = new SaveContact();
            newContact.Show();
        }

        /// <summary>
        /// Add something to the cache.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            CacheManager manager = CacheFactory.GetCacheManager("Cache Manager");
            manager.Add("date", DateTime.Now);
        }

        /// <summary>
        /// Take something out of the cache.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CacheManager manager = CacheFactory.GetCacheManager("Cache Manager");
            if (manager.Contains("date"))
            {
                DateTime olddate = (DateTime)manager["date"];
                MessageBox.Show(olddate.ToString());
            }
        }
    }
}
