// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/ArduinoConfig.java
// public static final String ROOT_PLATFORM_ARCH = "avr";
// 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/ArduinoConfig.java
// public static final String ROOT_PLATFORM_VENDOR = "arduino";
// 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/pic32/PIC32Platform.java
// public class PIC32Platform extends Platform {
//     
//     public PIC32Platform(Platform parent, String vendor, Path rootPath) throws IOException {
//         super(parent, vendor, "pic32", rootPath );
//         putValue("compiler.c.cmd", "xc32-gcc");
//         putValue("compiler.c.elf.cmd", "xc32-g++");
//         putValue("compiler.cpp.cmd", "xc32-g++");
//         putValue("compiler.ar.cmd", "xc32-ar");
//         putValue("compiler.objcopy.cmd", "xc32-objcopy");
//         putValue("compiler.elf2hex.cmd", "xc32-bin2hex");
//         putValue("compiler.size.cmd", "xc32-size");
//         data.entrySet().forEach( e -> e.setValue( e.getValue().replaceAll(" -O2 ", " -O1 ") ) );
//     }
//     
// }


import static com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoConfig.ROOT_PLATFORM_ARCH;
import static com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoConfig.ROOT_PLATFORM_VENDOR;
import com.microchip.mplab.nbide.embedded.arduino.importer.pic32.PIC32Platform;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
        return rootPath != null && Files.exists(rootPath.resolve(PLATFORM_FILENAME));
    }

    private static Platform createPlatformFromFile(Platform rootPlatform, Path platformFilePath) {
        // Pattern: /home/user/.arduino15/packages/{vendor}/hardware/{architecture}/x.x.x/platform.txt
        int hardwareIndex = -1;
        for (int i = platformFilePath.getNameCount() - 1; i >= 0; i--) {
            if ("hardware".equalsIgnoreCase(platformFilePath.getName(i).toString())) {
                hardwareIndex = i;
                break;
            }
        }
        String vendor = platformFilePath.getName(hardwareIndex - 1).toString();
        String architecture = platformFilePath.getName(hardwareIndex + 1).toString();

        try {
//            if (architecture.equalsIgnoreCase("pic32")) {
                return new PIC32Platform(rootPlatform, vendor, platformFilePath.getParent());
//            } else {
//                return new Platform(rootPlatform, vendor, architecture, platformFilePath.getParent());
//            }
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, String.format("Failed to create a platform for %s / %s ", vendor, architecture), ex);
            return null;
        }

    }

    private static Platform createRootPlatform() throws IOException {
        Path arduinoPlatformPath = ArduinoConfig.getInstance().getDefaultArduinoPlatformPath().get();
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/ArduinoConfig.java
// public static final String ROOT_PLATFORM_ARCH = "avr";
// 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/ArduinoConfig.java
// public static final String ROOT_PLATFORM_VENDOR = "arduino";
// 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/pic32/PIC32Platform.java
// public class PIC32Platform extends Platform {
//     
//     public PIC32Platform(Platform parent, String vendor, Path rootPath) throws IOException {
//         super(parent, vendor, "pic32", rootPath );
//         putValue("compiler.c.cmd", "xc32-gcc");
//         putValue("compiler.c.elf.cmd", "xc32-g++");
//         putValue("compiler.cpp.cmd", "xc32-g++");
//         putValue("compiler.ar.cmd", "xc32-ar");
//         putValue("compiler.objcopy.cmd", "xc32-objcopy");
//         putValue("compiler.elf2hex.cmd", "xc32-bin2hex");
//         putValue("compiler.size.cmd", "xc32-size");
//         data.entrySet().forEach( e -> e.setValue( e.getValue().replaceAll(" -O2 ", " -O1 ") ) );
//     }
//     
// }


// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/PlatformFactory.java
import static com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoConfig.ROOT_PLATFORM_ARCH;
import static com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoConfig.ROOT_PLATFORM_VENDOR;
import com.microchip.mplab.nbide.embedded.arduino.importer.pic32.PIC32Platform;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

        return rootPath != null && Files.exists(rootPath.resolve(PLATFORM_FILENAME));
    }

    private static Platform createPlatformFromFile(Platform rootPlatform, Path platformFilePath) {
        // Pattern: /home/user/.arduino15/packages/{vendor}/hardware/{architecture}/x.x.x/platform.txt
        int hardwareIndex = -1;
        for (int i = platformFilePath.getNameCount() - 1; i >= 0; i--) {
            if ("hardware".equalsIgnoreCase(platformFilePath.getName(i).toString())) {
                hardwareIndex = i;
                break;
            }
        }
        String vendor = platformFilePath.getName(hardwareIndex - 1).toString();
        String architecture = platformFilePath.getName(hardwareIndex + 1).toString();

        try {
//            if (architecture.equalsIgnoreCase("pic32")) {
                return new PIC32Platform(rootPlatform, vendor, platformFilePath.getParent());
//            } else {
//                return new Platform(rootPlatform, vendor, architecture, platformFilePath.getParent());
//            }
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, String.format("Failed to create a platform for %s / %s ", vendor, architecture), ex);
            return null;
        }

    }

    private static Platform createRootPlatform() throws IOException {
        Path arduinoPlatformPath = ArduinoConfig.getInstance().getDefaultArduinoPlatformPath().get();
        return new Platform(null, ROOT_PLATFORM_VENDOR, ROOT_PLATFORM_ARCH, arduinoPlatformPath);