package com.duowenlvshi.duowenlszp.app.util;

import com.alibaba.fastjson.JSON;
import com.duowenlvshi.duowenlszp.app.HuaWeiPush.HuaWeiPusher;
import com.duowenlvshi.duowenlszp.app.push.AndroidNotification;
import com.duowenlvshi.duowenlszp.app.push.android.AndroidCustomizedcast;
import com.duowenlvshi.duowenlszp.app.push.ios.IOSCustomizedcast;
import com.duowenlvshi.duowenlszp.model.Push.server.PushServer;
import com.duowenlvshi.duowenlszp.model.lawyer.server.LawyerServer;
import com.duowenlvshi.duowenlszp.app.push.PushClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author yang fang yu
 * @since 2019/9/6 17:44
 */
@Slf4j
@Service
public class AllPush {
    @Autowired
    PushClient pushClient;
    @Autowired
    AllPush allPush;
    @Autowired
    LawyerServer lawyerServer;
    @Autowired
    HuaWeiPusher huaWeiPusher;
    @Autowired
    PushServer pushServer;


    @Value("${YM.androidAppKey}")
    private String androidAppKey;

    @Value("${YM.androidAppMasterSecret}")
    private String androidAppMasterSecret;

    @Value("${YM.androidAliasType}")
    private String androidAliasType;

    @Value("${YM.androidPattern}")
    private String androidPattern;



    @Value("${YM.iosAppKey}")
    private String iosAppKey;

    @Value("${YM.iosAppMasterSecret}")
    private String iosAppMasterSecret;

    @Value("${YM.iosAliasType}")
    private String iosAliasType;

    @Value("${YM.iosPattern}")
    private String iosPattern;


    /**
     * 所有推送入口
     * */
    public void AllPush(Map<String,String> pushMap,Map extraFieldMap){
        try {
            if("android".equals(lawyerServer.selectDeviceType(pushMap.get("lawyerId")))){
                Map map1 = lawyerServer.selectPushToken(pushMap.get("lawyerId"));
                if(map1 != null){
                    boolean    useAble =(boolean) map1.get("useAble");
                    if(useAble){
                        String  pushToken =(String) map1.get("pushToken");
                        pushMap.put("pushToken",pushToken);
                        allPush.HuaWeiPush(pushMap);
                    }else{
                        allPush.AndroidPush(pushMap);
                    }
                }else{
                    allPush.AndroidPush(pushMap);
                }
            }else if("ios".equals(lawyerServer.selectDeviceType(pushMap.get("lawyerId")))){
                allPush.IosPush(pushMap);
            }

            Map jsonContext = new HashMap();
            jsonContext.put("title",pushMap.get("Title"));
            jsonContext.put("msg",pushMap.get("Text"));
            jsonContext.put("extraFieldMap",extraFieldMap);
            extraFieldMap.put("type",pushMap.get("type"));

            Map message = new HashMap();
            message.put("messageId",UUIDUtil.GetUUID());
            message.put("createDate",new Date());
            message.put("jsonContext",JSON.toJSONString(jsonContext));
            message.put("msgType","LSZP");
            message.put("receiverId",pushMap.get("lawyerId"));
            message.put("type",pushMap.get("type"));
            message.put("updateDate",new Date());
            pushServer.insertMessage(message);

            } catch (Exception e) {
             e.printStackTrace();
        }
    }

    /**
     * 友盟安卓推送
     * */
    public void AndroidPush(Map map) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(androidAppKey,androidAppMasterSecret);
        customizedcast.setAlias((String) map.get("lawyerId"),androidAliasType);
        customizedcast.setTicker((String)map.get("Ticker"));
        customizedcast.setTitle((String)map.get("Title"));
        customizedcast.setText((String)map.get("Text"));
        customizedcast.goAppAfterOpen();
        customizedcast.setExtraField(map);
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        if("test".equals(androidPattern)){
            customizedcast.setTestMode();
        }else{
            customizedcast.setProductionMode();
        }
        pushClient.send(customizedcast);
        log.info("安卓推送"+map.get("lawyerId"));
    }
    /**
     *友盟苹果推送
     * */
    public void IosPush(Map map) throws Exception {
        IOSCustomizedcast customizedcast = new IOSCustomizedcast(iosAppKey,iosAppMasterSecret);
        customizedcast.setAlias((String) map.get("lawyerId"),iosAliasType);
        customizedcast.setAlert((String) map.get("Alert"));
        customizedcast.setBadge( 0);
        customizedcast.setSound( "default");
        if("test".equals(iosPattern)){
            customizedcast.setTestMode();
        }else{
            customizedcast.setProductionMode();
        }
        customizedcast.setExtraField(map);
        pushClient.send(customizedcast);
        log.info("苹果推送"+map.get("lawyerId")+map.get("Alert"));
    }
    /**
     *华为推送
     * */
    public void HuaWeiPush(Map map) throws Exception {
        if("1".equals(map.get("NcOrTrans")) || "3".equals(map.get("NcOrTrans"))){//Push通知栏消息
            huaWeiPusher.sendToLawyerNcMsg(map);
        }
        if("2".equals(map.get("NcOrTrans")) || "3".equals(map.get("NcOrTrans"))){//Push透传消息
            huaWeiPusher.sendToLawyerTransMsg(map);
        }
        log.info("华为推送");
    }

}
