package cn.superdesk.uniorder.openapi.api._overview;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.enums.common.CommonEnabledEnum;
import cn.superdesk.uniorder.common.enums.common.CommonSoftDeleteEnum;
import cn.superdesk.uniorder.dto.param.system.role.SysMenuOpenApiParam;
import cn.superdesk.uniorder.dubbo.consumer.sys.role.SysMenuOpenApiDubboConsumerService;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Api(value = "概览", tags = {"概览"})
@ApiSort(value = 900000)
@Slf4j
@RestController
@RequestMapping("/v0/overview")
public class OverviewController {

	@Autowired
	private WebApplicationContext applicationContext;

	@Autowired
	private SysMenuOpenApiDubboConsumerService sysMenuOpenApiDubboConsumerService;

	/**
	 * http://192.168.6.223:8089/web/v0/overview/getAllURL
	 *
	 * @return
	 */
	@RequestMapping(value = "/getAllURL", method = RequestMethod.POST)
	public String getAllURL() {

		List<Map<String, String>> resultList = new ArrayList<>();

		RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
		// 获取url与类和方法的对应信息
		Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

		for (Map.Entry<RequestMappingInfo, HandlerMethod> mappingInfoHandlerMethodEntry : map.entrySet()) {
			Map<String, String> resultMap = new LinkedHashMap<>();

			RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey();
			HandlerMethod      handlerMethod      = mappingInfoHandlerMethodEntry.getValue();

			// 类名
			resultMap.put("className", handlerMethod.getMethod().getDeclaringClass().getName());

			Annotation[] parentAnnotations = handlerMethod.getBeanType().getAnnotations();
			for (Annotation annotation : parentAnnotations) {
				if (annotation instanceof Api) {
					Api api = (Api) annotation;
					resultMap.put("classDesc", api.value());
				} else if (annotation instanceof RequestMapping) {
					RequestMapping requestMapping = (RequestMapping) annotation;
					if (null != requestMapping.value() && requestMapping.value().length > 0) {
						// 类URL
						resultMap.put("classURL", requestMapping.value()[0]);
					}
				}
			}

			// 方法名
			resultMap.put("methodName", handlerMethod.getMethod().getName());

			Annotation[] annotations = handlerMethod.getMethod().getDeclaredAnnotations();
			if (annotations != null) {
				// 处理具体的方法信息
				for (Annotation annotation : annotations) {
					if (annotation instanceof ApiOperation) {
						ApiOperation methodDesc = (ApiOperation) annotation;
						String       desc       = methodDesc.value();
						// 接口描述
						resultMap.put("methodDesc", desc);
					}
				}
			}
			PatternsRequestCondition p = requestMappingInfo.getPatternsCondition();
			for (String url : p.getPatterns()) {
				// 请求URL
				resultMap.put("methodURL", url);
			}

			RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
			for (RequestMethod requestMethod : methodsCondition.getMethods()) {

				// 请求方式:POST/PUT/GET/DELETE
				resultMap.put("requestType", requestMethod.toString());
			}
			resultList.add(resultMap);
		}


		resultList.forEach(item -> {

			SysMenuOpenApiParam param = new SysMenuOpenApiParam();

			param.setBelongWho(1L);
			param.setLevel(0);

			param.setRequestType(item.get("requestType"));

			param.setClazzUrl(item.get("classURL"));
			param.setClazzName(item.get("className"));
			param.setClazzDesc(item.get("classDesc"));

			param.setMethodUrl(item.get("methodURL"));
			param.setMethodName(item.get("methodName"));
			param.setMethodDesc(item.get("methodDesc"));

			param.setCreateTime(DateUtil.date());
			param.setCreateUserId(1L);
			param.setCreateUserName("路特");

			param.setUpdateTime(DateUtil.date());
			param.setUpdateUserId(1L);
			param.setUpdateUserName("路特");

			param.setDeleted(CommonSoftDeleteEnum.正常.getKey());
			param.setEnabled(CommonEnabledEnum.启用.getKey());


			sysMenuOpenApiDubboConsumerService.addSysMenuOpenApi(param);
		});

		return JSONUtil.toJsonStr(resultList);
	}
}
