using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

using Quartz;
using SCC.Common;
using SCC.Models;
using SCC.Interface;
using Newtonsoft.Json;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;

namespace SCC.Crawler.DT
{
    /// <summary>
    /// 数据爬取类
    /// 华东15选5
    /// </summary>
    [DisallowConcurrentExecution]
    [PersistJobDataAfterExecution]
    public class HD15X5Job : IJob
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public HD15X5Job()
        {
            log = new LogHelper();
            services = IOC.Resolve<IDTOpenCode>();
            email = IOC.Resolve<IEmail>();
        }

        /// <summary>
        /// 作业执行入口
        /// </summary>
        /// <param name="context">作业执行上下文</param>
        public void Execute(IJobExecutionContext context)
        {
            Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
            //预设节假日不开奖
            if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
            LatestItem = context.JobDetail.JobDataMap["LatestItem"] as OpenCode5DTModel;
            try
            {
                //服务启动时配置初始数据
                if (LatestItem == null)
                {
                    LatestItem = services.GetOpenCode5DTLastItem(currentLottery);
                    if (LatestItem == null)
                    {
                        //服务第一次启动,数据库一条数据都没有时
                        LatestItem = new OpenCode5DTModel()
                        {
                            Term = CommonHelper.GenerateQiHaoYYYYQQQ(0),
                            OpenTime = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
                        };
                    }
                }
                //程序时间第二天,程序根据配置检查是否昨天有开奖
                isGetData = false;
                if (CommonHelper.CheckDTIsNeedGetData(Config))
                {
                    CheckingOpenDayTheLotteryData();
                }
                if (!LatestItem.Term.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yyyy")))
                {
                    //翻年时
                    LatestItem = new OpenCode5DTModel()
                    {
                        Term = CommonHelper.GenerateQiHaoYYYYQQQ(0),
                        OpenTime = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
                    };
                }
                //当今日开奖并且当前时间是晚上8点过后开始抓取
                if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 12)
                {
                    DoTodayJobByMainUrl();
                    DoTodayJobByBackUrl();
                }
                if (isGetData)
                    TrendChartHelper.GenerateHD15X5TrendChart(log);
            }
            catch (Exception ex)
            {
                log.Error(typeof(HD15X5Job), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
            }
            //保存最新期号
            context.JobDetail.JobDataMap["LatestItem"] = LatestItem;
        }

        /// <summary>
        /// 自检爬取未爬取到的开奖数据,并对昨日开奖但未爬取到开奖数据的彩种添加邮件提醒
        /// </summary>
        private void CheckingOpenDayTheLotteryData()
        {
            //从数据库中获取昨天数据抓取失败列表
            FailedQiHaoList = services.GetFailedYYYYQQQList(currentLottery);
            if (FailedQiHaoList.Count > 0)
            {
                DoYesterdayFailedListByMainUrl();
                DoYesterdayFailedListByBackUrl();
                foreach (var fQiHao in FailedQiHaoList)
                {
                    //将抓取失败数据推送至邮件列表,待邮件服务发送至配置管理员的邮箱中
                    if (email.AddEmail(Config.Area + Config.LotteryName, fQiHao, CommonHelper.GenerateDTOpenTime(Config)))
                        log.Error(typeof(HD15X5Job), CommonHelper.GetJobLogError(Config, fQiHao));
                }
            }
            if (LatestItem.OpenTime.ToString("yyyyMMdd") != CommonHelper.SCCSysDateTime.AddDays(-1).ToString("yyyyMMdd"))
            {
                //开奖时间(昨天)未抓取到最新开奖数据,则再抓取一次,若还不成功则写入待发送邮件列表
                DoTodayJobByMainUrl();
                DoTodayJobByBackUrl();
                if (LatestItem.OpenTime.ToString("yyyyMMdd") != CommonHelper.SCCSysDateTime.AddDays(-1).ToString("yyyyMMdd"))
                {
                    var openQiHao = (LatestItem.Term + 1).ToString();
                    if (email.AddEmail(Config.Area + Config.LotteryName, openQiHao, CommonHelper.GenerateDTOpenTime(Config)))
                        log.Error(typeof(HD15X5Job), CommonHelper.GetJobLogError(Config, openQiHao));
                }
            }
        }

        /// <summary>
        /// 通过主站点爬取开奖数据
        /// (江苏福彩网)
        /// </summary>
        private void DoTodayJobByMainUrl()
        {
            if (!string.IsNullOrEmpty(Config.MainUrl))
            {
                var OpenList = GetOpenListFromMainUrl(Config.MainUrl);
                if (OpenList.Count == 0) return;//无抓取数据
                var newestQiHao = OpenList.First().Term.ToString();
                var startQiNum = Convert.ToInt32(LatestItem.Term.ToString().Substring(4)) + 1;
                var newestQiNum = Convert.ToInt32(newestQiHao.Substring(4));
                if (startQiNum > newestQiNum) return;//无最新数据
                //处理最新开奖数据
                string getQiHao = string.Empty;
                OpenCode5DTModel matchItem = null;
                for (var i = startQiNum; i <= newestQiNum; i++)
                {
                    getQiHao = LatestItem.Term.ToString().Substring(0, 4) + i.ToString().PadLeft(3, '0');
                    matchItem = OpenList.Where(R => R.Term.ToString() == getQiHao).FirstOrDefault();
                    if (matchItem != null && OptimizeMainModel(ref matchItem) && services.AddDTOpen5Code(currentLottery, matchItem))
                    {
                        //Do Success Log
                        log.Info(typeof(HD15X5Job), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
                        LatestItem = matchItem;
                        isGetData = true;
                    }
                }
            }
        }

        /// <summary>
        /// 通过主站爬取错误期号列表中每一个期号
        /// (江苏福彩网)
        /// </summary>
        private void DoYesterdayFailedListByMainUrl()
        {
            if (!string.IsNullOrEmpty(Config.MainUrl) && FailedQiHaoList.Count > 0)
            {
                var OpenList = GetOpenListFromMainUrl(Config.MainUrl);
                if (OpenList.Count == 0) return;//无抓取数据
                OpenCode5DTModel matchItem = null;
                var SuccessList = new List<string>();
                foreach (string failedQiHao in FailedQiHaoList)
                {
                    matchItem = OpenList.Where(R => R.Term.ToString() == failedQiHao).FirstOrDefault();
                    if (matchItem != null && OptimizeMainModel(ref matchItem) && services.AddDTOpen5Code(currentLottery, matchItem))
                    {
                        //Do Success Log
                        log.Info(typeof(HD15X5Job), CommonHelper.GetJobMainLogInfo(Config, failedQiHao));
                        if (matchItem.Term > LatestItem.Term)
                        {
                            LatestItem = matchItem;
                        }
                        isGetData = true;
                        SuccessList.Add(failedQiHao);
                    }
                }
                foreach (var successQiHao in SuccessList)
                {
                    FailedQiHaoList.Remove(successQiHao);
                }
            }
        }
        /// <summary>
        /// 获取主站开奖列表数据
        /// </summary>
        /// <param name="mainUrl">主站地址</param>
        /// <returns></returns>
        private List<OpenCode5DTModel> GetOpenListFromMainUrl(string mainUrl)
        {
            List<OpenCode5DTModel> result = new List<OpenCode5DTModel>();
            try
            {
                var postData = "PlayType=5&currentPage=1&pageSize=200";
                var htmlResource = NetHelper.GetUrlResponse(mainUrl, "POST", postData, Encoding.UTF8);
                if (string.IsNullOrWhiteSpace(htmlResource)) return result;
                var obj = JsonConvert.DeserializeObject<JObject>(htmlResource);
                var lotteryList = obj["LotteryNumberList"];
                OpenCode5DTModel model = null;
                string[] openCodeList = null;
                string issue = string.Empty;
                foreach (var item in lotteryList)
                {
                    issue = item["Issue"].ToString();
                    if (issue.StartsWith((CommonHelper.SCCSysDateTime.Year - 1).ToString())) break;//只抓取今年数据
                    model = new OpenCode5DTModel();
                    model.Term = Convert.ToInt64(issue);
                    model.OpenTime = Convert.ToDateTime(item["LotteryDate"].ToString());
                    openCodeList = item["BasicNumber"].ToString().Trim().Replace("[", string.Empty).Replace("]", string.Empty).Replace("\r\n", string.Empty).Replace("\"", string.Empty).Split(',');
                    if (openCodeList.Length != 5) continue;
                    model.OpenCode1 = Convert.ToInt32(openCodeList[0]);
                    model.OpenCode2 = Convert.ToInt32(openCodeList[1]);
                    model.OpenCode3 = Convert.ToInt32(openCodeList[2]);
                    model.OpenCode4 = Convert.ToInt32(openCodeList[3]);
                    model.OpenCode5 = Convert.ToInt32(openCodeList[4]);
                    model.Detail = string.IsNullOrEmpty(item["PrizePool"].ToString().Replace("万元", string.Empty)) ? "0" : item["PrizePool"].ToString().Replace("万元", string.Empty);
                    result.Add(model);
                }
                result = result.OrderByDescending(S => S.Term).ToList();
            }
            catch (Exception ex)
            {
                log.Error(typeof(HD15X5Job), string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
            }
            return result;
        }

        /// <summary>
        /// 完善主站江苏体彩7位数开奖详情信息
        /// </summary>
        /// <param name="model"></param>
        private bool OptimizeMainModel(ref OpenCode5DTModel model)
        {
            var url = string.Format("http://www.jslottery.com/Lottery/LotteryDetails?PlayType=5&Issue={0}", model.Term);
            try
            {
                var htmlResource = NetHelper.GetUrlResponse(url);
                if (!string.IsNullOrEmpty(htmlResource))
                {
                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(htmlResource);
                    var tables = doc.DocumentNode.SelectNodes("//table");
                    if (tables.Count < 5) return false;
                    var trs = tables[4].ChildNodes[0].ChildNodes.Where(S => S.Name.ToLower() == "tr").ToList();
                    int Level1Num = 0, Level2Num = 0, Level3Num = 0;
                    decimal Level1Money = 0, Level2Money = 0, Level3Money = 0, Sales = 0, Jackpot = 0;
                    foreach (HtmlNode tr in trs)
                    {
                        var tds = tr.ChildNodes.Where(S => S.Name.ToLower() == "td").ToList();
                        if (tds.Count < 3) continue;
                        if (tds[0].InnerText == "特等奖")
                        {
                            Level1Num = Convert.ToInt32(tds[1].InnerText);
                            Level1Money = Convert.ToInt32(tds[2].InnerText);
                        }
                        else if (tds[0].InnerText == "一等奖")
                        {
                            Level2Num = Convert.ToInt32(tds[1].InnerText);
                            Level2Money = Convert.ToInt32(tds[2].InnerText);
                        }
                        else if (tds[0].InnerText == "二等奖")
                        {
                            Level3Num = Convert.ToInt32(tds[1].InnerText);
                            Level3Money = Convert.ToInt32(tds[2].InnerText);
                        }
                    }
                    Regex reg = new Regex(@"本期投注总额<span>([\d.,]*?)</span>");
                    var match = reg.Match(htmlResource);
                    if (match.Success)
                    {
                        Sales = Convert.ToDecimal(match.Result("$1").Replace(",", string.Empty)) * 10000;
                    }
                    else
                    {
                        reg = new Regex(@"本期投注总额<span lang=""EN-US"">([\d.,]*?)</span>");
                        match = reg.Match(htmlResource);
                        if (match.Success)
                        {
                            Sales = Convert.ToDecimal(match.Result("$1").Replace(",", string.Empty)) * 10000;
                        }
                    }
                    Jackpot = Convert.ToDecimal(model.Detail.Replace(",", string.Empty)) * 10000;
                    model.Detail = string.Format("{0},{1}^特等奖|{2}|{3},一等奖|{4}|{5},二等奖|{6}|{7}",
                                Sales, Jackpot, Level1Num, Level1Money, Level2Num, Level2Money, Level3Num, Level3Money);
                    return true;
                }
            }
            catch (Exception ex)
            {
                log.Error(typeof(HD15X5Job), string.Format("【{0}】通过主站点完善抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
            }
            return false;
        }

        /// <summary>
        /// 通过备用站点抓取开奖数据
        /// (百度乐彩)
        /// </summary>
        private void DoTodayJobByBackUrl()
        {
            if (!string.IsNullOrEmpty(Config.BackUrl))
            {
                var OpenList = GetOpenListFromBackUrl(Config.BackUrl);
                if (OpenList.Count == 0) return;//无抓取数据
                var newestQiHao = OpenList.First().Term.ToString();
                var startQiNum = Convert.ToInt32(LatestItem.Term.ToString().Substring(4)) + 1;
                var newestQiNum = Convert.ToInt32(newestQiHao.Substring(4));
                if (startQiNum > newestQiNum) return;//无最新数据
                //处理最新开奖数据
                var getQiHao = string.Empty;
                OpenCode5DTModel matchItem = null;
                for (var i = startQiNum; i <= newestQiNum; i++)
                {
                    getQiHao = LatestItem.Term.ToString().Substring(0, 4) + i.ToString().PadLeft(3, '0');
                    matchItem = OpenList.Where(R => R.Term.ToString() == getQiHao).FirstOrDefault();
                    if (matchItem != null && OptimizeBackModel(ref matchItem) && services.AddDTOpen5Code(currentLottery, matchItem))
                    {
                        //Do Success Log
                        log.Info(typeof(HD15X5Job), CommonHelper.GetJobBackLogInfo(Config, getQiHao));
                        LatestItem = matchItem;
                        isGetData = true;
                    }
                }
            }
        }

        /// <summary>
        /// 通过备用地址抓取错误期号列表中每一个期号
        /// (百度乐彩)
        /// </summary>
        private void DoYesterdayFailedListByBackUrl()
        {
            if (!string.IsNullOrEmpty(Config.BackUrl) && FailedQiHaoList.Count > 0)
            {
                var OpenList = GetOpenListFromBackUrl(Config.BackUrl);
                if (OpenList.Count == 0) return;//无抓取数据
                OpenCode5DTModel matchItem = null;
                var SuccessList = new List<string>();
                foreach (string failedQiHao in FailedQiHaoList)
                {
                    matchItem = OpenList.Where(R => R.Term.ToString() == failedQiHao).FirstOrDefault();
                    if (matchItem != null && OptimizeBackModel(ref matchItem) && services.AddDTOpen5Code(currentLottery, matchItem))
                    {
                        //Do Success Log
                        log.Info(typeof(HD15X5Job), CommonHelper.GetJobBackLogInfo(Config, failedQiHao));
                        if (matchItem.Term > LatestItem.Term)
                        {
                            LatestItem = matchItem;
                        }
                        SuccessList.Add(failedQiHao);
                        isGetData = true;
                    }
                }
                foreach (var successQiHao in SuccessList)
                {
                    FailedQiHaoList.Remove(successQiHao);
                }
            }
        }

        /// <summary>
        /// 获取备用站点开奖列表数据
        /// </summary>
        /// <param name="backUrl">备用站点</param>
        /// <returns></returns>
        private List<OpenCode5DTModel> GetOpenListFromBackUrl(string backUrl)
        {
            List<OpenCode5DTModel> result = new List<OpenCode5DTModel>();
            try
            {
                string requestUrl = string.Format("{0}?r={1}", backUrl, new Random().Next(1000, 9999));
                var htmlResource = NetHelper.GetBaiDuLeCaiResponse(requestUrl);
                if (string.IsNullOrWhiteSpace(htmlResource)) return result;
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(htmlResource);
                var table = doc.DocumentNode.SelectSingleNode("//tbody");
                if (table == null) return result;
                var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
                OpenCode5DTModel model = null;
                for (var i = 0; i < trs.Count; i++)//第一行
                {
                    var trstyle = trs[i].Attributes["style"];
                    if (trstyle != null && trstyle.Value == "display:none")
                    {
                        continue;
                    }
                    var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
                    if (tds.Count < 4) continue;
                    model = new OpenCode5DTModel();
                    model.OpenTime = Convert.ToDateTime(tds[0].InnerText);
                    model.Term = Convert.ToInt64(tds[1].ChildNodes.Where(n => n.Name == "a").ToList()[0].InnerText.Trim());
                    if (tds[2].ChildNodes.Count == 0) continue;
                    var opencodeNode = tds[2].ChildNodes[0].ChildNodes.Where(n => n.Name == "span").ToList();
                    if (opencodeNode.Count < 5) continue;
                    model.OpenCode1 = Convert.ToInt32(opencodeNode[0].InnerText.Trim());
                    model.OpenCode2 = Convert.ToInt32(opencodeNode[1].InnerText.Trim());
                    model.OpenCode3 = Convert.ToInt32(opencodeNode[2].InnerText.Trim());
                    model.OpenCode4 = Convert.ToInt32(opencodeNode[3].InnerText.Trim());
                    model.OpenCode5 = Convert.ToInt32(opencodeNode[4].InnerText.Trim());
                    result.Add(model);
                }
                result = result.OrderByDescending(S => S.Term).ToList();
            }
            catch (Exception ex)
            {
                log.Error(typeof(HD15X5Job), string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
            }
            return result;
        }
        /// <summary>
        /// 完善备用站点江苏体彩7位数开奖实体信息
        /// </summary>
        /// <param name="model"></param>
        private bool OptimizeBackModel(ref OpenCode5DTModel model)
        {
            var url = string.Format("http://baidu.lecai.com/lottery/draw/view/55/{0}?r={1}", model.Term, new Random().Next(1000, 9999));
            try
            {
                var htmlResource = NetHelper.GetBaiDuLeCaiResponse(url);
                Regex reg = new Regex(@"var phaseData = ([\s\S]*?);");
                Match m = reg.Match(htmlResource);
                if (m.Success)
                {
                    var dataJson = m.Result("$1");
                    var obj = JsonConvert.DeserializeObject<dynamic>(dataJson);
                    var data = obj[model.Term.ToString()];
                    if (data != null)
                    {
                        int Level1Num = 0, Level2Num = 0, Level3Num = 0;
                        decimal Level1Money = 0, Level2Money = 0, Level3Money = 0, Sales = 0, Jackpot = 0;
                        Jackpot = Convert.ToDecimal(data["formatPoolAmount"]);
                        Sales = Convert.ToDecimal(data["formatSaleAmount"]);
                        Level1Num = Convert.ToInt32(data["list"]["prize1"]["bet"].Value.Replace(",", string.Empty).Replace("注", string.Empty));
                        Level1Money = Convert.ToDecimal(data["list"]["prize1"]["prize"].Value.Replace(",", string.Empty).Replace("元", string.Empty));
                        Level2Num = Convert.ToInt32(data["list"]["prize2"]["bet"].Value.Replace(",", string.Empty).Replace("注", string.Empty));
                        Level2Money = Convert.ToDecimal(data["list"]["prize2"]["prize"].Value.Replace(",", string.Empty).Replace("元", string.Empty));
                        Level3Num = Convert.ToInt32(data["list"]["prize3"]["bet"].Value.Replace(",", string.Empty).Replace("注", string.Empty));
                        Level3Money = Convert.ToDecimal(data["list"]["prize3"]["prize"].Value.Replace(",", string.Empty).Replace("元", string.Empty));
                        model.Detail = string.Format("{0},{1}^特等奖|{2}|{3},一等奖|{4}|{5},二等奖|{6}|{7}",
                                    Sales, Jackpot, Level1Num, Level1Money, Level2Num, Level2Money, Level3Num, Level3Money);
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                log.Error(typeof(HD15X5Job), string.Format("【{0}】通过备用站点优化开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
            }
            return false;
        }

        #region Attribute
        /// <summary>
        /// 配置信息
        /// </summary>
        private SCCConfig Config = null;
        /// <summary>
        /// 当天抓取的最新一期开奖记录
        /// </summary>
        private OpenCode5DTModel LatestItem = null;
        /// <summary>
        /// 当天抓取失败列表
        /// </summary>
        private List<string> FailedQiHaoList = null;
        /// <summary>
        /// 日志对象
        /// </summary>
        private LogHelper log = null;
        /// <summary>
        /// 数据服务
        /// </summary>
        private IDTOpenCode services = null;
        /// <summary>
        /// 当前彩种
        /// </summary>
        private SCCLottery currentLottery
        {
            get
            {
                return SCCLottery.HD15X5;
            }
        }
        /// <summary>
        /// 邮件接口
        /// </summary>
        private IEmail email = null;
        /// <summary>
        /// 是否本次运行抓取到开奖数据
        /// </summary>
        private bool isGetData = false;
        #endregion
    }
}