// Path: src/main/java/nl/matsv/paaas/data/VersionDataFile.java
// @Data
// @AllArgsConstructor
// public class VersionDataFile {
//     private final MinecraftVersion version;
//     private VersionMeta metadata;
//     private BurgerOutput burgerData;
//     private MetadataTree metadataTree;
//     private List<String> sounds;
//     // Store executed modules & versions
//     // Store module data
// }
// 
// Path: src/main/java/nl/matsv/paaas/module/ModuleLoader.java
// @Component
// public class ModuleLoader {
//     private static List<Class<? extends Module>> modules = new ArrayList<>();
// 
//     static {
//         // Add module
//         modules.add(JarModule.class);
//         modules.add(BurgerModule.class);
//         modules.add(MetadataModule.class);
//         modules.add(SoundModule.class);
//     }
// 
//     @Autowired
//     private AutowireCapableBeanFactory beanFactory;
// 
//     public <T extends Module> T initModule(Class<T> clazz) {
//         try {
//             T m = clazz.newInstance();
//             beanFactory.autowireBean(m);
//             return m;
//         } catch (Exception e) {
//             throw new RuntimeException("Failed to initialize module: " + clazz.getName(), e);
//         }
//     }
// 
//     public void runModules(VersionDataFile vdf) {
//         for (Class<? extends Module> module : modules) {
//             Module m = initModule(module);
// 
//             try {
//                 m.run(vdf);
//             } catch (Exception e) {
//                 System.out.println("Failed to run module " + m.getClass().getName() + " for version " + vdf.getVersion().getId());
//                 e.printStackTrace();
//             } finally {
//                 // Force GC
//                 WeakReference<Module> ref = new WeakReference<>(m);
//                 m = null;
//                 while (ref.get() != null) {
//                     System.gc();
//                 }
//             }
//         }
//     }
// 
//     public JsonObject compareModules(VersionDataFile oldV, VersionDataFile newV) {
//         JsonObject obj = new JsonObject();
// 
//         obj.add("oldVersion", new JsonObject());
//         obj.add("newVersion", new JsonObject());
// 
//         JsonObject oldObject = obj.get("oldVersion").getAsJsonObject();
//         JsonObject newObject = obj.get("newVersion").getAsJsonObject();
// 
//         for (Class<? extends Module> module : modules) {
//             Optional<JsonElement> oldOutput = initModule(module).compare(oldV, newV);
//             Optional<JsonElement> newOutput = initModule(module).compare(newV, oldV);
// 
//             if (oldOutput.isPresent() && newOutput.isPresent()) {
//                 oldObject.add(module.getSimpleName(), oldOutput.get());
//                 newObject.add(module.getSimpleName(), newOutput.get());
//             }
//         }
//         return obj;
//     }
// }
// 
// Path: src/main/java/nl/matsv/paaas/services/VersionService.java
// @Service
// public class VersionService {
//     private final StorageManager storageManager;
//     private Map<String, VersionDataFile> versionMap = new ConcurrentHashMap<>();
// 
//     @Autowired
//     public VersionService(StorageManager storageManager) {
//         this.storageManager = storageManager;
//         refreshVersions();
//     }
// 
//     public void refreshVersions() {
//         versionMap = new ConcurrentHashMap<>(storageManager.getVersionDataFiles());
//     }
// 
//     public List<MinecraftVersion> getEnabledVersions() {
//         return versionMap.values()
//                 .stream()
//                 .filter(file -> file.getMetadata().isEnabled())
//                 .map(VersionDataFile::getVersion)
//                 .sorted((o1, o2) -> {
//                     if (o1.getReleaseTime() == null || o2.getReleaseTime() == null)
//                         return 0;
//                     return o2.getReleaseTime().compareTo(o1.getReleaseTime());
//                 })
//                 .collect(Collectors.toList());
//     }
// 
//     public Optional<VersionDataFile> getVersion(String version) {
//         if (!versionMap.containsKey(version))
//             return Optional.empty();
//         return Optional.of(versionMap.get(version));
//     }
// }


import com.google.gson.Gson;
import nl.matsv.paaas.data.VersionDataFile;
import nl.matsv.paaas.module.ModuleLoader;
import nl.matsv.paaas.services.VersionService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
/*
 * Copyright (c) 2016 Mats & Myles
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package nl.matsv.paaas.controllers;



@RestController
@RequestMapping(value = "/v1")
public class APIController {
    @Autowired
    private ModuleLoader moduleLoader;
    @Autowired
    private VersionService versionService;
    @Autowired
    private Gson gson;


    @RequestMapping(value = "/compare", method = RequestMethod.GET)
    public String compare(@RequestParam("old") String oldVersion, @RequestParam("new") String newVersion) {
        Optional<VersionDataFile> oldData = versionService.getVersion(oldVersion);
        Optional<VersionDataFile> newData = versionService.getVersion(newVersion);

// Path: src/main/java/nl/matsv/paaas/data/VersionDataFile.java
// @Data
// @AllArgsConstructor
// public class VersionDataFile {
//     private final MinecraftVersion version;
//     private VersionMeta metadata;
//     private BurgerOutput burgerData;
//     private MetadataTree metadataTree;
//     private List<String> sounds;
//     // Store executed modules & versions
//     // Store module data
// }
// 
// Path: src/main/java/nl/matsv/paaas/module/ModuleLoader.java
// @Component
// public class ModuleLoader {
//     private static List<Class<? extends Module>> modules = new ArrayList<>();
// 
//     static {
//         // Add module
//         modules.add(JarModule.class);
//         modules.add(BurgerModule.class);
//         modules.add(MetadataModule.class);
//         modules.add(SoundModule.class);
//     }
// 
//     @Autowired
//     private AutowireCapableBeanFactory beanFactory;
// 
//     public <T extends Module> T initModule(Class<T> clazz) {
//         try {
//             T m = clazz.newInstance();
//             beanFactory.autowireBean(m);
//             return m;
//         } catch (Exception e) {
//             throw new RuntimeException("Failed to initialize module: " + clazz.getName(), e);
//         }
//     }
// 
//     public void runModules(VersionDataFile vdf) {
//         for (Class<? extends Module> module : modules) {
//             Module m = initModule(module);
// 
//             try {
//                 m.run(vdf);
//             } catch (Exception e) {
//                 System.out.println("Failed to run module " + m.getClass().getName() + " for version " + vdf.getVersion().getId());
//                 e.printStackTrace();
//             } finally {
//                 // Force GC
//                 WeakReference<Module> ref = new WeakReference<>(m);
//                 m = null;
//                 while (ref.get() != null) {
//                     System.gc();
//                 }
//             }
//         }
//     }
// 
//     public JsonObject compareModules(VersionDataFile oldV, VersionDataFile newV) {
//         JsonObject obj = new JsonObject();
// 
//         obj.add("oldVersion", new JsonObject());
//         obj.add("newVersion", new JsonObject());
// 
//         JsonObject oldObject = obj.get("oldVersion").getAsJsonObject();
//         JsonObject newObject = obj.get("newVersion").getAsJsonObject();
// 
//         for (Class<? extends Module> module : modules) {
//             Optional<JsonElement> oldOutput = initModule(module).compare(oldV, newV);
//             Optional<JsonElement> newOutput = initModule(module).compare(newV, oldV);
// 
//             if (oldOutput.isPresent() && newOutput.isPresent()) {
//                 oldObject.add(module.getSimpleName(), oldOutput.get());
//                 newObject.add(module.getSimpleName(), newOutput.get());
//             }
//         }
//         return obj;
//     }
// }
// 
// Path: src/main/java/nl/matsv/paaas/services/VersionService.java
// @Service
// public class VersionService {
//     private final StorageManager storageManager;
//     private Map<String, VersionDataFile> versionMap = new ConcurrentHashMap<>();
// 
//     @Autowired
//     public VersionService(StorageManager storageManager) {
//         this.storageManager = storageManager;
//         refreshVersions();
//     }
// 
//     public void refreshVersions() {
//         versionMap = new ConcurrentHashMap<>(storageManager.getVersionDataFiles());
//     }
// 
//     public List<MinecraftVersion> getEnabledVersions() {
//         return versionMap.values()
//                 .stream()
//                 .filter(file -> file.getMetadata().isEnabled())
//                 .map(VersionDataFile::getVersion)
//                 .sorted((o1, o2) -> {
//                     if (o1.getReleaseTime() == null || o2.getReleaseTime() == null)
//                         return 0;
//                     return o2.getReleaseTime().compareTo(o1.getReleaseTime());
//                 })
//                 .collect(Collectors.toList());
//     }
// 
//     public Optional<VersionDataFile> getVersion(String version) {
//         if (!versionMap.containsKey(version))
//             return Optional.empty();
//         return Optional.of(versionMap.get(version));
//     }
// }


// Path: src/main/java/nl/matsv/paaas/controllers/APIController.java
import com.google.gson.Gson;
import nl.matsv.paaas.data.VersionDataFile;
import nl.matsv.paaas.module.ModuleLoader;
import nl.matsv.paaas.services.VersionService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;

/*
 * Copyright (c) 2016 Mats & Myles
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package nl.matsv.paaas.controllers;



@RestController
@RequestMapping(value = "/v1")
public class APIController {
    @Autowired
    private ModuleLoader moduleLoader;
    @Autowired
    private VersionService versionService;
    @Autowired
    private Gson gson;


    @RequestMapping(value = "/compare", method = RequestMethod.GET)
    public String compare(@RequestParam("old") String oldVersion, @RequestParam("new") String newVersion) {
        Optional<VersionDataFile> oldData = versionService.getVersion(oldVersion);
        Optional<VersionDataFile> newData = versionService.getVersion(newVersion);

        if (oldData.isPresent() && newData.isPresent() && oldData.get().getMetadata().isEnabled() && newData.get().getMetadata().isEnabled()) {