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

namespace MyQQ
{
    public partial class FriendGroupForm : Form
    {
        int fromUserId = RequestForm.fromUserId;

        DataBase mydatabase = new DataBase();

        public FriendGroupForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            
            try
            {
                string sql1 = string.Format("select count(*) from Friends where HostId={0} and FriendId={1}", UserHelper.Loginld, fromUserId);

                int count = (int)mydatabase.GetScalar(sql1);

                if (count < 1)
                {
                    string sql2 = string.Format("insert into Friends (HostId,FriendId,FriendGroup) values ({0},{1},{2})", UserHelper.Loginld, fromUserId,Convert.ToInt32(cboGroups.SelectedIndex));

                    int result2 = mydatabase.ExecuteSQL(sql2);

                    if (result2 >= 1)
                    {
                        int hostId = 0;

                        DialogResult result = MessageBox.Show("添加好友成功！", "添加好友提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                        if (result == DialogResult.OK)
                        {
                            string sql3 = string.Format("select count(HostId) from Friends where HostId={0} and FriendId={1}", fromUserId, UserHelper.Loginld);

                            hostId = (int)mydatabase.GetScalar(sql3);

                            if (hostId == 0)
                            {
                                string sql4 = string.Format("insert into Messages (FromUserId,ToUserId,MessageTypeId,MessageState) values ({0},{1},2,0)", UserHelper.Loginld, fromUserId);

                                int result1 = mydatabase.ExecuteSQL(sql4);

                            }
                        }
                    }
                }
                else
                {
                    int hostId = 0;

                    DialogResult result = MessageBox.Show("你的好友列表中已经存在该好友！", "添加好友提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                    if (result == DialogResult.OK)
                    {
                        string sql3 = string.Format("select count(HostId) from Friends where HostId={0} and FriendId={1}", fromUserId, UserHelper.Loginld);

                        hostId = (int)mydatabase.GetScalar(sql3);

                        if (hostId == 0)
                        {
                            string sql4 = string.Format("insert into Messages (FromUserId,ToUserId,MessageTypeId,MessageState) values ({0},{1},2,0)", UserHelper.Loginld, fromUserId);

                            int result1 = mydatabase.ExecuteSQL(sql4);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        //关闭窗体
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}