package org.deke.risk.riskahead.helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.google.android.gms.maps.model.LatLng;

import java.util.HashMap;

/**
 *
 * (C) Copyright 2016 RiskAhead (http://www.riskahead.net)
 * Developer & Contributor: Dennis Thießen (dennis.thiessen@riskahead.net)
 * Date of Release: 10.02.2016
 *
 */
public class SessionManager {
    private static String TAG = SessionManager.class.getSimpleName();

    SharedPreferences status;
    public SharedPreferences userdata;

    Editor statusEditor;
    Editor userDataEditor;
    Context _context;

    int PRIVATE_MODE = 0;

    private static final String PREF_NAME = "AndroidLogin";
    private static final String USERDATA_NAME = "AndroidLoginUser";

    public static final String KEY_UID = "uid";
    public static final String KEY_USERNAME = "username";
    public static final String KEY_NAME = "name";
    public static final String KEY_SURNAME = "surname";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_STATUS = "status";
    public static final String PROVIDER_TYPE = "providerType";
    public static final String KEY_CREATED_AT = "created_at";
    public static final String KEY_UPDATED_AT = "updated_at";
    public static final String KEY_LASTLOGIN_AT = "lastlogin_at";
    public static final String TOKEN = "accesskey";
    public static final String KEY_IS_LOGGEDIN = "isLoggedIn";

    public static final String KEY_LOCATION_LAT = "location_latitude";
    public static final String KEY_LOCATION_LNG = "location_longitude";

    public static final String KEY_MAN_LOCATION_LAT = "man_location_latitude";
    public static final String KEY_MAN_LOCATION_LNG = "man_location_longitude";

    public static final String KEY_LAST_NOTIFICATION = "lastNotificationTime";
    public static final String KEY_NOTIFY_ENABLED = "notifyEnabled" ;
    public static final String KEY_NOTIFY_RADIUS = "notifyRadius";
    public static final String KEY_NOTIFY_POLLINGFREQ = "notifyPollingfreq";
    public static final String KEY_NOTIFY_GPS = "notifyGPSEnabled";
    public static final String KEY_NOTIFY_READY = "notifyReady";

    public static final String KEY_SUBS_NOADS = "subs_noads";
    public static final String KEY_SUBS_PREMIUM = "subs_premium";

    public static final String KEY_LAST_ADD_SHOWN = "lastaddschown";

    public SessionManager(Context context) {
        this._context = context;
        status = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        userdata = _context.getSharedPreferences(USERDATA_NAME, PRIVATE_MODE);
        statusEditor = status.edit();
        userDataEditor = userdata.edit();
    }

    public void setLogin(boolean isLoggedIn) {
        statusEditor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn);
        statusEditor.apply();
    }

    public boolean isLoggedIn(){
        return status.getBoolean(KEY_IS_LOGGEDIN, false);
    }

    public void setLocation(Long latitude, Long longitude){
        statusEditor.putLong(KEY_LOCATION_LAT, latitude);
        statusEditor.putLong(KEY_LOCATION_LNG, longitude);
        statusEditor.apply();
    }

    public LatLng getLocation(){
        return new LatLng(Double.longBitsToDouble(status.getLong(KEY_LOCATION_LAT, 0)), Double.longBitsToDouble(status.getLong(KEY_LOCATION_LNG, 0)));
    }

    public void setManualLocation(Long latitude, Long longitude){
        statusEditor.putLong(KEY_MAN_LOCATION_LAT, latitude);
        statusEditor.putLong(KEY_MAN_LOCATION_LNG, longitude);
        statusEditor.apply();
    }

    public LatLng getManualLocation(){
        return new LatLng(Double.longBitsToDouble(status.getLong(KEY_MAN_LOCATION_LAT, 0)), Double.longBitsToDouble(status.getLong(KEY_MAN_LOCATION_LNG, 0)));
    }

    public void setGPSForNotificationsEnabled(boolean isEnabled){
        statusEditor.putBoolean(KEY_NOTIFY_GPS, isEnabled);
        statusEditor.apply();
    }

    public boolean isGPSForNotificationsEnabled(){
        return status.getBoolean(KEY_NOTIFY_GPS, true);
    }

    public void setLastAddShown(Long time){
        statusEditor.putLong(KEY_LAST_ADD_SHOWN, time);
        statusEditor.apply();
    }

    public Long getLastAddShown(){
        return status.getLong(KEY_LAST_ADD_SHOWN, 0);
    }

    public void setLastNotification(String time){
        statusEditor.putString(KEY_LAST_NOTIFICATION, time);
        statusEditor.apply();
    }

    public String getLastNotification(){
        return status.getString(KEY_LAST_NOTIFICATION, "");
    }

    public void setNotificationRadius(int radius){
        statusEditor.putInt(KEY_NOTIFY_RADIUS, radius);
        statusEditor.apply();
    }

    public void setNotificationServiceReady(boolean isReady) {
        statusEditor.putBoolean(KEY_NOTIFY_READY, isReady);
        statusEditor.apply();
    }

    public boolean getNotificationServiceReady(){
        return status.getBoolean(KEY_NOTIFY_READY, false);
    }

    public void setNotificationPollFreq(int pollingFreqInMinutes){
        statusEditor.putInt(KEY_NOTIFY_POLLINGFREQ, pollingFreqInMinutes);
        statusEditor.apply();
    }

    public void setNotificationEnabled(boolean isEnabled){
        statusEditor.putBoolean(KEY_NOTIFY_ENABLED, isEnabled);
        statusEditor.apply();
    }

    public Boolean getNotificationEnabled(){
        return status.getBoolean(KEY_NOTIFY_ENABLED, false);
    }

    public void setSubsNoAds(boolean isEnabled){
        statusEditor.putBoolean(KEY_SUBS_NOADS, isEnabled);
        statusEditor.apply();
    }

    public Boolean getSubsNoAds(){
        return status.getBoolean(KEY_SUBS_NOADS, false);
    }

    public void setSubsPremium(boolean isEnabled){
        statusEditor.putBoolean(KEY_SUBS_PREMIUM, isEnabled);
        statusEditor.apply();
    }

    public Boolean getSubsPremium(){
        return status.getBoolean(KEY_SUBS_PREMIUM, false);
    }

    public int getNotificationRadius(){
        return status.getInt(KEY_NOTIFY_RADIUS, 5);
    }

    public int getNotificationPollFreq(){
        return status.getInt(KEY_NOTIFY_POLLINGFREQ, 5);
    }

    public void addUser(String uid, String username, String name, String surname, String email, String status, String provider_type, String created_at, String updated_at, String lastlogin_at, String token, boolean premium, boolean addfree) {

        userDataEditor.putString(KEY_UID, uid); //UID
        userDataEditor.putString(KEY_USERNAME, username); // Username
        userDataEditor.putString(KEY_NAME, name); // Name
        userDataEditor.putString(KEY_SURNAME, surname); // Surame
        userDataEditor.putString(KEY_EMAIL, email); // Email
        userDataEditor.putString(KEY_STATUS, status); // Status
        userDataEditor.putString(PROVIDER_TYPE, provider_type); // ProviderType
        userDataEditor.putString(KEY_CREATED_AT, created_at); // Created At
        userDataEditor.putString(KEY_UPDATED_AT, updated_at); // Updated At
        userDataEditor.putString(KEY_LASTLOGIN_AT, lastlogin_at); // Last Login
        userDataEditor.putString(TOKEN, token); // TOKEN

        setSubsNoAds(addfree);
        setSubsPremium(premium);

        userDataEditor.apply();

        Log.d(TAG, "New user written to sharedPreferences: " + uid);
    }

    public HashMap<String, String> getUserDetails() {
        HashMap<String, String> user = new HashMap<>();

        user.put(KEY_UID, userdata.getString(KEY_UID, null));
        user.put(KEY_USERNAME, userdata.getString(KEY_USERNAME, null));
        user.put(KEY_NAME, userdata.getString(KEY_NAME, null));
        user.put(KEY_SURNAME, userdata.getString(KEY_SURNAME, null));
        user.put(KEY_EMAIL, userdata.getString(KEY_EMAIL, null));
        user.put(KEY_STATUS, userdata.getString(KEY_STATUS, null));
        user.put(PROVIDER_TYPE, userdata.getString(PROVIDER_TYPE, null));
        user.put(KEY_CREATED_AT, userdata.getString(KEY_CREATED_AT, null));
        user.put(KEY_UPDATED_AT, userdata.getString(KEY_UPDATED_AT, null));
        user.put(KEY_LASTLOGIN_AT, userdata.getString(KEY_LASTLOGIN_AT, null));
        user.put(TOKEN, userdata.getString(TOKEN, null));

        Log.d(TAG, "Fetching user from sharedPrefs: " + user.toString());

        return user;
    }

    public void removeUser(){
        userdata.edit().clear().apply();
    }


}
