package com.fs.gtPush.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.gtPush.domain.PushReqBean;
import com.fs.gtPush.domain.PushResult;
import com.fs.gtPush.domain.UniPushLog;
import com.fs.gtPush.service.UniPushLogService;
import com.fs.gtPush.service.uniPush2Service;
import com.fs.im.service.OpenIMService;
import org.springframework.beans.factory.annotation.Autowired;
import com.fs.gtPush.utils.PushUtils;
import com.fs.his.domain.FsUser;
import com.fs.his.service.IFsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class uniPush2ServiceImpl implements uniPush2Service {
    @Autowired
    private OpenIMService openIMService;
    private static final String url = "https://fc-mp-de6e03a9-c1a3-439b-9eec-d0dc3c565e4e.next.bspapp.com/push";

    @Autowired
    private IFsUserService userService;

    @Autowired
    private UniPushLogService logService;

    @Override
    public PushResult pushMessage(PushReqBean push) {
        String result = HttpUtil.post(url, push.toString());
        PushResult pushResult = JSONUtil.toBean(result, PushResult.class);
        return pushResult;
    }
    @Override
    public void pushSopAppLinkMsgByExternalIM(String cropId, String linkTile, String linkDescribe,String linkImageUrl, String link, Long companyUserId,Long fsUserId) throws JsonProcessingException {

        if (companyUserId!=null&&fsUserId!=null && fsUserId!=0){
            openIMService.sendCourse(fsUserId,companyUserId,link,linkTile,linkImageUrl,cropId);
        }

    }

    @Override
    public void pushOne(Long userId, Long businessId, String purl, String title, String content, Float type, Integer desType) {
        PushReqBean param = getParam(userId, purl,title,content,type,desType,"");
        PushResult pushResult = null;
        UniPushLog pushAddLog = new UniPushLog();
        if (param != null) {
            pushResult = pushMessage(param); //推送消息
            pushAddLog.setJpushId(param.getPush_clientid().toString());
            pushAddLog.setPushMsg(JSON.toJSONString(param));
            pushAddLog.setType(type);
            pushAddLog.setDesType(desType);
            pushAddLog.setUserId(userId);
            pushAddLog.setBusinessId(businessId);
            pushAddLog.setCreateTime(DateUtils.getNowDate());
            UniPushLog uniPushLog = PushUtils.returnMsg(pushResult,pushAddLog);
            logService.insertUniPushLog(uniPushLog);
        }
    }

    @Override
    public PushReqBean getParam(Long userId,String purl,String title,String content,Float type,Integer desType,String imJsonString){
        if (userId !=null) {
            FsUser fsUser = userService.selectFsUserByUserId(userId);
            if (fsUser !=null){
                String jpushId = fsUser.getJpushId();
                if (StringUtils.isNotBlank(jpushId) && !"0".equals(jpushId) && !"string".equals(jpushId)){
                    HashMap<String, Object> map = new HashMap<>();
                    HashMap<String, Object> map2 = new HashMap<>();
                    if (StringUtils.isNotBlank(purl)){
                        map.put("url",purl);
                        if (StringUtils.isNotEmpty(imJsonString)){
                            map.put("data",imJsonString);
                        }
                    }
                    if (content.length()>50 || title.length()>20){
                        return null;//通知栏标题，长度小于20;通知栏内容，长度小于50
                    }
                    Map<String, Object> xmConfig = new HashMap<>();
                    Map<String, Object> vvConfig = new HashMap<>();

                    if (type!=null){
                        String channel = "";
                        if (type.toString().contains("0")){
                            channel = "113892"; //订单
                        } else if (type.toString().contains("1")){
                            channel = "113891";
                            vvConfig.put("/classification",0);
                        } else if (type.toString().contains("2")){
                            channel = "113791"; //系统类
                            vvConfig.put("/classification",1);
                        }
                        if (StringUtils.isNotBlank(channel)){
                            xmConfig.put("/extra.channel_id", channel);
                            map2.put("XM", xmConfig);
                        }
                        vvConfig.put("/category","IM");
                        map2.put("VV",vvConfig);

                    }
                    return new PushReqBean(
                            jpushId,
                            title,
                            content,
                            map,
                            map2
                    );
                }
            }
        }
        return null;
    }


}
