package usda.weru.weps.startup;

import de.schlichtherle.truezip.file.TFile;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class ReleaseVersion implements Comparable<ReleaseVersion> {

    private static final Logger LOGGER = Logger.getLogger(ReleaseVersion.class);

    /**
     *
     */
    protected String c_release;

    /**
     *
     */
    protected Integer[] c_version;

    /**
     *
     * @param text
     * @throws IllegalArgumentException
     */
    public ReleaseVersion(String text) throws IllegalArgumentException {
        try {
//            int indexOfBar = text.indexOf("_");
//            if (indexOfBar < 0) {
//                throw new IllegalArgumentException("missing _");
//            }
//            c_release = text.substring(0, indexOfBar);
//
//            List<Integer> versions = new LinkedList<Integer>();
//            int indexOfLastDot = indexOfBar;
//
//            for (int indexOfNextDot = text.indexOf(".", indexOfLastDot + 1);
//                    indexOfNextDot > 0; indexOfNextDot = text.indexOf(".", indexOfLastDot + 1)) {
//                String version = text.substring(indexOfLastDot + 1, indexOfNextDot);
//                versions.add(Integer.valueOf(version));
//                indexOfLastDot = indexOfNextDot;
//            }
//            String version = text.substring(indexOfLastDot + 1);
//            versions.add(Integer.valueOf(version));
//
//            if (versions.isEmpty()) {
//                throw new IllegalStateException("No version numbers.");
//            }
//
//            c_version = versions.toArray(new Integer[versions.size()]);

               //Not a super intelligent way to do this, but I'm copying the format
               //that was used before.
               String[] split = text.split("_");
               c_release = split[0];
               String trans = split[split.length - 1];
               String[] version = trans.split("\\.");
               List<Integer> versions = new LinkedList<Integer>();
               for(String item : version)
               {
                   versions.add(Integer.valueOf(item));
               }
               c_version = versions.toArray(new Integer[versions.size()]);

        } catch (IllegalArgumentException | IllegalStateException e) {
            throw new IllegalArgumentException("invalid format, must be release_x.y.z[.n]", e);
        }
    }

    /**
     *
     * @param text
     * @return
     */
    public static ReleaseVersion valueOf(String text) {
        return valueOf(new TFile(text));
    }

    /**
     *
     * @param file
     * @return
     */
    public static ReleaseVersion valueOf(TFile file) {
        try {
            return new ReleaseVersion(file.getName());
        } catch (IllegalArgumentException e) {
//            String path = file != null ? file.getAbsolutePath() : "NULL";
//            LOGGER.error("Unable to parse ReleaseVersion from file: " + path, e);
            return null;
        }
    }

    /**
     *
     * @param release
     * @param version
     */
    public ReleaseVersion(String release, Integer... version) {
        c_release = release;
        c_version = version;
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        buffer.append(c_release);
        boolean major = true;
        for (int i : c_version) {
            if (major) {
                buffer.append("_");
                major = false;
            } else {
                buffer.append(".");
            }
            buffer.append(String.valueOf(i));
        }
        return buffer.toString();
    }

    /**
     *
     * @param other
     * @return
     */
    public boolean isReleaseEqual(ReleaseVersion other) {
        return c_release.equals(other.c_release);
    }

    /**
     *
     * @param other
     * @return
     */
    @Override
    public int compareTo(ReleaseVersion other) {
        if (other == null) {
            return 1;
        }
        if (isReleaseEqual(other)) {
            //go through each version
            int compareSize = Math.max(c_version.length, other.c_version.length);

            for (int i = 0; i < compareSize; i++) {

                int subCompare = other.c_version[i] - c_version[i];
                if (subCompare == 0) {
                } else {
                    return subCompare;
                }
            }

        } else {
            return c_release.compareTo(other.c_release);
        }

        return 0;
    }
}
