package com.kfzs.duanduan.datashare;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 17/2/21.
 */
public class InformationHolder {
    public static final String KEY_HOLDER_SESSION_TIME = "Holder.kfzs.duanduan.data.share.session.time";
    public static final String KEY_HOLDER_SESSION_HASH = "Holder.kfzs.duanduan.data.share.session.hash";
    private static final String KEY_INFO_HOLDER = "Holder.kfzs.duanduan.data.share";
    private static final String HOLD_XML_NAME = "holder_kfzs_duanduan_data_share";


    public static void saveHash(Context ctx, Integer info, Long time) {
        SharedPreferences sp = ctx.getSharedPreferences(HOLD_XML_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(KEY_HOLDER_SESSION_HASH, info);
        edit.putLong(KEY_HOLDER_SESSION_TIME, time);
        edit.apply();
    }

    public static Integer readHash(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences(HOLD_XML_NAME, Context.MODE_PRIVATE);
        return sp.getInt(KEY_HOLDER_SESSION_HASH, 0);
    }

    public static Long readLastSaveHashTime(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences(HOLD_XML_NAME, Context.MODE_PRIVATE);
        return sp.getLong(KEY_HOLDER_SESSION_TIME, 0);
    }

    public static void save(Context ctx, String info) {
        save(ctx, KEY_INFO_HOLDER, info);
    }

    public static void save(Context ctx, String key, String info) {
        SharedPreferences sp = ctx.getSharedPreferences(HOLD_XML_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(key, info);
        edit.apply();
    }

    public static String read(Context ctx) {
        return read(ctx, KEY_INFO_HOLDER);
    }

    public static String read(Context ctx, String key) {
        SharedPreferences sp = ctx.getSharedPreferences(HOLD_XML_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, "");
    }

    public static String readClear(Context ctx) {
        return readClear(ctx, KEY_INFO_HOLDER);
    }

    public static String readClear(Context ctx, String key) {
        SharedPreferences sp = ctx.getSharedPreferences(HOLD_XML_NAME, Context.MODE_PRIVATE);
        String msg = sp.getString(key, "");
        SharedPreferences.Editor edit = sp.edit();
        edit.clear();
        edit.apply();
        return msg;
    }

    private InformationHolder() {
    }
}
