package com.wmyun.module.system.controller.admin.forward;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wmyun.framework.common.pojo.CommonResult;
import com.wmyun.module.system.controller.admin.forward.vo.ForwardReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

import static com.wmyun.framework.common.pojo.CommonResult.error;
import static com.wmyun.framework.common.pojo.CommonResult.success;


/**
 * @author zhouc
 * @date 2025/3/3 15:05
 * @className ForwardController
 * @description
 */
@Tag(name = "管理后台 -接口转发")
@RestController
@RequestMapping("/system/forward")
@Validated
@Transactional
public class ForwardController {

    @PostMapping("/interface")
    @Operation(summary = "接口转发")
    @PreAuthorize("@ss.hasPermission('system:forward:interface')")
    public CommonResult<String> Project(@Valid @RequestBody ForwardReqVO forwardReqVO) {
        try {
            JSONObject jsonObject = JSON.parseObject(JSONObject.toJSONString(forwardReqVO));
            String url = jsonObject.getString("url");
            Map<String, String> headers = new HashMap<>();
            JSONObject headparameter = jsonObject.getJSONObject("headparameter");
            for (Map.Entry<String, Object> entry : headparameter.entrySet()) {
                headers.put(entry.getKey(), entry.getValue().toString());
            }
            String body = jsonObject.getString("bodyparameter");
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpPost.setHeader(entry.getKey(), entry.getValue());
            }
            StringEntity entity = new StringEntity(body);
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                String responseBody = EntityUtils.toString(responseEntity);
                return success(responseBody);
            } else {
                return error(411, "调用第三方接口返回为空！");
            }
        } catch (Exception exception) {
            String message = exception.getMessage();
            return error(411, message);
        }
    }
}
