package kr.rvs.ksecurity.util;

import org.bukkit.Bukkit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Junhyeong Lim on 2017-10-19.
 */
public class Version {
    private static final Pattern PATTERN = Pattern.compile("(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?");
    public static final Version BUKKIT = new Version(Bukkit.getBukkitVersion());
    public static Version PLUGIN = new Version(4, 0, 0);
    private int major = 0;
    private int minor = 0;
    private int maintenance = 0;

    public static void init(String version) {
        PLUGIN = new Version(version);
    }

    public Version(int major, int minor, int maintenance) {
        this.major = major;
        this.minor = minor;
        this.maintenance = maintenance;
    }

    public Version(String version) {
        Matcher matcher = PATTERN.matcher(version);
        if (matcher.find()) {
            String majorStr = matcher.group(1);
            String minorStr = matcher.group(2);
            String maintenance = matcher.group(3);

            if (majorStr != null)
                this.major = Integer.parseInt(majorStr);
            if (minorStr != null)
                this.minor = Integer.parseInt(minorStr);
            if (maintenance != null)
                this.maintenance = Integer.parseInt(maintenance);
        }
    }

    public boolean after(Version version) {
        return major > version.major || minor > version.minor || maintenance > version.maintenance;
    }

    public boolean afterEquals(Version version) {
        return major == version.major ? minor == version.minor ?
                maintenance >= version.maintenance : minor >= version.minor : major >= version.major;
    }

    public boolean before(Version version) {
        return major < version.major || minor < version.minor || maintenance < version.maintenance;
    }

    public boolean beforeEquals(Version version) {
        return major == version.major ? minor == version.minor ?
                maintenance <= version.maintenance : minor <= version.minor : major <= version.minor;
    }

    public int getMajor() {
        return major;
    }

    public int getMinor() {
        return minor;
    }

    public int getMaintenance() {
        return maintenance;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Version version = (Version) o;

        if (major != version.major) return false;
        if (minor != version.minor) return false;
        return maintenance == version.maintenance;
    }

    @Override
    public int hashCode() {
        int result = major;
        result = 31 * result + minor;
        result = 31 * result + maintenance;
        return result;
    }

    @Override
    public String toString() {
        String target = maintenance > 0 ? "%d.%d.%d" : "%d.%d";
        Object[] args = maintenance > 0 ? new Integer[] {major, minor, maintenance} : new Integer[] {major, minor};
        return String.format(target, args);
    }
}

@@ -285,16 +285,26 @@
 (%5C%5Cd+))?
+(?:-(.*))?
 %22);%0A    
@@ -437,16 +437,62 @@
  0, 0);%0A
+    public static String RELEASE = %22RELEASE%22;%0A
     priv
@@ -569,16 +569,50 @@
 nce = 0;
+%0A    private String tag = RELEASE;
 %0A%0A    pu
@@ -753,16 +753,28 @@
 ntenance
+, String tag
 ) %7B%0A    
@@ -857,32 +857,173 @@
  = maintenance;%0A
+        this.tag = tag;%0A    %7D%0A%0A    public Version(int major, int minor, int maintenance) %7B%0A        this(major, minor, maintenance, RELEASE);%0A
     %7D%0A%0A    publi
@@ -1278,16 +1278,59 @@
 roup(3);
+%0A            String tag = matcher.group(4);
 %0A%0A      
@@ -1616,17 +1616,178 @@
         
-%7D
+    if (tag != null)%0A                this.tag = tag.toUpperCase();%0A        %7D%0A    %7D%0A%0A    public boolean isRelease() %7B%0A        return tag.equalsIgnoreCase(RELEASE);
 %0A    %7D%0A%0A
@@ -1920,32 +1920,87 @@
 sion.maintenance
+%0A                %7C%7C isRelease() && !version.isRelease()
 ;%0A    %7D%0A%0A    pub
@@ -2061,158 +2061,41 @@
 urn 
-major == version.major ? minor == version.minor ?%0A                maintenance %3E= version.maintenance : minor %3E= version.minor : major %3E= 
+equals(version) %7C%7C after(
 version
-.major
+)
 ;%0A  
@@ -2234,32 +2234,87 @@
 sion.maintenance
+%0A                %7C%7C !isRelease() && version.isRelease()
 ;%0A    %7D%0A%0A    pub
@@ -2376,158 +2376,42 @@
 urn 
-major == version.major ? minor == version.minor ?%0A                maintenance %3C= version.maintenance : minor %3C= version.minor : major %3C= 
+equals(version) %7C%7C before(
 version
-.minor
+)
 ;%0A  
@@ -2590,32 +2590,88 @@
 tenance;%0A    %7D%0A%0A
+    public String getTag() %7B%0A        return tag;%0A    %7D%0A%0A
     @Override%0A  

Support version tag