using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Threading;

namespace OsaGateway
{
    public partial class Contacts : Form
    {
        public delegate void ContactsFormHandler(List<Contact> contacts);

        static public event ContactsFormHandler Choosed;

        bool choose = false;

        public Contacts()
        {
            InitializeComponent();

            Contact.ContactAdded += new Contact.ContactHandler(RefreshList);
            Contact.ContactRemoved += new Contact.ContactHandler(RefreshList);
            Contact.ContactUpdated += new Contact.ContactHandler(RefreshList);

            RefreshList();
        }

        public Contacts(bool chooseContacts)
        {
            InitializeComponent();
            RefreshList();
            choose = true;
            button1.Text = "Vybrat";
        }

        private void RefreshList()
        {
            textBox1.Text = string.Empty;
            gvContacts.DataSource = Contact.GetAll();
            gvContacts.ClearSelection();
        }
        private void RefreshList(string findText)
        {
            gvContacts.DataSource = Contact.GetAll(findText);
            gvContacts.ClearSelection();
        }

        private void AddContact(object sender, EventArgs e)
        {
            if (!choose)
            {
                NewContact nc = new NewContact();
                nc.Show();
            }
            else
            {
                List<Contact> list = new List<Contact>();
                foreach (DataGridViewRow item in gvContacts.SelectedRows)
                {
                    list.Add(Contact.Get(new Guid(item.Cells["cGUID"].Value.ToString())));
                }
                if (Choosed != null)
                    Choosed(list);
                this.Hide();
                this.Close();
            }
        }
        private void DeleteContacts(object sender, EventArgs e)
        {
            foreach (DataGridViewRow item in gvContacts.SelectedRows)
            {
                Contact.Delete(new Guid(item.Cells[0].Value.ToString()), true);
            }
            RefreshList(textBox1.Text);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            RefreshList(textBox1.Text);
        }

        private void gvContacts_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!choose)
            {
                NewContact cd = new NewContact(new Guid(gvContacts.Rows[e.RowIndex].Cells["cGUID"].Value.ToString()));
                cd.Show();
            }
            else
            {
                this.AddContact(null, null);
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvContacts.SelectedRows.Count; i++)
            {
                if (i == gvContacts.SelectedRows.Count - 1)
                    Contact.Delete(new Guid(gvContacts.SelectedRows[i].Cells["cGUID"].Value.ToString()));
                else
                    Contact.Delete(new Guid(gvContacts.SelectedRows[i].Cells["cGUID"].Value.ToString()), true);
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            string phoneNumbers = string.Empty;
            for (int i = 0; i < gvContacts.SelectedRows.Count; i++)
                phoneNumbers += gvContacts.SelectedRows[i].Cells["cPhone"].Value.ToString() + "; ";
            NewCall nc = new NewCall(phoneNumbers);
            nc.Show();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            string phoneNumbers = string.Empty;
            for (int i = 0; i < gvContacts.SelectedRows.Count; i++)
                phoneNumbers += gvContacts.SelectedRows[i].Cells["cPhone"].Value.ToString() + "; ";
            NewSMS ns = new NewSMS(phoneNumbers);
            ns.Show();
        }
    }
}