using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.gexin.rp.sdk.dto;
using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using com.igetui.api.openservice.payload;
using System.Net;
using Newtonsoft.Json.Linq;
using PushPlatform.Common.Model;
using PushPlatform.Common.Util;

namespace PushInfo.Core.BLL
{
    public class GeTuiPushMessage
    {
        //参数设置 <-----参数需要重新设置----->
        //http的域名
        private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";

        //https的域名
        //private static String HOST = "https://api.getui.com/apiex.htm";

        //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
        public static String APPID = "";
        public static String APPKEY = "";
        public static String MASTERSECRET = "";

        public static APIResult<string> PushMessageToSingle(List<string> phoneTypeList, string clientId, int templateType, string templateTitle, string templateBody, string templateTransmissionContent)
        {
            APIResult<string> result= new APIResult<string>();
            result.Result = 1;
            string pushResult = "";
            try
            {
                IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);

                // 单推消息模型
                SingleMessage message = new SingleMessage();
                message.IsOffline = true;                         // 用户当前不在线时，是否离线存储,可选
                message.OfflineExpireTime = 1000 * 3600 * 12;            // 离线有效时间，单位为毫秒，可选
                //判断是否客户端是否wifi环境下推送，2为4G/3G/2G，1为在WIFI环境下，0为不限制环境
                message.PushNetWorkType = 0;

                com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target();
                target.appId = APPID;
                target.clientId = clientId;
                //target.alias = ALIAS;
                //设置消息模板
                if (templateType == 0)
                {
                    message.Data = NotificationTemplateModel(APPID, APPKEY, templateTitle, templateBody, templateTransmissionContent);
                }
                else if (templateType == 1)
                {
                    message.Data = TransmissionTemplateModel(APPID, APPKEY, templateTitle, templateBody, templateTransmissionContent);
                }
                pushResult = push.pushMessageToSingle(message, target);
                result.Msg = pushResult;
                if(string.IsNullOrWhiteSpace(pushResult))
                {
                    result.Result = 0;
                    return result;
                }
                JObject jo = JObject.Parse(pushResult);
                if (!(jo["result"] != null && jo["result"].ToString().ToUpper() == "OK"))
                {
                    result.Result = 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(pushResult);
                LogHelper.Error(ex);
                result.Result = 0;
                result.code = 1;
            }
            return result;
        }

        public static TransmissionTemplate TransmissionTemplateDemo2()
        {
            TransmissionTemplate template = new TransmissionTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            template.TransmissionType = "1";            //应用启动类型，1：强制应用启动 2：等待应用启动
            template.TransmissionContent = "{\"InfoType\":3,\"Content\":\"总质疑字段数为[num]，请尽快处理\",\"Data\":\"{\"ProjectId\":\"[projectId]\"}\"}";  //透传内容

            //iOS简单推送
            //APNPayload apnpayload = new APNPayload();
            //SimpleAlertMsg alertMsg = new SimpleAlertMsg("alertMsg");
            //apnpayload.AlertMsg = alertMsg;
            //apnpayload.Badge = 11;
            //apnpayload.ContentAvailable = 1;
            //apnpayload.Category = "";
            //apnpayload.Sound = "";
            //apnpayload.addCustomMsg("", "");
            //template.setAPNInfo(apnpayload);

            //APN高级推送
            APNPayload apnpayload = new APNPayload();
            DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
            alertMsg.Body = "Body";
            alertMsg.ActionLocKey = "ActionLocKey";
            alertMsg.LocKey = "LocKey";
            alertMsg.addLocArg("LocArg");
            alertMsg.LaunchImage = "LaunchImage";
            //iOS8.2支持字段
            alertMsg.Title = "Title";
            alertMsg.TitleLocKey = "TitleLocKey";
            alertMsg.addTitleLocArg("TitleLocArg");

            apnpayload.AlertMsg = alertMsg;
            apnpayload.Badge = 0;
            apnpayload.ContentAvailable = 0;
            //apnpayload.Category = "";
            //apnpayload.Sound = "test1.wav";
            apnpayload.addCustomMsg("payload", "{\"InfoType\":3,\"Content\":\"总质疑字段数为[num]，请尽快处理\",\"Data\":\"{\"ProjectId\":\"[projectId]\"}\"}");
            template.setAPNInfo(apnpayload);


            //设置客户端展示时间
            //String begin = "2015‐03‐06 14:28:10";
            //String end = "2015‐03‐06 14:38:20";
            //template.setDuration(begin, end);

            return template;
        }

        //PushMessageToList接口测试代码
        private static void PushMessageToList(string clientId)
        {
            // 推送主类（方式1，不可与方式2共存）
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            // 推送主类（方式2，不可与方式1共存）此方式可通过获取服务端地址列表判断最快域名后进行消息推送，每10分钟检查一次最快域名
            //IGtPush push = new IGtPush("",APPKEY,MASTERSECRET);
            ListMessage message = new ListMessage();

            NotificationTemplate template = NotificationTemplateDemo();
            // 用户当前不在线时，是否离线存储,可选
            message.IsOffline = true;
            // 离线有效时间，单位为毫秒，可选
            message.OfflineExpireTime = 1000 * 3600 * 12;
            message.Data = template;
            //message.PushNetWorkType = 0;        //判断是否客户端是否wifi环境下推送，1为在WIFI环境下，0为不限制网络环境。
            //设置接收者
            List<com.igetui.api.openservice.igetui.Target> targetList = new List<com.igetui.api.openservice.igetui.Target>();
            com.igetui.api.openservice.igetui.Target target1 = new com.igetui.api.openservice.igetui.Target();
            target1.appId = APPID;
            target1.clientId = clientId;

            // 如需要，可以设置多个接收者
            //com.igetui.api.openservice.igetui.Target target2 = new com.igetui.api.openservice.igetui.Target();
            //target2.AppId = APPID;
            //target2.ClientId = "ddf730f6cabfa02ebabf06e0c7fc8da0";

            targetList.Add(target1);
            //targetList.Add(target2);

            String contentId = push.getContentId(message);
            String pushResult = push.pushMessageToList(contentId, targetList);
            System.Console.WriteLine("-----------------------------------------------");
            System.Console.WriteLine("服务端返回结果:" + pushResult);
        }


        //pushMessageToApp接口测试代码
        private static void pushMessageToApp()
        {
            // 推送主类（方式1，不可与方式2共存）
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            // 推送主类（方式2，不可与方式1共存）此方式可通过获取服务端地址列表判断最快域名后进行消息推送，每10分钟检查一次最快域名
            //IGtPush push = new IGtPush("",APPKEY,MASTERSECRET);

            AppMessage message = new AppMessage();

            // 设置群推接口的推送速度，单位为条/秒，仅对pushMessageToApp（对指定应用群推接口）有效
            message.Speed = 100;

            TransmissionTemplate template = TransmissionTemplateDemo();

            // 用户当前不在线时，是否离线存储,可选
            message.IsOffline = false;
            // 离线有效时间，单位为毫秒，可选  
            message.OfflineExpireTime = 1000 * 3600 * 12;
            message.Data = template;
            //message.PushNetWorkType = 0;        //判断是否客户端是否wifi环境下推送，1为在WIFI环境下，0为不限制网络环境。
            List<String> appIdList = new List<string>();
            appIdList.Add(APPID);

            //通知接收者的手机操作系统类型
            List<String> phoneTypeList = new List<string>();
            //phoneTypeList.Add("ANDROID");
            //phoneTypeList.Add("IOS");
            //通知接收者所在省份
            List<String> provinceList = new List<string>();
            //provinceList.Add("浙江");
            //provinceList.Add("上海");
            //provinceList.Add("北京");

            List<String> tagList = new List<string>();
            //tagList.Add("开心");

            message.AppIdList = appIdList;
            message.PhoneTypeList = phoneTypeList;
            message.ProvinceList = provinceList;
            message.TagList = tagList;


            String pushResult = push.pushMessageToApp(message);
            System.Console.WriteLine("-----------------------------------------------");
            System.Console.WriteLine("服务端返回结果：" + pushResult);
        }

        public static void singleBatchDemo()
        {
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            IBatch batch = new BatchImpl(APPKEY, push);
            //消息模版：TransmissionTemplate:透传模板
            TransmissionTemplate templateTrans = TransmissionTemplateDemo();

            // 单推消息模型
            SingleMessage messageTrans = new SingleMessage();
            messageTrans.IsOffline = true;                         // 用户当前不在线时，是否离线存储,可选
            messageTrans.OfflineExpireTime = 1000 * 3600 * 12;            // 离线有效时间，单位为毫秒，可选
            messageTrans.Data = templateTrans;
            //判断是否客户端是否wifi环境下推送，2为4G/3G/2G，1为在WIFI环境下，0为不限制环境
            //messageTrans.PushNetWorkType = 1;  

            com.igetui.api.openservice.igetui.Target targetTrans = new com.igetui.api.openservice.igetui.Target();
            targetTrans.appId = APPID;
            targetTrans.clientId = "";
            batch.add(messageTrans, targetTrans);

            NotificationTemplate templateNoti = NotificationTemplateDemo();

            // 单推消息模型
            SingleMessage messageNoti = new SingleMessage();
            messageNoti.IsOffline = true;                         // 用户当前不在线时，是否离线存储,可选
            messageNoti.OfflineExpireTime = 1000 * 3600 * 12;            // 离线有效时间，单位为毫秒，可选
            messageNoti.Data = templateNoti;
            //判断是否客户端是否wifi环境下推送，2为4G/3G/2G，1为在WIFI环境下，0为不限制环境
            //messageNoti.PushNetWorkType = 1;  

            com.igetui.api.openservice.igetui.Target targetNoti = new com.igetui.api.openservice.igetui.Target();
            targetNoti.appId = APPID;
            targetNoti.clientId = "";
            batch.add(messageNoti, targetNoti);
            try
            {
                batch.submit();
            }
            catch (Exception e)
            {
                batch.retry();
            }
        }

        //通知透传模板动作内容
        private static NotificationTemplate NotificationTemplateModel(string appId, string appKey, string templateTitle, string templateBody, string templateTransmissionContent)
        {
            NotificationTemplate template = new NotificationTemplate();
            template.AppId = appId;
            template.AppKey = appKey;
            //通知栏标题
            template.Title = templateTitle;
            //通知栏内容     
            template.Text = templateBody;
            //通知栏显示本地图片
            template.Logo = "";
            //通知栏显示网络图标
            template.LogoURL = "";
            //应用启动类型，1：强制应用启动  2：等待应用启动
            template.TransmissionType = "0";
            //透传内容  
            template.TransmissionContent = templateTransmissionContent;
            //接收到消息是否响铃，true：响铃 false：不响铃   
            template.IsRing = true;
            //接收到消息是否震动，true：震动 false：不震动   
            template.IsVibrate = true;
            //接收到消息是否可清除，true：可清除 false：不可清除    
            template.IsClearable = true;
            //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
            //String begin = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            //String end = DateTime.Now.AddMinutes(10).ToString("yyyy-MM-dd hh:mm:ss");
            //template.setDuration(begin, end);

            return template;
        }

        //透传模板动作内容
        private static TransmissionTemplate TransmissionTemplateModel(string appId, string appKey, string templateTitle, string templateBody, string templateTransmissionContent)
        {
            TransmissionTemplate template = new TransmissionTemplate();
            template.AppId = appId;
            template.AppKey = appKey;
            //应用启动类型，1：强制应用启动 2：等待应用启动
            template.TransmissionType = "1";
            //透传内容  
            template.TransmissionContent = templateTransmissionContent;
            //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
            //String begin = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            //String end = DateTime.Now.AddMinutes(10).ToString("yyyy-MM-dd hh:mm:ss");
            //template.setDuration(begin, end);
            //APN高级推送
            APNPayload apnpayload = new APNPayload();
            DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
            alertMsg.Body = templateBody;
            alertMsg.ActionLocKey = "ActionLocKey";
            alertMsg.LocKey = "LocKey";
            alertMsg.addLocArg("LocArg");
            alertMsg.LaunchImage = "LaunchImage";
            //iOS8.2支持字段
            alertMsg.Title = templateTitle;
            alertMsg.TitleLocKey = "TitleLocKey";
            alertMsg.addTitleLocArg("TitleLocArg");

            apnpayload.AlertMsg = alertMsg;
            apnpayload.Badge = 0;
            apnpayload.ContentAvailable = 0;
            //apnpayload.Category = "";
            //apnpayload.Sound = "test1.wav";
            apnpayload.addCustomMsg("payload", templateTransmissionContent);
            template.setAPNInfo(apnpayload);

            return template;
        }

        public static NotificationTemplate NotificationTemplateDemo()
        {
            NotificationTemplate template = new NotificationTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //通知栏标题
            template.Title = "请填写通知标题";
            //通知栏内容     
            template.Text = "请填写通知内容";
            //通知栏显示本地图片
            template.Logo = "";
            //通知栏显示网络图标
            template.LogoURL = "";
            //应用启动类型，1：强制应用启动  2：等待应用启动
            template.TransmissionType = "1";
            //透传内容  
            template.TransmissionContent = "请填写透传内容";
            //接收到消息是否响铃，true：响铃 false：不响铃   
            template.IsRing = true;
            //接收到消息是否震动，true：震动 false：不震动   
            template.IsVibrate = true;
            //接收到消息是否可清除，true：可清除 false：不可清除    
            template.IsClearable = true;
            //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
            //String begin = "2015-03-06 14:36:10";
            //String end = "2015-03-06 14:46:20";
            //template.setDuration(begin, end);

            return template;
        }

        //透传模板动作内容
        public static TransmissionTemplate TransmissionTemplateDemo()
        {
            TransmissionTemplate template = new TransmissionTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //应用启动类型，1：强制应用启动 2：等待应用启动
            template.TransmissionType = "2";
            //透传内容  
            template.TransmissionContent = "{\"InfoType\":3,\"Content\":\"总质疑字段数为123，请尽快处理\",\"Data\":\"{\"ProjectId\":\"[projectId]\"}\"}";
            //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
            //String begin = "2015-03-06 14:36:10";
            //String end = "2015-03-06 14:46:20";
            //template.setDuration(begin, end);

            return template;
        }

        //网页模板内容
        public static LinkTemplate LinkTemplateDemo()
        {
            LinkTemplate template = new LinkTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //通知栏标题
            template.Title = "请填写通知标题";
            //通知栏内容 
            template.Text = "请填写通知内容";
            //通知栏显示本地图片 
            template.Logo = "";
            //通知栏显示网络图标，如无法读取，则显示本地默认图标，可为空
            template.LogoURL = "";
            //打开的链接地址    
            template.Url = "http://www.baidu.com";
            //接收到消息是否响铃，true：响铃 false：不响铃   
            template.IsRing = true;
            //接收到消息是否震动，true：震动 false：不震动   
            template.IsVibrate = true;
            //接收到消息是否可清除，true：可清除 false：不可清除
            template.IsClearable = true;
            return template;
        }

        //通知栏弹框下载模板
        public static NotyPopLoadTemplate NotyPopLoadTemplateDemo()
        {
            NotyPopLoadTemplate template = new NotyPopLoadTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //通知栏标题
            template.NotyTitle = "请填写通知标题";
            //通知栏内容
            template.NotyContent = "请填写通知内容";
            //通知栏显示本地图片
            template.NotyIcon = "icon.png";
            //通知栏显示网络图标
            template.LogoURL = "http://www-igexin.qiniudn.com/wp-content/uploads/2013/08/logo_getui1.png";
            //弹框显示标题
            template.PopTitle = "弹框标题";
            //弹框显示内容    
            template.PopContent = "弹框内容";
            //弹框显示图片    
            template.PopImage = "";
            //弹框左边按钮显示文本    
            template.PopButton1 = "下载";
            //弹框右边按钮显示文本    
            template.PopButton2 = "取消";
            //通知栏显示下载标题
            template.LoadTitle = "下载标题";
            //通知栏显示下载图标,可为空 
            template.LoadIcon = "file://push.png";
            //下载地址，不可为空
            template.LoadUrl = "http://www.appchina.com/market/d/425201/cop.baidu_0/com.gexin.im.apk ";
            //应用安装完成后，是否自动启动
            template.IsActived = true;
            //下载应用完成后，是否弹出安装界面，true：弹出安装界面，false：手动点击弹出安装界面 
            template.IsAutoInstall = true;
            //接收到消息是否响铃，true：响铃 false：不响铃
            template.IsBelled = true;
            //接收到消息是否震动，true：震动 false：不震动   
            template.IsVibrationed = true;
            //接收到消息是否可清除，true：可清除 false：不可清除    
            template.IsCleared = true;
            return template;
        }


    }
}
