using System;
using System.Collections.Generic;
using System.Net.Mail;

namespace Stat.BLL
{
    /// <summary>
    /// 邮件系统
    /// </summary>
    public class Stmp
    {
        private static IDAL.IStmp dal = DALFactory.AbstractFactory.GetInstance().CreateStmp();


        /// <summary>
        /// 添加STMP信息
        /// </summary>
        /// <returns></returns>
        public static bool Insert(Stat.Model.Stmp model)
        {


            return dal.Insert(model);
        }
        /// <summary>
        /// 更新信息
        /// </summary>
        /// <returns></returns>
        public static bool Update(Stat.Model.Stmp model)
        {

            return dal.Update(model);
        }
        /// <summary>
        ///删除信息
        /// </summary>
        /// <returns></returns>
        public static bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 获取链接
        /// </summary>
        ///<param name="id"></param>
        /// <returns></returns>
        public static IList<Stat.Model.Stmp> Get(int id)
        {
            return dal.Get(id);
        }
        /// <summary>
        /// 获取链接
        /// </summary>
        /// <returns></returns>
        public static IList<Stat.Model.Stmp> Get()
        {
            return dal.Get();
        }




        /// <summary>
        /// 向某个用户发送邮件（请提供用户的信息：用户名，密码，接受Email）
        /// </summary>
        private static void SendEmail(Model.Users model)
        {
            Model.Stmp stmpInfo = new Stat.Model.Stmp();

            IList<Stat.Model.Stmp> list = dal.Get();
            //====================================================
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].State == 1)
                {
                    stmpInfo.MailSmtp = list[i].MailSmtp;
                    stmpInfo.MailName = list[i].MailName;
                    stmpInfo.MailPass = list[i].MailPass;
                    stmpInfo.MailContent = list[i].MailContent;
                    break;
                }
            }
            stmpInfo.UserName = stmpInfo.MailName.Substring(0, stmpInfo.MailName.IndexOf("@"));
            stmpInfo.Title = model.UserName + " 您成功找回密码。";
            stmpInfo.MailContent = stmpInfo.MailContent.Replace("{用户名}", model.UserName);
            stmpInfo.MailContent = stmpInfo.MailContent.Replace("{新密码}", model.UserPwd);
            stmpInfo.MailContent = stmpInfo.MailContent.Replace("{当前时间}", DateTime.Now.ToString());
            //============================================================================================
            SmtpClient client = new SmtpClient();
            client.Host = stmpInfo.MailSmtp;
            //是否随请求一起发送
            client.UseDefaultCredentials = false;
            //获取验证发信人的身份
            client.Credentials = new System.Net.NetworkCredential(stmpInfo.UserName, stmpInfo.MailPass);
            //通过网络处理待发邮件
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            MailMessage message = new MailMessage(stmpInfo.MailName, model.UserEmail);

            message.Subject = stmpInfo.Title;
            message.Body = stmpInfo.MailContent;
            message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            message.IsBodyHtml = true;
            //添加附件 第二个参数可以是null，这里选择应用程序数据类型
            // Attachment att = new Attachment(@"d:\1.txt", System.Net.Mime.MediaTypeNames.Application.Octet);
            // message.Attachments.Add(att);
            try
            {
                client.Send(message);
                Plugin.Common.Alert("发送成功！请稍后查收邮箱。", "?");
            }
            catch (Exception ex)
            {
                Plugin.Common.Alert("发送失败！" + ex.Message.ToString() + ex.Source.ToString());
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static void ResetPassWord(Model.Users model)
        {
            Model.Users list = Stat.BLL.Users.Get(model.UserName);

            if (list != null && list.UserId > 0)
            {
                if (list.UserEmail.ToLower() == model.UserEmail.ToLower())
                {
                    string strNewPwd = model.UserPwd = new Random().Next(10000000, 99999999).ToString();

                    if (Stat.BLL.Users.EditPWD(model))
                    {
                        model.UserPwd = strNewPwd;
                        //
                        BLL.Stmp.SendEmail(model);
                    }
                    else
                    {
                        Plugin.Common.Alert("当前系统有误，请稍后再提交！");
                    }
                }
                else
                {
                    Plugin.Common.Alert("你输入的Email不匹配！");
                }
            }
            else
            {
                Plugin.Common.Alert("该用户不存在！");
            }
        }
    }
}
