package com.yunzhupaas.base.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.ActionResultCode;
import com.yunzhupaas.base.entity.VisualdevEntity;
import com.yunzhupaas.config.ConfigValueUtil;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.exception.WorkFlowException;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.ServletUtil;
import com.yunzhupaas.util.wxutil.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 流程表单 http请求处理表单
 *
 * @author 云筑产品开发平台组
 * @version V3.4.5
 * @copyright 深圳市乐程软件有限公司（http://www.szlecheng.cn）
 * @date 2024/10/21
 */
@Component
public class FlowFormHttpReqUtils {

    private static ConfigValueUtil configValueUtil;

    @Autowired
    public void setConfigValueUtil(ConfigValueUtil configValueUtil) {
        FlowFormHttpReqUtils.configValueUtil = configValueUtil;
    }

    public Map<String, Object> info(VisualdevEntity visualdevEntity, String id, String token) {
        String requestURL = this.getReqURL(visualdevEntity, id);
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "GET", null, token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        if (actionResult == null) {
            return new HashMap<>();
        }
        Object data = actionResult.getData();
        return data != null ? JsonUtil.entityToMap(data) : new HashMap<>();
    }

    public boolean isUpdate(VisualdevEntity visualdevEntity, String id, String token) {
        String requestURL = this.getReqURL(visualdevEntity, id);
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "GET", null, token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        return actionResult != null && actionResult.getData() != null;
    }

    public void create(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
        String requestURL = this.getReqURL(visualdevEntity, id);
        map.remove("id");
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "POST", JsonUtil.getObjectToString(map), token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
        if (!b) {
            String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
            throw new WorkFlowException(msg);
        }
    }

    public void update(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
        String requestURL = this.getReqURL(visualdevEntity, id);
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "PUT", JsonUtil.getObjectToString(map), token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
        if (!b) {
            String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
            throw new WorkFlowException(msg);
        }
    }

    public void saveOrUpdate(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
        boolean update = this.isUpdate(visualdevEntity, id, token);
        if (update) {
            this.update(visualdevEntity, id, token, map);
        } else {
            this.create(visualdevEntity, id, token, map);
        }
    }


    private String getReqURL(VisualdevEntity visualdevEntity, String id) {
        HttpServletRequest request = ServletUtil.getRequest();
        //请求来源
        String requestURL = visualdevEntity.getInterfaceUrl();
        boolean isHttp = requestURL.toLowerCase().startsWith("http");
        if (!isHttp) {
            //补全(内部)
            requestURL = configValueUtil.getApiDomain() + requestURL;
        }
        return requestURL + "/" + id;
    }

    /**
     * 删除数据
     *
     * @param visualdevEntity
     * @param id
     * @param token
     * @throws WorkFlowException
     */
    public void delete(VisualdevEntity visualdevEntity, String id, String token) throws WorkFlowException {
        String requestURL = this.getReqURL(visualdevEntity, id) + "?forceDel=true";
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "DELETE", null, token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
        if (!b) {
            String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
            throw new WorkFlowException(msg);
        }
    }
}

