package com.foxx.androidcast.remoteaccess;

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

/**
 * Last remote-access / VPN tunnel event for developer network self-test.
 */
public final class RemoteAccessStatusStore {
    private static final String PREFS = "androidcast_remote_access_status";
    private static final String KEY_TYPE = "tunnel_type";
    private static final String KEY_STATE = "state";
    private static final String KEY_METHOD = "method";
    private static final String KEY_INITIATOR = "initiator";
    private static final String KEY_FAIL_REASON = "fail_reason";
    private static final String KEY_UPDATED_MS = "updated_ms";
    private static final String KEY_SESSION_ID = "session_id";

    public static final String STATE_SUCCESS = "success";
    public static final String STATE_FAILURE = "failure";
    public static final String STATE_CONNECTING = "connecting";
    public static final String STATE_IDLE = "idle";
    public static final String STATE_OFF = "off";

    public static final String INITIATOR_DEVICE = "device";
    public static final String INITIATOR_BE = "backend";
    public static final String INITIATOR_USER = "user";

    private RemoteAccessStatusStore() {}

    public static void record(
            Context context,
            RemoteAccessMode mode,
            String state,
            String method,
            String initiator,
            String failReason,
            String sessionId) {
        String type = mode == null || mode == RemoteAccessMode.DISABLED
                ? "off" : mode.name().toLowerCase();
        prefs(context).edit()
                .putString(KEY_TYPE, type)
                .putString(KEY_STATE, state != null ? state : STATE_IDLE)
                .putString(KEY_METHOD, method != null ? method : "")
                .putString(KEY_INITIATOR, initiator != null ? initiator : "")
                .putString(KEY_FAIL_REASON, failReason != null ? failReason : "")
                .putString(KEY_SESSION_ID, sessionId != null ? sessionId : "")
                .putLong(KEY_UPDATED_MS, System.currentTimeMillis())
                .apply();
    }

    public static Snapshot load(Context context) {
        SharedPreferences p = prefs(context);
        return new Snapshot(
                p.getString(KEY_TYPE, "off"),
                p.getString(KEY_STATE, STATE_OFF),
                p.getString(KEY_METHOD, ""),
                p.getString(KEY_INITIATOR, ""),
                p.getString(KEY_FAIL_REASON, ""),
                p.getString(KEY_SESSION_ID, ""),
                p.getLong(KEY_UPDATED_MS, 0L));
    }

    private static SharedPreferences prefs(Context context) {
        return context.getApplicationContext().getSharedPreferences(PREFS, Context.MODE_PRIVATE);
    }

    public static final class Snapshot {
        public final String type;
        public final String state;
        public final String method;
        public final String initiator;
        public final String failReason;
        public final String sessionId;
        public final long updatedMs;

        Snapshot(
                String type,
                String state,
                String method,
                String initiator,
                String failReason,
                String sessionId,
                long updatedMs) {
            this.type = type != null ? type : "off";
            this.state = state != null ? state : STATE_OFF;
            this.method = method != null ? method : "";
            this.initiator = initiator != null ? initiator : "";
            this.failReason = failReason != null ? failReason : "";
            this.sessionId = sessionId != null ? sessionId : "";
            this.updatedMs = updatedMs;
        }
    }
}
