package cc.smtweb.system.bpm.util;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Akmm at 14-2-14 上午10:13
 * 读取Idea信息的工具类
 */
public class IdeaUtil {
    private final static int MODE_POM = 1;

    //获取工程中的Module文件
    public static Map<String, String> getModules(String ideaPath, int mode) {
        if (mode == MODE_POM) {
            File file = new File(ideaPath + "/pom.xml");
            if (file.exists()) {
                return getModulesFromPom(ideaPath, file);
            }
        } else {
            File file = new File(ideaPath + "/.idea/modules.xml");
            if (file.exists()) return getModulesFromModule(ideaPath, file);
        }
        return null;
    }

    public static Map<String, String> getModulesFromPom(String ideaPath, File file) {
        Map<String, String> map = new HashMap<>();
        PomProject project = XmlUtil.readValue(file, PomProject.class);
        if (project == null || project.modules == null) return map;
        for (String module : project.modules) {
            File f = new File(ideaPath + "/" + module);
            String s = f.getName();
            int i = s.lastIndexOf(".");
            if (i >= 0) s = s.substring(0, i);
            map.put(s, f.getAbsolutePath());
        }
        return map;
    }

    public static Map<String, String> getModulesFromModule(String ideaPath, File file) {
        Map<String, String> map = new HashMap<>();
        IdeaProject project = XmlUtil.readValue(file, IdeaProject.class);
        if (project == null || project.component == null || project.component.modules == null) return map;
        for (Module module : project.component.modules) {
            String s = module.filepath.replace("$PROJECT_DIR$", ideaPath);
            File f = new File(s);
            s = f.getName();
            int i = s.lastIndexOf(".");
            if (i >= 0) s = s.substring(0, i);
            map.put(s, f.getParent());
        }
        return map;
    }

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JacksonXmlRootElement(localName = "project")
    public static class PomProject {
        @JacksonXmlElementWrapper(localName = "modules")
        @JacksonXmlProperty(localName = "module")
        public List<String> modules;

    }

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JacksonXmlRootElement(localName = "project")
    public static class IdeaProject {
        private Component component;

    }

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class Component {
        @JacksonXmlElementWrapper(localName = "modules")
        @JacksonXmlProperty(localName = "module")
        public List<Module> modules;
    }

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class Module {
        @JacksonXmlProperty
        private String fileurl;
        @JacksonXmlProperty
        private String filepath;
    }
}
