/*
 * Copyright (C) 2013 Aaron Madlon-Kay <aaron@madlon-kay.com>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

package org.madlonkay.supertmxmerge;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.madlonkay.supertmxmerge.data.JAXB.JAXBTmx;
import org.madlonkay.supertmxmerge.data.WriteFailedException;
import org.madlonkay.supertmxmerge.util.LocString;

/**
 *
 * @author Aaron Madlon-Kay <aaron@madlon-kay.com>
 */
public class CombineIOController {
    
    private static final Logger LOGGER = Logger.getLogger(CombineIOController.class.getName());
    
    private final PropertyChangeSupport propertySupport;
    
    public static final String PROP_INPUTISVALID = "inputIsValid";
    public static final String PROP_OUTPUTFILE = "outputFile";
    
    private List<File> files = new ArrayList<File>();
    private File outputFile;
    
    public CombineIOController() {
        propertySupport = new PropertyChangeSupport(this);
    }
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
    
    public List<File> getFiles() {
        return files;
    }
    
    public void addFile(File file) {
        if (!files.contains(file)) {
            files.add(file);
            propertySupport.firePropertyChange(PROP_INPUTISVALID, null, null);
        }
    }
    
    public void setFiles(Enumeration<File> files) {
        this.files = Collections.list(files);
        propertySupport.firePropertyChange(PROP_INPUTISVALID, null, null);
    }
    
    public void setFiles(File[] files) {
        this.files = Arrays.asList(files);
        propertySupport.firePropertyChange(PROP_INPUTISVALID, null, null);
    }
    
    public boolean getInputIsValid() {
        return files.size() >= 2;
    }
    
    public File getOutputFile() {
        return this.outputFile;
    }
    
    public void setOutputFile(File outputFile) {
        File oldOutputFile = this.outputFile;
        this.outputFile = outputFile;
        propertySupport.firePropertyChange(PROP_OUTPUTFILE, oldOutputFile, outputFile);
    }
    
    public void go() {
        JAXBTmx combined = null;
        for (File file : files) {
            try {
                JAXBTmx tmx = new JAXBTmx(file);
                if (combined == null) {
                    combined = JAXBTmx.newEmptyJAXBTmx(tmx);
                }
                combined.combine(tmx);
            } catch (Exception ex) {
                LOGGER.log(Level.SEVERE, null, ex);
            }
        }
        
        if (combined == null) {
            // User canceled out.
            return;
        }
        while (true) {
            if (getOutputFile() != null) {
                break;
            }
            // Output location not set.
            JFileChooser chooser = new JFileChooser();
            if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                setOutputFile(chooser.getSelectedFile());
            } else {
                int response = JOptionPane.showConfirmDialog(null,
                    LocString.get("confirm_cancel_save_message"),
                    LocString.get("combine_window_title"),
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE);
                if (response == JOptionPane.YES_OPTION) {
                    return;
                }
            }
        }

        try {
            combined.writeTo(getOutputFile());
        } catch (WriteFailedException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
    }
}

@@ -1218,16 +1218,62 @@
 onPane;%0A
+import org.madlonkay.supertmxmerge.data.ITmx;%0A
 import o
@@ -3398,15 +3398,8 @@
 ined
- = null
 ;%0A  
@@ -3408,51 +3408,140 @@
     
-for (File file : files) %7B%0A            try %7B
+try %7B%0A            combined = new JAXBTmx(files.get(0));%0A        %7D catch (Exception ex) %7B%0A            throw new RuntimeException(ex);
 %0A   
@@ -3545,24 +3545,26 @@
         
+%7D%0A
         
 JAXBTmx 
@@ -3559,85 +3559,294 @@
     
-JAXBTmx tmx = new JAXBTmx(file);%0A                if (combined == null) %7B%0A    
+ITmx empty = JAXBTmx.newEmptyJAXBTmx(combined);%0A        %0A        MergeController merger = new MergeController();%0A        merger.setIsTwoWayMerge(true);%0A        %0A        for (int i = 1; i %3C files.size(); i++) %7B%0A            try %7B%0A                JAXBTmx next = new JAXBTmx(files.get(i));%0A
     
@@ -3872,94 +3872,182 @@
 d = 
+(
 JAXBTmx
-.newEmptyJAXBTmx(tmx);%0A                %7D%0A                combined.combine(tmx);
+) merger.merge(empty, combined, next);%0A                if (combined == null) %7B%0A                    // User canceled out.%0A                    return;%0A                %7D
 %0A   
@@ -4169,104 +4169,8 @@
    %0A
-        if (combined == null) %7B%0A            // User canceled out.%0A            return;%0A        %7D%0A
     

Reimplement combining to be a repeated two-way merge with conflict resolution