package com.fs.gtPush.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fs.gtPush.domain.PushResult;
import com.fs.gtPush.domain.UniPushLog;

import java.util.ArrayList;
import java.util.Map;

public class PushUtils {

    /**
     * 推送单个 返回结果解析
     * @param pushResult
     * @param addLog
     */
    public static UniPushLog returnMsg(PushResult pushResult, UniPushLog addLog) {
        if(pushResult != null){
            if ("success".equals(pushResult.getErrMsg())){
                Map<String, Object> dataMap = JSON.parseObject(JSON.toJSONString(pushResult.getData()), new TypeReference<Map<String, Object>>() {});
                if (dataMap != null) {
                    for (Map.Entry<String, Object> outerEntry : dataMap.entrySet()) {
                        String outerKey = outerEntry.getKey(); // 外层键
                        Map<String, String> innerMap = (Map<String, String>) outerEntry.getValue(); // 内层 Map
                        System.out.println("Outer Key: " + outerKey);
                        // 遍历内层 Map
                        for (Map.Entry<String, String> innerEntry : innerMap.entrySet()) {
                            String innerKey = innerEntry.getKey();
                            String innerValue = innerEntry.getValue();
                            System.out.println("    Inner Key: " + innerKey + ", Inner Value: " + innerValue);
                            //匹配对应log (因不确定返回值是否按序排列 )
                            if (addLog.getJpushId().equals(innerKey)) {
                                addLog.setReturnMsg(innerValue);
                                if (innerValue.contains("successed")) {
                                    addLog.setPushRes(1);
                                } else {
                                    addLog.setPushRes(0);
                                }
                            }
                        }
                    }
                }

            } else {
                    addLog.setPushRes(0);
                    addLog.setReturnMsg(pushResult.getErrMsg());
            }
        } else {
            addLog.setPushRes(0);
        }
        return addLog;
    }

    /**
     * 推送列表 返回结果解析
     * @param pushResult
     * @param addLogs
     */
    public static ArrayList<UniPushLog> returnArrayMsg(PushResult pushResult, ArrayList<UniPushLog> addLogs) {
        if(pushResult != null){
            if ("success".equals(pushResult.getErrMsg())){
                Map<String, Object> dataMap = JSON.parseObject(JSON.toJSONString(pushResult.getData()), new TypeReference<Map<String, Object>>() {});
                if (dataMap != null) {

                    for (Map.Entry<String, Object> outerEntry : dataMap.entrySet()) {
                        String outerKey = outerEntry.getKey(); // 外层键
                        Map<String, String> innerMap = (Map<String, String>) outerEntry.getValue(); // 内层 Map
                        System.out.println("Outer Key: " + outerKey);
                        // 遍历内层 Map
                        int i = 0;
                        for (Map.Entry<String, String> innerEntry : innerMap.entrySet()) {
                            String innerKey = innerEntry.getKey();
                            String innerValue = innerEntry.getValue();
                            System.out.println("    Inner Key: " + innerKey + ", Inner Value: " + innerValue);
                            if (addLogs.get(i).getJpushId().equals(innerKey)) {
                                if (innerValue.contains("successed")){
                                    addLogs.get(i).setPushRes(1);

                                } else {
                                    addLogs.get(i).setPushRes(0);
                                }
                                addLogs.get(i).setReturnMsg(innerValue);
                            } else {
                                //匹配对应log (因不确定返回值是否按序排列 )
                                for (UniPushLog addLog : addLogs) {
                                    if (addLog.getJpushId().equals(innerKey)) {
                                        addLogs.get(i).setReturnMsg(innerValue);
                                        if (innerValue.contains("successed")) {
                                            addLog.setPushRes(1);
                                        } else {
                                            addLog.setPushRes(0);
                                        }
                                        addLogs.get(i).setReturnMsg(innerValue);
                                        break;
                                    }
                                }
                            }
                            i++;
                        }
                    }
                }

            } else {
                addLogs.forEach(log->{
                    log.setPushRes(0);
                    log.setReturnMsg(pushResult.getErrMsg());
                });
            }

        } else {
            addLogs.forEach(log->{
                log.setPushRes(0);
            });
        }
        return addLogs;
    }
}
