package net.cobem.grizzly.licensing;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.utils.ParameterString;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class LicenseServer {
    private String publicKey;

    private String serverResponse;

    public LicenseServer() {
        this.publicKey = buildPublicKey();

        this.sendDetails();
    }

    private void sendDetails() {
        try {
            URLConnection Connection = new URL("http://cobem.net/crackhead/mhmmhmmhm.php").openConnection();

            Connection.setDoOutput(true);

            OutputStreamWriter Writer = new OutputStreamWriter(Connection.getOutputStream());

            Writer.write(this.buildURL(Grizzly.getConfig().get("licensing.grizzly.key")));

            Writer.flush();

            saveServerResponse(Connection);

        } catch (IOException e) {
        }
    }

    private String buildURL(String licenseKey) {
        return new ParameterString("license={license_key}&public={public}").append("license_key", licenseKey).append("public", publicKey).toString();
    }

    private String buildPublicKey() {
        String Key = "";
        String alphaChars = "abcdefghijklmnopqrstuvwxyz";

        for (int i = 0; i < 25; i++) {
            if (Grizzly.rand(0, 1) == 0) {
                String character = alphaChars.split("")[Grizzly.rand(0, 26)];

                if (Grizzly.rand(0, 1) == 1) {
                    character = character.toUpperCase();
                }

                Key += character;
            } else {
                Key += Grizzly.rand(0, 9);
            }
        }

        return Key;
    }

    private void saveServerResponse(URLConnection Connection) {
        try {
            BufferedReader Reader = new BufferedReader(new InputStreamReader(Connection.getInputStream()));

            while (Reader.ready()) {
                this.serverResponse = Reader.readLine();
            }

            Reader.close();
        } catch (Exception ignored) {
        }
    }

    public ResponseType getResponse() {
        try {
            if (this.serverResponse.equals(this.calculateSuccess())) {
                return ResponseType.SUCCESS;
            //} else if (this.serverResponse.isEmpty() || this.serverResponse.equals("")) {
            } else {
                return ResponseType.ERROR;
            }
        }
        catch(Exception ignored) {
            return ResponseType.CONNECTION;
        }
    }

    private String calculateSuccess() {
        try {
            return String.format("%s%s", publicKey.substring(12, 18), String.format("%s%s", publicKey.replace(publicKey.substring(0, 6), ""), publicKey.substring(0, 6)).replace(publicKey.substring(12, 18), "")).replace(String.format("%s%s", publicKey.substring(12, 18), String.format("%s%s", publicKey.replace(publicKey.substring(0, 6), ""), publicKey.substring(0, 6)).replace(publicKey.substring(12, 18), "")).substring(0, 1), "");
        } catch (Exception ignored) {
        }

        return null;
    }

    private static String md5(String inputSring) throws NoSuchAlgorithmException {
        String md5String = inputSring.toString();
        byte[] defaultBytes = inputSring.getBytes();
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(defaultBytes);
            byte messageDigest[] = algorithm.digest();

            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
            }

            md5String = hexString.toString();
        } catch (NoSuchAlgorithmException nsae) {

        }
        return md5String;
    }
}
