package com.weaver.seconddev.chapanda.feishu.controller;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.authority.annotation.WeaPermission;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.Map;

@RestController
@RequestMapping("/papi/secondev/cbd/feishucard")
public class QueryFeishuCardDataController {

    private final Logger log = LoggerFactory.getLogger(QueryFeishuCardDataController.class);
    @WeaPermission(publicPermission = true)
    @PostMapping("/queryCardData")
    public JSONObject queryCardData(@RequestBody Map<String, Object> params) {

        Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            log.error("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
//        String challenge = "";
//        if(params.containsKey("challenge")){
//            challenge = params.get("challenge").toString();
//        }
//        String token = "";
//        if(params.containsKey("token")){
//            token = params.get("token").toString();
//        }
//        String type = "";
//        if(params.containsKey("type")){
//            type = params.get("type").toString();
//        }

//        dataMap.put("challenge",challenge);
//        dataMap.put("token",token);
//        dataMap.put("type",type);
//        returnJson.put("challenge",challenge);

        //        AAqzTJll9BcFE

        boolean isTrue = false;
        if(params.containsKey("event")){
            String event = params.get("event").toString();
            log.error("event:{}",event);
            if(StringUtils.isNotBlank(event)){
                isTrue = convertData(event);
            }
        }

        JSONObject returnJson = new JSONObject();
        log.error("isTrue:{}",isTrue);
        if(isTrue){
            JSONObject dataJson = new JSONObject();
            dataJson.put("template_id","AAq9MOwxqoNUF");
            JSONObject template_variable = new JSONObject();
            template_variable.put("zzrs","2000");
            template_variable.put("jdlzrs","20");
            template_variable.put("jdrzrs","30");
            template_variable.put("ycqrs","1900");
            template_variable.put("qjrs","10");
            template_variable.put("cdztrs","50");
            template_variable.put("srtx","5");
            template_variable.put("zztx","3");
            template_variable.put("zntx","1");
            dataJson.put("template_variable",template_variable);

            JSONObject cardJson = new JSONObject();
            cardJson.put("type","template");
            cardJson.put("data",dataJson);

            returnJson.put("card",cardJson);
            log.error("returnJson:{}",returnJson.toJSONString());
        }else{
            JSONObject dataJson = new JSONObject();
            dataJson.put("template_id","AAqzTJll9BcFE");
            JSONObject template_variable = new JSONObject();
            dataJson.put("template_variable",template_variable);
            JSONObject cardJson = new JSONObject();
            cardJson.put("type","template");
            cardJson.put("data",dataJson);
            returnJson.put("card",cardJson);
            log.error("returnJson:{}",returnJson.toJSONString());
        }

        return returnJson;
    }


    public boolean convertData(String data){
        boolean isTrue = false;
        if(data.contains("observer") && data.contains("owner")){
            int oberIndex = data.indexOf("observer");  //100
            int ownerIndex = data.indexOf("owner");  //10

            log.error("oberIndex:{}",oberIndex);
            log.error("ownerIndex:{}",ownerIndex);

            String ownerOpenId = "";
            String oberOpenId = "";
            if(oberIndex <= ownerIndex){
                String ownerData = data.substring(ownerIndex,data.length());
                String oberData = data.substring(oberIndex,ownerIndex);
                log.error("ownerData:{}"+ownerData);
                log.error("oberData:{}"+oberData);
                if(ownerData.contains("open_id")){
                    ownerData = ownerData.substring(ownerData.indexOf("open_id")+8);
                    if(ownerData.contains(",")){
                        ownerOpenId = ownerData.substring(0,ownerData.indexOf(","));
                    }
                }

                if(oberData.contains("open_id")){
                    oberData = oberData.substring(oberData.indexOf("open_id")+8);
                    System.out.println("oberData:"+oberData);
                    if(oberData.contains(",")){
                        oberOpenId = oberData.substring(0,oberData.indexOf(","));
                    }
                }
            }else{
                String oberData = data.substring(oberIndex,data.length());
                String ownerData = data.substring(ownerIndex,oberIndex);
                log.error("ownerData:{}"+ownerData);
                log.error("oberData:{}"+oberData);

                if(ownerData.contains("open_id")){
                    ownerData = ownerData.substring(ownerData.indexOf("open_id")+8);
                    if(ownerData.contains(",")){
                        ownerOpenId = ownerData.substring(0,ownerData.indexOf(","));
                    }
                }

                if(oberData.contains("open_id")){
                    oberData = oberData.substring(oberData.indexOf("open_id")+8);
                    if(oberData.contains(",")){
                        oberOpenId = oberData.substring(0,oberData.indexOf(","));
                    }
                }
            }

            log.error("ownerOpenId:{}"+ownerOpenId);
            log.error("oberOpenId:{}"+oberOpenId);

            if(StringUtils.isNotBlank(ownerOpenId) && StringUtils.isNotBlank(oberOpenId) && ownerOpenId.equals(oberOpenId)){
                isTrue = true;
            }
        }
        return isTrue;
    }
}
