package com.miraclegarden.smsmessage;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 监听日志持久化，App 在后台时也能保留历史，回到监听页可恢复显示。
 */
public final class MessageLogStore {

    private static final String SP_NAME = "message_log";
    private static final String KEY_LINES = "lines";
    private static final int MAX_LINES = 500;
    private static final String SEP = "\u001E";

    private MessageLogStore() {
    }

    public static void append(Context context, String message) {
        if (context == null || TextUtils.isEmpty(message)) {
            return;
        }
        Context appContext = context.getApplicationContext();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        String line = format.format(new Date()) + " " + message;

        SharedPreferences sp = appContext.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        String raw = sp.getString(KEY_LINES, "");
        List<String> lines = decode(raw);
        lines.add(line);
        while (lines.size() > MAX_LINES) {
            lines.remove(0);
        }
        sp.edit().putString(KEY_LINES, encode(lines)).apply();
    }

    public static String getDisplayText(Context context) {
        if (context == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (String line : getLines(context)) {
            sb.append(line).append('\n');
        }
        return sb.toString();
    }

    public static List<String> getLines(Context context) {
        if (context == null) {
            return new ArrayList<>();
        }
        SharedPreferences sp = context.getApplicationContext()
                .getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return decode(sp.getString(KEY_LINES, ""));
    }

    public static void clear(Context context) {
        if (context == null) {
            return;
        }
        context.getApplicationContext()
                .getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
                .edit()
                .remove(KEY_LINES)
                .apply();
    }

    private static List<String> decode(String raw) {
        List<String> lines = new ArrayList<>();
        if (TextUtils.isEmpty(raw)) {
            return lines;
        }
        for (String part : raw.split(SEP, -1)) {
            if (!TextUtils.isEmpty(part)) {
                lines.add(part);
            }
        }
        return lines;
    }

    private static String encode(List<String> lines) {
        if (lines == null || lines.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < lines.size(); i++) {
            if (i > 0) {
                sb.append(SEP);
            }
            sb.append(lines.get(i));
        }
        return sb.toString();
    }
}
