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 SearchFriendForm : Form
    {
        public static int friendId = 0;
        DataSet dataset = new DataSet();
        DataBase mydatabase = new DataBase();
        public SearchFriendForm()
        {
            InitializeComponent();
        }

        //按查找所有人的按钮后，把所有的好友用数据集显示出来
        private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
        {
            string Sql = "select Id,NickName,Age,Sex from Users";
            dataset = mydatabase.GetDataSet(Sql);
            dgvfriends.DataSource = dataset.Tables["Users"];
            pnlFriends.Location = pnlFirst.Location;
            pnlFriends.Visible = true;
            grpOneFriends.Visible = false;
            btnAdd.Visible = true;
            btnUp.Visible = true;
        }

        //点击按钮显示隐藏的框架
        private void radioButton2_CheckedChanged_1(object sender, EventArgs e)
        {
            pnlFriends.Visible = false;
            grpOneFriends.Visible = true;
            btnAdd.Visible = false;
            btnSearch.Visible = true;
        }

        //关闭查找好友窗口
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //单击查找按钮时.查找符合条件的用户
        private void button1_Click(object sender, EventArgs e)
        {
            if (tbcChoice.SelectedIndex == 0)
            {
                BasicallySearch();
            }
            else
            {
                if (!(cboAge.Text.Equals("")) && cboSex.Text.Equals(""))
                {
                    AdvansedSearch();
                }
                else if (!(cboAge.Text.Equals("")) && !(cboSex.Text.Equals("")))
                {
                    ageAndSex();
                }
                else if (cboAge.Text.Equals("") && !(cboSex.Text.Equals("")))
                {
                    sexSearch();
                }
            }
        }

        //基本查找页面（精确查找页面）
        private void BasicallySearch()
        {
            string sql = null;
            grpOneFriends.Visible = true;
            if (txtUsers.Text.Trim() != "" && txtNickName.Text.Trim() == "")
            {
                sql = string.Format("select Id,NickName,Age,Sex from Users where Id={0}", Convert.ToInt32(txtUsers.Text));
            }
            else if (txtUsers.Text.Trim() == "" && txtNickName.Text.Trim() != "")
            {
                sql = string.Format("select Id,NickName,Age,Sex from Users where NickName='{0}'", txtNickName.Text);
            }
            else
            {
                sql = string.Format("select Id,NickName,Age,Sex from Users where Id={0} and NickName='{1}'", Convert.ToInt32(txtUsers.Text), txtNickName.Text);
            }
            dataset = mydatabase.GetDataSet(sql);
            dgvfriends.DataSource = dataset.Tables["Users"];
            txtUsers.Text = "";
            txtNickName.Text = "";
            pnlFriends.Location = pnlFirst.Location;
            pnlFriends.Visible = true;
            btnUp.Visible = true;
            btnAdd.Visible = true;
            grpOneFriends.Visible = false;
        }

        //高级查找的年龄的方法
        private void sexSearch()
        {
            dataset.Clear();
            string sql = string.Format("select Id,NickName,Age,Sex from Users where Sex='{0}'", cboSex.Text.ToString());
            dataset = mydatabase.GetDataSet(sql);
            dgvFriends1.DataSource = dataset.Tables["Users"];
            pnlFriend.Location = pnlOne.Location;
            pnlFriend.Visible = true;
            btnAdd.Visible = true;
            btnUp.Visible = true;
        }

        //高级查找年龄和性别的方法
        private void ageAndSex()
        {
            if (cboAge.Text.Equals("0-20"))
            {
                dataset.Clear();
                string sql = string.Format("select Id,NickName,Age,Sex from Users where age between 0 and 20 and Sex='{0}'", cboSex.Text.ToString());
                dataset = mydatabase.GetDataSet(sql);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            else if (cboAge.Text.Equals("20-30"))
            {
                dataset.Clear();
                string sql1 = string.Format("select Id,NickName,Age,Sex from Users where age between 20 and 30 and Sex='{0}'", cboSex.Text.ToString());
                dataset = mydatabase.GetDataSet(sql1);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            else if (cboAge.Text.Equals("30-40"))
            {
                dataset.Clear();
                string tian = string.Format("select Id,NickName,Age,Sex from Users where age between 30 and 40 and Sex='{0}'", cboSex.Text.ToString());
                dataset = mydatabase.GetDataSet(tian);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            else if (cboAge.Text.Equals("40-50"))
            {
                dataset.Clear();
                string tian = string.Format("select Id,NickName,Age,Sex from Users where age between 40 and 50 and Sex='{0}'", cboSex.Text.ToString());
                dataset = mydatabase.GetDataSet(tian);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            else
            {
                dataset.Clear();
                string tian = string.Format("select Id,NickName,Age,Sex from Users where age between 50 and 100 and Sex='{0}'", cboSex.Text.ToString());
                dataset = mydatabase.GetDataSet(tian);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            //结果面板占用高级查询首页面板
            pnlFriend.Location = pnlOne.Location;
            //显示结果窗口可见
            pnlFriend.Visible = true;
            //加为好友键可见
            btnAdd.Visible = true;
            //上一步按钮可见
            btnUp.Visible = true;
        }
        //高级查找页面年龄的方法
        private void AdvansedSearch()
        {
            if (cboAge.Text.Equals("0-20"))
            {
                dataset.Clear();
                string sql = "select Id,NickName,Age,Sex from Users where age between 0 and 20";
                dataset = mydatabase.GetDataSet(sql);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            else if (cboAge.Text.Equals("20-30"))
            {
                dataset.Clear();
                string sql1 = "select Id,NickName,Age,Sex from Users where age between 20 and 30";
                dataset = mydatabase.GetDataSet(sql1);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            else if (cboAge.Text.Equals("30-40"))
            {
                dataset.Clear();
                string tian = "select Id,NickName,Age,Sex from Users where age between 30 and 40";
                dataset = mydatabase.GetDataSet(tian);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            else if (cboAge.Text.Equals("40-50"))
            {
                dataset.Clear();
                string tian = "select Id,NickName,Age,Sex from Users where age between 40 and 50";
                dataset = mydatabase.GetDataSet(tian);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }
            else
            {
                dataset.Clear();
                string tian = "select Id,NickName,Age,Sex from Users where age between 50 and 100";
                dataset = mydatabase.GetDataSet(tian);
                dgvFriends1.DataSource = dataset.Tables["Users"];
            }

            //结果面板占用高级查询首页面板
            pnlFriend.Location = pnlOne.Location;
            //显示结果窗口可见
            pnlFriend.Visible = true;
            //加为好友键可见
            btnAdd.Visible = true;
            //上一步按钮可见
            btnUp.Visible = true;
        }

        //选项卡中的事件
        private void xuanka2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tbcChoice.SelectedIndex == 0)
            {
                btnAdd.Visible = true;
                btnUp.Visible = true;
            }
            else
            {
                if (dgvFriends1.Visible)
                {
                    btnAdd.Visible = true;
                    btnUp.Visible = true;
                }
                else
                {
                    btnAdd.Visible = false;
                    btnUp.Visible = false;
                }
            }
        }
        //上一步按钮发生的事件
        private void button3_Click(object sender, EventArgs e)
        {
            if (tbcChoice.SelectedIndex == 0)
            {
                pnlFriends.Visible = false;
                if (rdoAllFriends.Checked)
                {
                    grpOneFriends.Visible = false;
                }
                else
                {
                    grpOneFriends.Visible = true;
                }
                btnAdd.Visible = false;
                btnUp.Visible = false;
                btnSearch.Visible = true;
                btnSend.Visible = false;
            }
            else
            {
                btnSearch.Visible = true;
                pnlFriend.Visible = false;
                btnAdd.Visible = false;
                btnUp.Visible = false;
                pnlMessage2.Visible = false;
                btnSend.Visible = false;
                txtMessage1.Text = "";
            }
        }

        //加为好友按钮事件
        private void button4_Click(object sender, EventArgs e)
        {
            if (tbcChoice.SelectedIndex == 0)
            {
                OneFriends();
            }
            else
            {
                SecondFriends();
            }
        }

        //基本查找加为好友
        private void OneFriends()
        {
            if (dgvfriends.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择一个好友", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                friendId = int.Parse(dgvfriends.SelectedRows[0].Cells[0].Value.ToString());
                UserHelper.friendId = friendId;
                if (friendId == Convert.ToInt32(UserHelper.Loginld))
                {
                    MessageBox.Show("你不能加自己为好友！", "提示");
                }
                else
                {
                    string sql = string.Format("select count(*) from Friends where FriendId={0} and HostId={1}", friendId, UserHelper.Loginld);
                    int count = (int)mydatabase.GetScalar(sql);
                    if (count == 1)
                    {
                        MessageBox.Show("你的好友列表已经存在此好友！", "提示");
                    }
                    else
                    {
                        int sum = 0;
                        string Sql = string.Format("select FriendshipPolocyId from Users where Id={0}  ", friendId);
                        SqlDataReader dataReder = mydatabase.GetDataReader(Sql);
                        while (dataReder.Read())
                        {
                            sum = Convert.ToInt32(dataReder["FriendshipPolocyId"]);
                            if (sum == 1)
                            {
                                GroupForm group = new GroupForm();
                                group.ShowDialog();
                                break;
                            }
                            else if (sum == 2)
                            {
                                pnlMessage.Visible = true;
                                btnSearch.Visible = false;
                                btnUp.Visible = true;
                                btnAdd.Visible = false;
                                btnSend.Visible = true;
                                break;
                            }
                            else if (sum == 3)
                            {                                
                                MessageBox.Show(" 对不起，对方不允许你加为好友", "提示");
                                break;
                            }
                        }
                        dataReder.Close();
                    }
                }
            }
        }
        private void SecondFriends()
        {
            int friendId = 0;
            if (dgvFriends1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择一个好友", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                friendId = int.Parse(dgvFriends1.SelectedRows[0].Cells[0].Value.ToString());
                UserHelper.friendId = friendId;
                if (friendId == Convert.ToInt32(UserHelper.Loginld))
                {
                    MessageBox.Show("你不能加自己为好友！", "提示");
                }
                else
                {
                    string sql = string.Format("select count(*) from Friends where FriendId={0}  ", friendId);
                    int count = (int)mydatabase.GetScalar(sql);
                    if (count == 1)
                    {
                        MessageBox.Show("你的好友列表已经存在此好友！", "提示");
                    }
                    else
                    {
                        int sum = 0;
                        string Sql = string.Format("select FriendshipPolocyId from Users where Id={0}  ", friendId);
                        SqlDataReader dataReder = mydatabase.GetDataReader(Sql);
                        while (dataReder.Read())
                        {
                            sum = Convert.ToInt32(dataReder["FriendshipPolocyId"]);
                            if (sum == 1)
                            {
                                dataReder.Close();
                                string sql2 = string.Format("insert into Friends(HostId,FriendId,FriendGroup) values({0},{1},0)", UserHelper.Loginld, friendId);
                                int result = mydatabase.ExecuteSQL(sql2);
                                if (result == 1)
                                {
                                    MessageBox.Show("你加对好友成功", "提示");
                                    string sql3 = string.Format("insert into Messages (FromUserId,ToUserId,MessageTypeId,MessageState) values({0},{1},2,0)", UserHelper.Loginld, friendId);
                                    int result2 = mydatabase.ExecuteSQL(sql3);
                                }
                                break;
                            }
                            else if (sum == 2)
                            {
                                pnlMessage2.Visible = true;
                                btnSearch.Visible = false;
                                btnUp.Visible = true;
                                btnAdd.Visible = false;
                                btnSend.Visible = true;
                                break;
                            }
                            else if (sum == 3)
                            {
                                MessageBox.Show(" 对不起，对方不允许你加为好友", "提示");
                                break;
                            }
                        }
                        dataReder.Close();
                    }
                }
            }
        }
        //发送验证信息
        private void btnSend_Click(object sender, EventArgs e)
        {
            string sql = string.Format("insert into Messages (FromUserId,ToUserId,Message,MessageTypeId,MessageState,MessageTime) values ({0},{1},'{2}',2,0,default)", UserHelper.Loginld, UserHelper.friendId, txtMessage.Text.Trim());
            try
            {
                int result = mydatabase.ExecuteSQL(sql);
                if (result == 1)
                {
                    if (tbcChoice.SelectedIndex == 0)
                    {
                        pnlMessage.Visible = false;
                        btnSearch.Visible = true;
                        btnUp.Visible = true;
                        btnAdd.Visible = true;
                        btnSend.Visible = false;

                    }
                    else
                    {
                        pnlMessage2.Visible = false;
                        btnSearch.Visible = true;
                        btnUp.Visible = true;
                        btnAdd.Visible = true;
                        btnSend.Visible = false;

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void ShowSkin()
        {
            int i = 0;
            string sql = string.Format("select skinId from Users where Id={0}", UserHelper.Loginld);
            try
            {
                SqlDataReader dataReader = mydatabase.GetDataReader(sql);
                if (dataReader.Read())
                {
                    i = Convert.ToInt32(dataReader["skinId"]);
                }
                dataReader.Close();
                if (i == 1)
                {
                    this.skinEngine.SkinFile = "Wave.ssk";
                }
                else if (i == 2)
                {
                    this.skinEngine.SkinFile = "WaveColor1.ssk";
                }
                else if (i == 3)
                {
                    this.skinEngine.SkinFile = "WaveColor1.ssk";
                }
                else if (i == 4)
                {
                    this.skinEngine.SkinFile = "SportsBlack.ssk";
                }
                else if (i == 5)
                {
                    this.skinEngine.SkinFile = "SportsBlue.ssk";
                }
                else if (i == 6)
                {
                    this.skinEngine.SkinFile = "SportsCyan.ssk";
                }
                else if (i == 7)
                {
                    this.skinEngine.SkinFile = "SportsGreen.ssk";
                }
                else
                {
                    this.skinEngine.SkinFile = "MP10.ssk";
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //查找窗口的加载事件
        private void SearchFriendForm_Load(object sender, EventArgs e)
        {
            ShowSkin();
        }
    }
}