
package de.andreasgiemza.jgeagle.gui;

import de.andreasgiemza.jgeagle.repo.data.EagleFile;
import de.andreasgiemza.jgeagle.options.Options;
import de.andreasgiemza.jgeagle.panels.ImageViewerPanel;
import de.andreasgiemza.jgeagle.repo.Repo;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import org.eclipse.jgit.revwalk.RevCommit;

/**
 *
 * @author Andreas Giemza
 */
public class SheetLayerDiffImage {

    private final Options options;
    private final JComboBox sheetComboBox;
    private final JComboBox layerComboBox;
    private final JButton diffImageButton;

    public SheetLayerDiffImage(
            Options options,
            JComboBox sheetComboBox,
            JComboBox layerComboBox,
            JButton diffImageButton) {
        this.options = options;
        this.sheetComboBox = sheetComboBox;
        this.layerComboBox = layerComboBox;
        this.diffImageButton = diffImageButton;
    }

    public void reset() {
        sheetComboBox.setEnabled(false);
        sheetComboBox.removeAllItems();
        layerComboBox.setEnabled(false);
        layerComboBox.removeAllItems();
        diffImageButton.setEnabled(false);
    }

    @SuppressWarnings("unchecked")
    public void brdSelected(Options options,
            Repo repo,
            EagleFile eagleFile,
            RevCommit oldCommit,
            RevCommit newCommit) {
        reset();

        List<String> oldLayers = repo.getOrCreateLayersFile(options, oldCommit, eagleFile, "old.brd");
        List<String> newLayers = repo.getOrCreateLayersFile(options, newCommit, eagleFile, "new.brd");

        for (String layer : oldLayers) {
            if (newLayers.contains(layer)) {
                layerComboBox.addItem(layer);
            }
        }

        layerComboBox.setEnabled(true);
        diffImageButton.setEnabled(true);
    }

    @SuppressWarnings("unchecked")
    public void schSelected(
            Repo repo,
            EagleFile eagleFile,
            RevCommit oldCommit,
            RevCommit newCommit) {
        reset();

        int oldCount = repo.getorCreateSheetCountFile(options, oldCommit, eagleFile, "old.sch");
        int newCount = repo.getorCreateSheetCountFile(options, newCommit, eagleFile, "new.sch");

        for (int i = 1; i <= Math.min(oldCount, newCount); i++) {
            sheetComboBox.addItem(i);
        }

        sheetComboBox.setEnabled(true);
        diffImageButton.setEnabled(true);
    }

    public void createDiffImage(
            Repo repo,
            EagleFile eagleFile,
            RevCommit oldCommit,
            RevCommit newCommit)
            throws IOException, InterruptedException {
        Path diffImageFile;
        String titleExtraText;

        if (eagleFile.getFileExtension().equals(EagleFile.BRD)) {
            String layer = (String) layerComboBox.getSelectedItem();

            diffImageFile = repo.getOrCreateBoardDiffImage(
                    options,
                    eagleFile,
                    oldCommit,
                    newCommit,
                    layer);

            titleExtraText = " - Layer " + layer;
        } else {
            int sheet = (int) sheetComboBox.getSelectedItem();

            diffImageFile = repo.getOrCreateSchematicDiffImage(
                    options,
                    eagleFile,
                    oldCommit,
                    newCommit,
                    sheet);

            titleExtraText = " - Sheet " + sheet;
        }

        if (diffImageFile != null) {
            ImageViewerPanel.showImageViewer(options, diffImageFile, eagleFile, titleExtraText);
        }
    }
}
