package org.cyclops.cyclopscore.init;

import net.minecraftforge.fml.common.versioning.ArtifactVersion;
import net.minecraftforge.fml.common.versioning.DefaultArtifactVersion;
import org.cyclops.cyclopscore.tracking.IModVersion;

/**
 * A {@link ModBase} which is also a {@link org.cyclops.cyclopscore.tracking.IModVersion}.
 * @author rubensworks
 */
public abstract class ModBaseVersionable extends ModBase implements IModVersion {

    private boolean versionInfo = false;
    private String version;
    private String info;
    private String updateUrl;

    public ModBaseVersionable(String modId, String modName, String modVersion) {
        super(modId, modName);
        putGenericReference(REFKEY_MOD_VERSION, modVersion);
    }

    @Override
    public void setVersionInfo(String version, String info, String updateUrl) {
        versionInfo = true;
        this.version = version;
        this.info = info;
        this.updateUrl = updateUrl;
    }

    @Override
    public boolean isVersionInfo() {
        return versionInfo;
    }

    @Override
    public String getVersion() {
        return version;
    }

    @Override
    public String getInfo() {
        return info;
    }

    @Override
    public String getUpdateUrl() {
        return updateUrl;
    }

    @Override
    public boolean needsUpdate() {
        String currentVersionString = getReferenceValue(REFKEY_MOD_VERSION);
        if("@VERSION@".equals(currentVersionString)) return false; // we're in this mod's dev environment
        ArtifactVersion currentVersion = new DefaultArtifactVersion(currentVersionString);
        ArtifactVersion newVersion = new DefaultArtifactVersion(getVersion());
        return getVersion() != null && currentVersion.compareTo(newVersion) < 0;
    }

}

@@ -1412,11 +1412,94 @@
     
-if(
+String latestVersionString = getVersion();%0A        if (latestVersionString == null %7C%7C 
 %22@VE
@@ -1535,16 +1535,30 @@
 String))
+ %7B%0A           
  return 
@@ -1571,14 +1571,33 @@
  // 
-w
+W
 e're 
+running offline or 
 in t
@@ -1622,16 +1622,26 @@
 ronment%0A
+        %7D%0A
         
@@ -1787,26 +1787,29 @@
 Version(
-ge
+lates
 tVersion
 ());%0A   
@@ -1800,18 +1800,22 @@
 tVersion
-()
+String
 );%0A     

Fix version checker crash when running offline, Closes #76