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
{
    /// <summary>
    /// (RequestForm)添加好友请求窗体中的相关功能
    /// </summary>
    public partial class RequestForm : Form
    {
       public static  int fromUserId = 0;
        public RequestForm()
        {
            InitializeComponent();
        }
        DataBase mydatabase = new DataBase();
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定不加对方为好友吗？", "添加好友提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                this.Close();
            }
        }

        //显示皮肤的方法
        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 RequestForm_Load(object sender, EventArgs e)
        {
            ShowSkin();
            int faceId = 0;
            string message = null;
            try
            {
                string sql = string.Format("select FromUserId,Message from Messages where MessageTypeId=2 and MessageState=0 and ToUserId={0}",Convert.ToInt32(UserHelper.Loginld));
                SqlDataReader detaReader = mydatabase.GetDataReader(sql);
                if (detaReader.Read())
                {
                    fromUserId = (int)detaReader["FromUserId"];
                    message = detaReader["Message"].ToString();
                }
                detaReader.Close();
                sql = string.Format("select NickName,FaceId from Users where Id={0}", fromUserId);
                SqlDataReader dataReader1 = mydatabase.GetDataReader(sql);
                if (dataReader1.Read())
                {
                    if (dataReader1["FaceId"] is DBNull)
                    {
                        faceId = 0;
                    }
                    else 
                    {
                        faceId = (int)dataReader1["FaceId"];
                    }
                    string nickName = dataReader1["NickName"].ToString();
                    picAddFace.Image = ilFaces.Images[faceId];
                    lblAddMessage.Text = string.Format("{0}({1})请求添加您为好友", nickName, fromUserId);
                    if (message != null)
                    {
                        lblMessages.Visible = true;
                        lblMessages.Text = "验证信息：" + message;
                    }
                }
                dataReader1.Close();
                sql = string.Format("update Messages set MessageState=1 where FromUserId={0}", fromUserId);
                int result = mydatabase.ExecuteSQL(sql);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        //当单击“同意”按钮时，将相应的信息插入到数据库的好友(Friends)表中
        private void btnAgree_Click(object sender, EventArgs e)
        {
            FriendGroupForm groups = new FriendGroupForm();
            groups.ShowDialog();
            this.Close();
        }
    }
}