package com.izouma.awesomeadmin.web;

import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.util.EncodingTool;
import com.izouma.awesomeadmin.util.ObjectTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dev")
public class DevelopController {
    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @RequestMapping(value = "/mappings", method = RequestMethod.GET)
    @ResponseBody
    public Result list() {
        List<HashMap<String, String>> urlList = new ArrayList<>();

        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            HashMap<String, String> hashMap = new HashMap<>();
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();
            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                hashMap.put("url", url);
            }
            hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
            hashMap.put("method", method.getMethod().getName()); // 方法名
            RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
            String type = methodsCondition.toString();
            if (type != null && type.startsWith("[") && type.endsWith("]")) {
                type = type.substring(1, type.length() - 1);
                hashMap.put("type", type); // 方法名
            }
            urlList.add(hashMap);
        }
        return new Result(true, urlList);
    }

    @RequestMapping(value = "/selectMappings", method = RequestMethod.GET)
    @ResponseBody
    public Result selectMappings() {
        List<HashMap<String, String>> urlList = new ArrayList<>();

        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            HashMap<String, String> hashMap = new HashMap<>();
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();
            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                hashMap.put("url", url);
            }

            if (!hashMap.get("url").startsWith("/activiti")
                    && !hashMap.get("url").startsWith("/sysRole")
                    && !hashMap.get("url").startsWith("/powerInfo")
                    && !hashMap.get("url").startsWith("/genCode")
                    && !hashMap.get("url").startsWith("/commonQuery")
                    && !hashMap.get("url").startsWith("/dataSourceInfo")
                    && !hashMap.get("url").startsWith("/queryConfig")
                    && !hashMap.get("url").startsWith("/sysDatarule")
                    && !hashMap.get("url").startsWith("/sysMenu")
                    && !hashMap.get("url").startsWith("/genTestTable")
                    && !hashMap.get("url").startsWith("/tableField")
                    && hashMap.get("url").endsWith("/all")) {

                hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
                hashMap.put("method", method.getMethod().getName()); // 方法名
                RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
                String type = methodsCondition.toString();
                if (type != null && type.startsWith("[") && type.endsWith("]")) {
                    type = type.substring(1, type.length() - 1);
                    hashMap.put("type", type); // 方法名
                }

                String className = "com.izouma.awesomeadmin.model." + EncodingTool.firstUpperCase(hashMap.get("url").split("/")[1]);
                String[] fileds = ObjectTool.getFiledName(className);
                String filedsStr = String.join(",", fileds)
                        .replace(",delFlag", "")
                        .replace(",searchKey", "")
                        .replace(",advancedQuery", "")
                        .replace(",orderByStr", "");

                hashMap.put("fileds", filedsStr);

                urlList.add(hashMap);
            }
        }
        return new Result(true, urlList);
    }
}
