package com.gh.gamecenter.core.utils;

import android.text.TextUtils;

import com.lightgame.utils.Utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

public class MD5Utils {

    public static String getUpdateMD5(String url, String content) {
        if (url == null && content == null) {
            return null;
        }
        MessageDigest digest;
        ByteArrayInputStream bais;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            bais = new ByteArrayInputStream((url + content).getBytes());
            while ((len = bais.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            bais.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }

    public static String getUrlMD5(String url) {
        if (url == null) {
            return null;
        }
        MessageDigest digest;
        ByteArrayInputStream bais;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            bais = new ByteArrayInputStream(url.getBytes());
            while ((len = bais.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            bais.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }

    public static String getContentMD5(String content) {
        if (content == null) {
            return null;
        }
        MessageDigest digest;
        ByteArrayInputStream bais;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            bais = new ByteArrayInputStream(content.getBytes());
            while ((len = bais.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            bais.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }


    public static boolean checkMD5(String md5, File updateFile) {
        if (TextUtils.isEmpty(md5) || updateFile == null) {
            Utils.log("MD5 string empty or updateFile null");
            return false;
        }

        String calculatedDigest = calculateMD5(updateFile);
        if (calculatedDigest == null) {
            Utils.log("calculatedDigest null");
            return false;
        }

        Utils.log("Calculated digest: " + calculatedDigest);
        Utils.log("Provided digest: " + md5);
        return calculatedDigest.equalsIgnoreCase(md5);
    }

    public static String calculateMD5(File updateFile) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            Utils.log("Exception while getting digest", e);
            return null;
        }

        InputStream is;
        try {
            is = new FileInputStream(updateFile);
        } catch (Exception e) {
            Utils.log("Exception while getting FileInputStream", e);
            return null;
        }

        byte[] buffer = new byte[8192];
        int read;
        try {
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            String output = bigInt.toString(16);
            // Fill to 32 chars
            output = String.format("%32s", output).replace(' ', '0');
            Utils.log("MD5:::", output);
            return output;
        } catch (Exception e) {
            throw new RuntimeException("Unable to process file for MD5", e);
        } finally {
            try {
                is.close();
            } catch (Exception e) {
                Utils.log("Exception on closing MD5 input stream", e);
            }
        }
    }
}
