package com.common.lib_base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import com.common.lib_base.BuildConfig;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


/**
 * Date: 2022/4/26 0026 17:48
 * Description:
 */
public class SpUtils {

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    /**
     * key - values 存储方式
     * 它的存储路径：data/data/packageName/shared_prefs/sp_name.xml
     * <p>
     * File存储：/sdcard/ 读写方式不一样
     * 数据库：LitePal
     * get/post:数据的读写
     */

    private volatile static SpUtils mInstance = null;

    private SpUtils(Context context) {
        sp = context.getSharedPreferences(BuildConfig.SP_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    public static SpUtils getInstance(Context context) {
        if (mInstance == null) {
            synchronized (SpUtils.class) {
                if (mInstance == null) {
                    mInstance = new SpUtils(context);
                }
            }
        }
        return mInstance;
    }


    public void initSp(Context mContext) {

        /**
         * MODE_PRIVATE：只限于本应用读写
         * MODE_WORLD_READABLE:支持其他应用读，但是不能写
         * MODE_WORLD_WRITEABLE:其他应用可以写
         */
        sp = mContext.getSharedPreferences(BuildConfig.SP_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    public void putInt(String key, int values) {
        editor.putInt(key, values);
        editor.commit();
    }

    public int getInt(String key, int defValues) {
        return sp.getInt(key, defValues);
    }

    public void putString(String key, String values) {
        editor.putString(key, values);
        editor.commit();
    }

    public String getString(String key, String defValues) {
        return sp.getString(key, defValues);
    }

    public void putBoolean(String key, boolean values) {
        editor.putBoolean(key, values);
        editor.commit();
    }

    public boolean getBoolean(String key, boolean defValues) {
        return sp.getBoolean(key, defValues);
    }

    public void deleteKey(String key) {
        editor.remove(key);
        editor.commit();
    }


    /**
     * 保存对象到本地SP中
     *
     * @param context
     * @param t
     * @param preferenceId
     * @param <T>
     */
    public static <T> void saveObj2SP(Context context, T t, String preferenceId) {
        SharedPreferences preferences = context.getSharedPreferences(BuildConfig.SP_NAME, Context.MODE_PRIVATE);
        ByteArrayOutputStream bos;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(t);
            byte[] bytes = bos.toByteArray();
            String ObjStr = Base64.encodeToString(bytes, Base64.DEFAULT);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(preferenceId, ObjStr);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.flush();
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }

    }

    /**
     * 从本地SP中读取对象
     *
     * @param context
     * @param preferenceId
     * @param <T>
     * @return
     */
    public static <T extends Object> T getObjFromSP(Context context, String preferenceId) {
        SharedPreferences preferences = context.getSharedPreferences(BuildConfig.SP_NAME, Context.MODE_PRIVATE);
        byte[] bytes = Base64.decode(preferences.getString(preferenceId, ""), Base64.DEFAULT);
        ByteArrayInputStream bis;
        ObjectInputStream ois = null;
        T obj = null;
        try {
            bis = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bis);
            obj = (T) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return obj;
    }




}

