/**
 * Copyright © CENRA (2016)
 *
 * remi.clement@espaces-naturels.fr
 * 
 * This software is a multi-platform and portable application based on 
 * the scientifical toolbox to monitor wetlands. With this toolbox it’s 
 * possible to calculate indicators «RhoMéO» to monitor quality, 
 * fonctionalities and urban and agricultural pressures of wetlands. 
 * Indicators are calculated using naturalist data entered and/or imported,
 * and geographic data downloaded from a geographic server.
 * 
 * This software is governed by the CeCILL license under French law and
 * abiding by the rules of distribution of free software.  You can  use, 
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info". 
 * 
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability. 
 * 
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or 
 * data to be ensured and,  more generally, to use and operate it in the 
 * same conditions as regards security. 
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
*/
package fr.cenra.rhomeo.api;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.sis.util.logging.Logging;

/**
 * A comparable to hold version values and allow for comparison between them.
 * This object will try to split an input string to extract a suite of digits,
 * which will compose the version. The weight of each digit in the suit is
 * determined by its encounter order in the string. First encountered are priorized
 * over the last ones.
 * 
 * Ex : the String "1.2.3" will result in the following digit suite : 1 2 3,
 * where 1 is the major version, 2 the minor version, and 3 the update version.
 *
 * @author Alexis Manin (Geomatys)
 */
public class Version implements Comparable<Version> {

    private static final Logger LOGGER = Logging.getLogger("fr.cenra.core");

    final String stringVersion;
    final int[] version;

    public Version(String inputVersion) {
        stringVersion = inputVersion == null? "" : inputVersion;

        String[] splitted = stringVersion.split("[^\\d]+");
        if (splitted.length < 1) {
            version = new int[0];
        } else {
            int[] tmpVersion = new int[splitted.length];
            try {
                for (int i = 0; i < splitted.length; i++) {
                    tmpVersion[i] = Integer.parseInt(splitted[i]);
                }
            } catch (NumberFormatException e) {
                LOGGER.log(Level.WARNING, "Input version cannot be split as a suite of numbers : "+ inputVersion, e);
                tmpVersion = new int[0];
            }

            version = tmpVersion;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int compareTo(Version o) {
        if (o == null) {
            return -1;
        } else if (version.length < 1 || o.version.length < 1) {
            return stringVersion.compareTo(o.stringVersion);
        } else {
            final int maxIndex = Math.min(version.length, o.version.length);
            int comparison;
            for (int i = 0 ; i < maxIndex ; i++) {
                comparison = version[i] - o.version[i];
                if (comparison != 0) {
                    return comparison;
                }
            }

            // if we arrived here, common version parts are equals. The longest should be the last version.
            return version.length - o.version.length;
        }
    }

    /**
     * A string representing current application version.
     *
     * @return current application version.
     */
    @Override
    public String toString() {
        return stringVersion;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Version) {
            return compareTo((Version) obj) == 0;
        }
        return false;
    }

    @Override
    public int hashCode() {
        if (version.length < 1) {
            return Arrays.hashCode(version);
        } else {
            return stringVersion.hashCode();
        }
    }


}
