package com.bohuikeji.delivery.backstage.util;

import com.gexin.rp.sdk.base.IBatch;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.dto.GtReq;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * App 个推配置配置
 */
@Data
@Slf4j
@Component
public class CustomerAppPush {
    /**
     * 推送消息到单个设备
     * @param title
     * @param text
     * @param cid
     * @return
     */
    public static boolean push(String title,String text,String cid){
        IGtPush push = getPUshClient();
        IPushResult res=push.pushMessageToSingle(PushClientHolder.getSingleMessage(title,text), PushClientHolder.getTarget(cid));
        if(res==null){return false;}
        log.info("推送结果：{}",res.getResponse());
        return true;
    }

    /**
     * 批量推送
     * @param title
     * @param text
     * @param cids
     * @return
     */
    public static boolean pusBatch(String title, String text, List<Object> cids) throws Exception {
        IGtPush push = getPUshClient();
        IBatch batch =push.getBatch();
        for (Object cid:cids){
            batch.add(PushClientHolder.getSingleMessage(title,text), PushClientHolder.getTarget(cid.toString()));
        }
        IPushResult res=batch.submit();
        if(res==null){return false;}
        log.info("推送结果：{}",res.getResponse());
        return true;
    }



    /**
     * 获取消息推送客户端
     * @return
     */
    private static IGtPush getPUshClient(){return PushClientHolder.pushClient;}

    /**
     * 静态内部类
     */
    private static class PushClientHolder{
        private static final String AppID = "MgVw598N4i7yraW14z1tX9";
        private static final String AppSecret = "t0xQylmD248kLgtLQoTkt7";
        private static final String AppKey = "y87Gv8Hh8l6CoC1mg1Ctn";
        private static final String MasterSecret = "cbcUKLALJ0AaKn24LuY6H8";
        private static final String HOST = "http://sdk.open.api.igexin.com/apiex.htm";

        private static IGtPush pushClient;
        static {
            pushClient=new IGtPush(HOST, AppKey, MasterSecret);
        }
        /**
         * 获取打开首页模板
         * @return
         */
        private static NotificationTemplate getNotificationTemplate(String title,String text){
            NotificationTemplate template = new NotificationTemplate();
            template.setAppId(AppID);
            template.setAppkey(AppKey);
            template.setTransmissionType(1);
            template.setTransmissionContent("请输入您要透传的内容");
            Style0 style = new Style0();
            // 设置通知栏标题与内容
            style.setTitle(title);
            style.setText(text);
            // 配置通知栏图标
            style.setLogo("icon.png");
            // 配置通知栏网络图标
            style.setLogoUrl("");
            // 设置通知是否响铃，震动，或者可清除
            style.setRing(true);
            style.setVibrate(true);
            style.setClearable(true);
            //style.setChannel("自定义channel");
            //style.setChannelName("自定义channelName");
            style.setChannelLevel(3);
            template.setStyle(style);
            //template.setAPNInfo(getAPNPayload()); //详见本页iOS通知样式设置
            // 设置定时展示时间，安卓机型可用
            // template.setDuration("2019-08-16 11:40:00", "2019-08-16 12:24:00");
            // 消息覆盖
            // template.setNotifyid(123); // 在消息推送的时候设置自定义的notifyid。如果需要覆盖此条消息，则下次使用相同的notifyid发一条新的消息。客户端sdk会根据notifyid进行覆盖。
            return template;
        }
        /**
         * 获取单推消息体
         * @param title
         * @param text
         * @return
         */
        private static SingleMessage getSingleMessage(String title,String text){
            SingleMessage message = new SingleMessage();
            message.setOffline(true);
            // 离线有效时间，单位为毫秒
            message.setOfflineExpireTime(3* 60* 1000);
            //message.setData(getNotificationTemplate(title,text));
            message.setData(getNotificationTemplate(title,text));
            // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
            message.setPushNetWorkType(0);
            // 厂商通道下发策略
            message.setStrategyJson("{\"default\":4,\"ios\":4,\"st\":4}");
            return message;
        }

        /**
         * 获取推送目标
         * @param AppID
         * @return
         */
        private static Target getTarget(String cid){
            Target target = new Target();
            target.setAppId(AppID);
            target.setClientId(cid);
            return target;
        }


        /**
         * 废弃方法
         * @param appId
         * @param appKey
         * @return
         */
        public static TransmissionTemplate transmissionTemplateDemo(String appId, String appKey){
            TransmissionTemplate template = new TransmissionTemplate();
            template.setAppId(appId);
            template.setAppkey(appKey);
            template.setTransmissionContent("透传内容");
            template.setTransmissionType(2);
            Notify notify = new Notify();
            notify.setTitle("请输入通知栏标题");
            notify.setContent("请输入通知栏内容");
            notify.setIntent("intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;package=com.tcjskd.app.qishou.android;component=com.tcjskd.app.qishou.android/io.dcloud.PandoraEntry; S.UP-OL-SU=true;S.title=测试标题;S.content=" +
                    "测试内容;S.payload=test;end");
            /*notify.setIntent("(\"intent:#Intent;launchFlags=0x10000000;package=com.pp.yl;component=com.tcjskd.app.qishou.android" +
                    "/com.getui.demo.MainActivity;i.parm1=12;end");*/
            notify.setType(GtReq.NotifyInfo.Type._intent);
            // notify.setUrl("https://dev.getui.com/");
            template.set3rdNotifyInfo(notify);//设置第三方通知
            return template;
        }

    }


}
