using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    public class WeiBoReceiveController : BaseController
    {
        /// <summary>
        /// 发送消息 - 发送被动响应消息
        /// </summary>
        /// <returns></returns>
        public ActionResult SignaTure()
        {
            string SendToWiBo = string.Empty; //返回的响应结果。默认为空字符串。                        
            string postString = string.Empty;
            try
            {
                string signature = RequestString.GetQueryString("signature");   //加密签名
                string timestamp = RequestString.GetQueryString("timestamp");   //时间戳
                string nonce = RequestString.GetQueryString("nonce");           //随机数
                string echostr = RequestString.GetQueryString("echostr");       //随机字符串

                bool isValid = WeiBoHelper.ValidateSHA(signature, nonce, timestamp);
                //签名正确时，如果存在echoStr就返回echoStr，否则接收数据
                if (isValid)
                {
                    #region 响应发送验证GET
                    if (Request.HttpMethod.Equals("GET"))
                    {
                        if (!string.IsNullOrEmpty(echostr))
                        {
                            //存在echoStr，是首次配置时验证url可达性。
                            SendToWiBo = echostr;
                        }
                    }
                    #endregion

                    #region 获取推送消息，POST
                    //发送消息 - 发送被动响应消息 接收post过来的消息数据 
                    string msgType = "";
                    long receiver_id = 0;   //回复消息的接收方id。蓝v粉丝的uid。这个字段需要在接收的推送消息中获取。//session("receiver_id")
                    long sender_id = 0;        //回复消息的发送方id。即蓝v自己的uid
                    string msgContent = "";

                    StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Request.InputStream);
                    postString = reader.ReadToEnd();

                    if (!string.IsNullOrWhiteSpace(postString))
                    {

                        //根据业务对消息进行处理。处理完成可以返回空串，也可以返回回复消息。
                        JObject result = null;
                        result = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(postString);
                        msgType = result["type"].ToString();
                        sender_id = long.Parse(result["receiver_id"].ToString());
                        receiver_id = long.Parse(result["sender_id"].ToString());
                        msgContent = result["text"].ToString();
                        //判断msgType
                        switch (msgType)
                        {
                            case "text":
                                if (msgContent == "你好")
                                {
                                    SendToWiBo = ReceiveMsg.pushMsg(ReceiveMsg.textMsg(postString), receiver_id, sender_id, msgType);
                                }
                                else if (msgContent.Contains("新闻"))
                                {
                                    SendToWiBo = ReceiveMsg.pushMsg(ReceiveMsg.articlesMsg(postString), receiver_id, sender_id, "articles");
                                }
                                else
                                {
                                    SendToWiBo = ReceiveMsg.pushMsg(ReceiveMsg.textMsg(postString), receiver_id, sender_id, msgType);
                                }
                                //Log.WriteLog("seccess:" + SendToWiBo + "");
                                break;
                            case "position":
                                break;
                            case "voice":
                                break;
                            case "image":
                                SendToWiBo = ReceiveMsg.pushMsg(ReceiveMsg.articlesMsg(postString), receiver_id, sender_id, "articles");
                                //Log.WriteLog("seccess:" + SendToWiBo + "|" + WeiBoAPI.WeiBoAPI.articlesMsg(postString));
                                break;
                            case "event":
                                break;
                            case "mention": //被@消息
                                break;
                            default:
                                WeiBoLog.WriteLog("error:判断类型错误");
                                break;
                        }
                        #endregion
                    }
                    else
                    {
                        WeiBoLog.WriteLog("error:推送的数据为" + postString);
                    }
                }
                else
                {
                    WeiBoLog.WriteLog("error:签名不通过");                    
                }
            }
            catch (Exception ex)
            {
                WeiBoLog.WriteLog("error:" + ex.Message);
            }
            return Content(SendToWiBo);
        }
    }
}