package com.nincraft.modpackdownloader.container;

import com.google.common.base.Strings;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.nincraft.modpackdownloader.util.Reference;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j2;
import lombok.val;
import org.apache.commons.lang3.math.NumberUtils;

import javax.annotation.Generated;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

@Generated("org.jsonschema2pojo")
@Log4j2
@Data
@EqualsAndHashCode(callSuper = false, exclude = {"fileID"})
public class CurseFile extends Mod {

	@SerializedName("fileID")
	@Expose
	public Integer fileID;
	@SerializedName("projectID")
	@Expose
	public Integer projectID;
	@SerializedName("release")
	@Expose
	public String releaseType;
	@SerializedName("skipUpdate")
	@Expose
	private Boolean skipUpdate;
	private String projectUrl;
	private String projectName;
	private boolean curseForge;
	private String fileExtension;
	private Reference reference = Reference.getInstance();

	public CurseFile() {
		curseForge = true;
		fileExtension = reference.getJarFileExt();
	}

	public CurseFile(CurseFile curseFile) {
		super(curseFile);
		fileID = curseFile.fileID;
		projectID = curseFile.projectID;
		releaseType = curseFile.releaseType;
		skipUpdate = curseFile.skipUpdate;
		projectUrl = curseFile.projectUrl;
		projectName = curseFile.projectName;
		curseForge = curseFile.curseForge;
		fileExtension = curseFile.fileExtension;
	}

	public CurseFile(String projectId, String projectName) {
		if (NumberUtils.isParsable(projectId)) {
			setProjectID(Integer.parseInt(projectId));
		}
		setProjectName(projectName);
		curseForge = true;
	}

	public String getCurseforgeWidgetJson() {
		return reference.getCurseforgeWidgetJsonMod();
	}

	@Override
	public void init() {
		setProjectUrl(buildProjectUrl());

		try {
			if (Strings.isNullOrEmpty(getProjectName()) || Strings.isNullOrEmpty(getName())) {
				val conn = (HttpURLConnection) new URL(getProjectUrl()).openConnection();
				conn.setInstanceFollowRedirects(false);
				conn.connect();

				if (Strings.isNullOrEmpty(getProjectName())) {
					setProjectName(conn.getHeaderField("Location").split("/")[2]);
				}

				if (Strings.isNullOrEmpty(getName())) {
					setName(getProjectName());
				}
			}
		} catch (final IOException e) {
			log.error(e);
		}
		setDownloadUrl(getDownloadUrl());

	}

	private String buildProjectUrl() {
		return String.format(reference.getCurseforgeBaseUrl() + "%s" + reference.getCookieTest1(), getProjectID());
	}

	public String getCurseForgeDownloadUrl() {
		String baseUrl = curseForge ? reference.getCurseforgeBaseUrl() : reference.getFtbBaseUrl();
		return String.format(baseUrl + "%s/files/%s/download", getProjectName(),
				getFileID());
	}
}
@@ -1878,16 +1878,40 @@
 nit() %7B%0A
+%09%09resolveProjectName();%0A
 %09%09setPro
@@ -1942,18 +1942,95 @@
 ));%0A
-%0A%09%09try
+%09%09setDownloadUrl(getCurseForgeDownloadUrl());%0A%09%7D%0A%0A%09private void resolveProjectName()
  %7B%0A
-%09
 %09%09if
@@ -2106,24 +2106,33 @@
 tName())) %7B%0A
+%09%09%09try %7B%0A
 %09%09%09%09val conn
@@ -2162,16 +2162,51 @@
 new URL(
+reference.getCurseforgeBaseUrl() + 
 getProje
@@ -2207,19 +2207,18 @@
 tProject
-Url
+ID
 ()).open
@@ -2296,16 +2296,89 @@
 ect();%0A%0A
+%09%09%09%09val newProjectName = conn.getHeaderField(%22Location%22).split(%22/%22)%5B2%5D;%0A%0A
 %09%09%09%09if (
@@ -2444,53 +2444,22 @@
 ame(
-conn.getHeaderField(%22Location%22).split(%22/%22)%5B2%5D
+newProjectName
 );%0A%09
@@ -2521,19 +2521,19 @@
 setName(
-get
+new
 ProjectN
@@ -2535,18 +2535,16 @@
 jectName
-()
 );%0A%09%09%09%09%7D
@@ -2552,20 +2552,16 @@
 %09%09%09%7D
-%0A%09%09%7D
  catch (
 fina
@@ -2560,14 +2560,8 @@
 ch (
-final 
 IOEx
@@ -2576,16 +2576,17 @@
 e) %7B%0A%09%09%09
+%09
 log.erro
@@ -2595,48 +2595,16 @@
 e);%0A
+%09
 %09%09%7D%0A%09%09
-setDownloadUrl(getDownloadUrl());%0A
+%7D
 %0A%09%7D%0A
@@ -2800,22 +2800,19 @@
 l() %7B%0A%09%09
-String
+val
  baseUrl
@@ -2963,21 +2963,17 @@
 tName(),
-%0A%09%09%09%09
+ 
 getFileI

Fix Project Name Resolution for CurseMods