package com.miraclegarden.smsmessage;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class App extends Application {
    private static final String TAG = "App";
    public static String Hash_value;

    @Override
    public void onCreate() {
        super.onCreate();
        String packageName = getApplicationContext().getPackageName();
        MessageDigest messageDigest = getMessageDigest();
        String signature = getSignature(this, packageName);
        Hash_value = getHashCode(packageName, messageDigest, signature);
    }

    public static MessageDigest getMessageDigest() {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "No Such Algorithm.", e);
        }
        return messageDigest;
    }

    public static String getSignature(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        Signature[] signatureArrs;
        try {
            signatureArrs = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Package name inexistent.");
            return "";
        }
        if (null == signatureArrs || 0 == signatureArrs.length) {
            Log.e(TAG, "signature is null.");
            return "";
        }
        return signatureArrs[0].toCharsString();
    }

    private String getHashCode(String packageName, MessageDigest messageDigest, String signature) {
        String appInfo = packageName + " " + signature;
        messageDigest.update(appInfo.getBytes(StandardCharsets.UTF_8));
        byte[] hashSignature = messageDigest.digest();
        hashSignature = Arrays.copyOfRange(hashSignature, 0, 9);
        String base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING | Base64.NO_WRAP);
        base64Hash = base64Hash.substring(0, 11);
        return base64Hash;
    }


    public static void saveNotiList(Context context, String value) {
        SharedPreferences sp = context.getSharedPreferences("server", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("saveNotiList", value);
        editor.apply();
    }

    public static List<MessageInfo> getNotiList(Context context) {
        SharedPreferences sp = context.getSharedPreferences("server", Activity.MODE_PRIVATE);
        String messages = sp.getString("saveNotiList", "");
        if (TextUtils.isEmpty(messages)) {
            return null;
        }
        List<MessageInfo> messageInfos = GsonUtils.getListFromJSON(messages, MessageInfo.class);
        return messageInfos;
    }


    public static void deleteNotiBean(Context context, MessageInfo appInfo) {
        List<MessageInfo> messageInfos = getNotiList(context);
        if (messageInfos == null) {
            saveNotiList(context, "");
            return;
        }
        for (int i = 0; i < messageInfos.size(); i++) {
            if (messageInfos.get(i).getPackageName().equals(appInfo.getPackageName())) {
                messageInfos.remove(i);
                if (messageInfos.size() == 0) {
                    saveNotiList(context, "");
                } else {
                    saveNotiList(context, GsonUtils.beanToJSONString(messageInfos));
                }
                break;
            }
        }
    }


    public static void saveNotiBean(Context context, AppInfo appInfo) {
        List<MessageInfo> messageInfos = getNotiList(context);
        if (messageInfos == null) {
            messageInfos = new ArrayList<>();
            messageInfos.add(MessageInfo.AppInfoToMessageInfo(appInfo));
            saveNotiList(context, GsonUtils.beanToJSONString(messageInfos));
            return;
        }
        boolean isAdd = true;
        for (int i = 0; i < messageInfos.size(); i++) {
            if (messageInfos.get(i).getPackageName().equals(appInfo.getPackageName())) {
                messageInfos.get(i).setName(appInfo.getName());
                messageInfos.get(i).setCode(appInfo.getCode());
                messageInfos.get(i).setRemark(appInfo.getRemark());
                isAdd = false;
            }
        }
        if(isAdd){
            messageInfos.add(MessageInfo.AppInfoToMessageInfo(appInfo));
        }
        saveNotiList(context, GsonUtils.beanToJSONString(messageInfos));
    }


    public static AppInfo getAppInfoByNotiList(Context context, AppInfo appInfo) {
        List<MessageInfo> messageInfos = getNotiList(context);
        if (messageInfos == null) {
            return appInfo;
        }
        for (int i = 0; i < messageInfos.size(); i++) {
            if (messageInfos.get(i).getPackageName().equals(appInfo.getPackageName())) {
                appInfo.setName(messageInfos.get(i).getName());
                appInfo.setCode(messageInfos.get(i).getCode());
                appInfo.setRemark(messageInfos.get(i).getRemark());
                return appInfo;
            }
        }
        return appInfo;
    }


    public static MessageInfo getMessageByNotiList(Context context, String packageName) {
        List<MessageInfo> messageInfos = getNotiList(context);
        if (messageInfos == null) {
            return null;
        }
        for (int i = 0; i < messageInfos.size(); i++) {
            if (messageInfos.get(i).getPackageName().equals(packageName)) {
                return messageInfos.get(i);
            }
        }
        return null;
    }

}
