package com.tsi.navi.preferences;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;

import com.tsi.navi.TsiNaviApplication;

public final class PrefManager {

    public static final String APP_NAME = "tsi-navi";
    public static final String DEVICE_ID  = "deviceId";
    public static final String API_URL = "apiUrl";
    public static final String APP_VERSION  = "appVersion";
    public static final String NODE_VERSION  = "nodeVersion";
    public static final String WEBSOCKET_URL = "websocketUrl";
    public static final String DISP_AREA_CROSS = "dispAreaCross";

    public static final String MAP_TYPE = "mapType";
    public static final String MAP_RUN_ZOOM = "mapRunZoom";
    public static final String MAP_NIGHT_MODE = "mapNightMode";

    public static final String MAP_RUN_TILT = "mapRunTilt";
    public static final String MAP_RUN_TRACKING_DISTANCE = "mapRunTrackingDistance";
    public static final String MAP_RUN_DRIVE_DISTANCE = "mapRunDriveDistance";


    private static SharedPreferences get(final Context context) {
        return context.getSharedPreferences(TsiNaviApplication.packageName(), Context.MODE_PRIVATE);
    }

    public static String getString(final Context context, String pref, String def) {
        SharedPreferences prefs = PrefManager.get(context);
        String val = prefs.getString(pref, def);

        if (val == null || val.equals("") || val.equals("null"))
            return def;
        else
            return val;
    }

    public static void putString(final Context context, String pref, String val) {
        SharedPreferences prefs = PrefManager.get(context);
        SharedPreferences.Editor editor = prefs.edit();

        editor.putString(pref, val);
        editor.apply();
    }

    public static boolean getBoolean(final Context context, String pref, boolean def) {
        SharedPreferences prefs = PrefManager.get(context);
        boolean val = prefs.getBoolean(pref, def);
        return val;
    }

    public static void putBoolean(final Context context, String pref, boolean val) {
        SharedPreferences prefs = PrefManager.get(context);
        SharedPreferences.Editor editor = prefs.edit();

        editor.putBoolean(pref, val);
        editor.apply();
    }

    public static int getInt(final Context context, String pref, int def) {
        SharedPreferences prefs = PrefManager.get(context);
        return prefs.getInt(pref, def);
    }

    public static void putInt(final Context context, String pref, int val) {
        SharedPreferences prefs = PrefManager.get(context);
        SharedPreferences.Editor editor = prefs.edit();

        editor.putInt(pref, val);
        editor.apply();
    }

    public static long getLong(final Context context, String pref, long def) {
        SharedPreferences prefs = PrefManager.get(context);
        return prefs.getLong(pref, def);
    }

    public static void putLong(final Context context, String pref, long val) {
        SharedPreferences prefs = PrefManager.get(context);
        SharedPreferences.Editor editor = prefs.edit();

        editor.putLong(pref, val);
        editor.apply();
    }

    public static int encryptInteger(int i) {
        return i;
    }

    public static int decryptInteger(int i) {
        return i;
    }

    public static String getAppVersion(final Context context) {
        String versionName = PrefDefaultValue.APP_VERSION;
        try {
            PackageInfo pInfo = null;
            pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            versionName = pInfo.versionName; // build.gradle 에 기록된 VersionName
            PrefManager.setAppVersion(context, versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return PrefManager.getString(context, APP_VERSION, versionName);
    }

    public static void setAppVersion(final Context context, String version) {
        PrefManager.putString(context, APP_VERSION, version);
    }

    @SuppressLint("HardwareIds")
    public static String getAndroidId(final Context context) {
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    public static String getDeviceId(final Context context) {
        String deviceId = PrefManager.getString(context, DEVICE_ID, PrefDefaultValue.UNKNOWN_DEVICE_ID);
        if (deviceId.equals(PrefDefaultValue.UNKNOWN_DEVICE_ID)) {
            deviceId = PrefManager.getAndroidId(context);
            PrefManager.setDeviceId(context, deviceId);
        }
        return deviceId;
    }
    public static void setDeviceId(final Context context, String value){
        PrefManager.putString(context, DEVICE_ID, value);
    }

    public static String getApiUrl(final Context context) {
        return PrefDefaultValue.API_URL;    // 기본 URL 은 고정값으로 사용해야 함
        //return PrefManager.getString(context, API_URL, PrefDefaultValue.API_URL);
    }
    public static void setApiUrl(final Context context, String value){
        PrefManager.putString(context, API_URL, value);
    }

    public static String getWebsocketUrl(final Context context) {
        return PrefManager.getString(context, WEBSOCKET_URL, PrefDefaultValue.WEBSOCKET_URL);
    }
    public static void setWebsocketUrl(final Context context, String value){
        PrefManager.putString(context, WEBSOCKET_URL, value);
    }

    public static boolean getDispAreaCross(final Context context) {
        return PrefManager.getBoolean(context, DISP_AREA_CROSS, true);
    }
    public static void setDispAreaCross(final Context context, boolean value){
        PrefManager.putBoolean(context, DISP_AREA_CROSS, value);
    }

    public static String getNodeVersion(final Context context) {
        return PrefManager.getString(context, NODE_VERSION, PrefDefaultValue.NODE_VERSION);
    }
    public static void setNodeVersion(final Context context, String value){
        PrefManager.putString(context, NODE_VERSION, value);
    }

    public static String getMapType(final Context context) {
        return PrefManager.getString(context, MAP_TYPE, PrefDefaultValue.MAP_TYPE);
    }
    public static void setMapType(final Context context, String value){
        PrefManager.putString(context, MAP_TYPE, value);
    }

    public static int getMapTrackingZoom(final Context context) {
        return PrefManager.getInt(context, MAP_RUN_ZOOM, PrefDefaultValue.MAP_TRACKING_ZOOM);
    }
    public static void setMapTrackingZoom(final Context context, int value){
        PrefManager.putInt(context, MAP_RUN_ZOOM, value);
    }

    public static boolean getMapNightMode(final Context context) {
        return PrefManager.getBoolean(context, MAP_NIGHT_MODE, PrefDefaultValue.MAP_NIGHT_MODE);
    }
    public static void setMapNightMode(final Context context, boolean value){
        PrefManager.putBoolean(context, MAP_NIGHT_MODE, value);
    }

    public static void error(final Context context) {
        Toast.makeText(context, "설정정보 읽어오기 실패", Toast.LENGTH_LONG).show();
        AlertDialog.Builder b = new AlertDialog.Builder(context);//, android.R.style.Theme_DeviceDefault_Light_Dialog);
        b.setTitle("알림");
        b.setMessage("시스템 설정정보를 얻어오지 못하였습니다.\nAPP 를 종료합니다.");
        b.setCancelable(false);
        b.setPositiveButton("확인", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.finishAffinity((Activity) context);
                System.exit(0);
            }
        });
        b.show();
    }

}
