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

namespace ISIM
{
    public partial class ProtocolsForm : Form
    {
        public ProtocolsForm()
        {
            InitializeComponent();

            this.TopMost = Global.OpenForms.mainForm.TopMost;

            rememberLoginCheckBox.Text = Global.LangManager.Translate("remember");
            connectButton.Text = Global.LangManager.Translate("connectMe");

            jabberName.Text = Login.Jabber.userJid;
            jabberPassword.Text = Login.Jabber.password;

            facebookEmail.Text = Login.Facebook.userJid;
            facebookPassword.Text = Login.Facebook.password;

            googleEmail.Text = Login.GoogleTalk.userJid;
            googlePassword.Text = Login.GoogleTalk.password;

            icqUin.Text = Login.Icq.uin;
            icqPassword.Text = Login.Icq.password;

            // pokud tam není nebo je a je true tak bude zaškrtlej
            rememberLoginCheckBox.Checked = !((Global.settingsDictionary["ISIMsettings"].ContainsKey("rememberLoginCheckBox")) && (!bool.Parse(Global.settingsDictionary["ISIMsettings"]["rememberLoginCheckBox"])));
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            #region !!HNUS!!
            if ((Login.Jabber.userJid != jabberName.Text) || (Login.Jabber.password != jabberPassword.Text))
            {
                Login.Jabber.userJid = jabberName.Text;
                Login.Jabber.password = jabberPassword.Text;

                Global.OpenForms.mainForm.Invoke(new MethodInvoker(() =>
                {
                    Global.OpenForms.mainForm.jabberDisconnect();

                    foreach (ContactListViewItem item in Global.OpenForms.mainForm.contactListView.Items)
                    {
                        if (item.contact.protocol == "jabber")
                            item.Remove();
                    }

                    if (Login.Jabber.userJid.Length > 0)
                    {
                        Global.OpenForms.mainForm.jabberThread = new Thread(new ThreadStart(Global.OpenForms.mainForm.jabberOpenConnection));
                        Global.OpenForms.mainForm.jabberThread.Start();
                    }
                }));
            }




            if ((Login.Facebook.userJid != facebookEmail.Text) || (Login.Facebook.password != facebookPassword.Text))
            {
                if (!facebookEmail.Text.Contains("@chat.facebook.com"))
                    facebookEmail.Text += "@chat.facebook.com";
                Login.Facebook.userJid = facebookEmail.Text;
                Login.Facebook.password = facebookPassword.Text;

                Global.OpenForms.mainForm.Invoke(new MethodInvoker(() =>
                {
                    Global.OpenForms.mainForm.fbDisconnect();

                    foreach (ContactListViewItem item in Global.OpenForms.mainForm.contactListView.Items)
                    {
                        if (item.contact.protocol == "facebook")
                            item.Remove();
                    }

                    if (Login.Facebook.userJid.Length > 0)
                    {
                        Global.OpenForms.mainForm.facebookThread = new Thread(new ThreadStart(Global.OpenForms.mainForm.facebookOpenConnection));
                        Global.OpenForms.mainForm.facebookThread.Start();
                    }
                }));
            }



            if ((Login.GoogleTalk.userJid != googleEmail.Text) || (Login.GoogleTalk.password != googlePassword.Text))
            {
                Login.GoogleTalk.userJid = googleEmail.Text;
                Login.GoogleTalk.password = googlePassword.Text;

                Global.OpenForms.mainForm.Invoke(new MethodInvoker(() =>
                {
                    Global.OpenForms.mainForm.gtDisconnect();

                    foreach (ContactListViewItem item in Global.OpenForms.mainForm.contactListView.Items)
                    {
                        if (item.contact.protocol == "gtalk")
                            item.Remove();
                    }

                    if (Login.GoogleTalk.userJid.Length > 0)
                    {
                        Global.OpenForms.mainForm.googleThread = new Thread(new ThreadStart(Global.OpenForms.mainForm.googleOpenConnection));
                        Global.OpenForms.mainForm.googleThread.Start();
                    }
                }));
            }


            if ((Login.Icq.uin != icqUin.Text) || (Login.Icq.password != icqPassword.Text))
            {
                Login.Icq.uin = icqUin.Text;
                Login.Icq.password = icqPassword.Text;

                Global.OpenForms.mainForm.Invoke(new MethodInvoker(() =>
                {
                    Global.OpenForms.mainForm.icqDisconnect();

                    foreach (ContactListViewItem item in Global.OpenForms.mainForm.contactListView.Items)
                    {
                        if (item.contact.protocol == "ICQ")
                            item.Remove();
                    }

                    if (Login.Icq.uin.Length > 0)
                    {
                        Global.OpenForms.mainForm.icqThread = new Thread(new ThreadStart(Global.OpenForms.mainForm.icqOpenConnection));
                        Global.OpenForms.mainForm.icqThread.Start();
                    }
                }));
            }
#endregion

            Global.ChangeISIMsettings("rememberLoginCheckBox", rememberLoginCheckBox.Checked.ToString());

            if (rememberLoginCheckBox.Checked)
                Global.ISIMFunctions.SaveLogin();
            else
                Global.ISIMFunctions.DeleteLogin();

            Close();
        }

        private void getFacebookUsername_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("www.facebook.com/username");
        }
    }
}
