package kr.co.vividnext.sodalive.common

import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kr.co.vividnext.sodalive.settings.notification.MemberRole

object SharedPreferenceManager {
    private lateinit var sharedPreferences: SharedPreferences

    fun init(context: Context) {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
    }

    fun clear() {
        sharedPreferences.edit { it.clear() }
    }

    private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
        val editor = this.edit()
        operation(editor)
        editor.apply()
    }

    private operator fun SharedPreferences.set(key: String, value: Any?) {
        when (value) {
            is String? -> edit { it.putString(key, value) }
            is Int -> edit { it.putInt(key, value) }
            is Boolean -> edit { it.putBoolean(key, value) }
            is Float -> edit { it.putFloat(key, value) }
            is Long -> edit { it.putLong(key, value) }
            else -> throw UnsupportedOperationException("Error")
        }
    }

    @Suppress("UNCHECKED_CAST")
    private operator fun <T> SharedPreferences.get(key: String, defaultValue: T? = null): T {
        return when (defaultValue) {
            is String, null -> getString(key, defaultValue as? String) as T
            is Int -> getInt(key, defaultValue as? Int ?: -1) as T
            is Boolean -> getBoolean(key, defaultValue as? Boolean ?: false) as T
            is Float -> getFloat(key, defaultValue as? Float ?: -1f) as T
            is Long -> getLong(key, defaultValue as? Long ?: -1) as T
            else -> throw UnsupportedOperationException("Error")
        }
    }

    var token: String
        get() = sharedPreferences[Constants.PREF_TOKEN, ""]
        set(value) {
            sharedPreferences[Constants.PREF_TOKEN] = value
        }

    var userId: Long
        get() = sharedPreferences[Constants.PREF_USER_ID, 0]
        set(value) {
            sharedPreferences[Constants.PREF_USER_ID] = value
        }

    var nickname: String
        get() = sharedPreferences[Constants.PREF_NICKNAME, ""]
        set(value) {
            sharedPreferences[Constants.PREF_NICKNAME] = value
        }

    var email: String
        get() = sharedPreferences[Constants.PREF_EMAIL, ""]
        set(value) {
            sharedPreferences[Constants.PREF_EMAIL] = value
        }

    var profileImage: String
        get() = sharedPreferences[Constants.PREF_PROFILE_IMAGE, ""]
        set(value) {
            sharedPreferences[Constants.PREF_PROFILE_IMAGE] = value
        }

    var can: Int
        get() = sharedPreferences[Constants.PREF_CAN, 0]
        set(value) {
            sharedPreferences[Constants.PREF_CAN] = value
        }

    var role: String
        get() = sharedPreferences[Constants.PREF_USER_ROLE, MemberRole.USER.name]
        set(value) {
            sharedPreferences[Constants.PREF_USER_ROLE] = value
        }

    var isAuth: Boolean
        get() = sharedPreferences[Constants.PREF_IS_ADULT, false]
        set(value) {
            sharedPreferences[Constants.PREF_IS_ADULT] = value
        }

    var isAdultContentVisible: Boolean
        get() = sharedPreferences[Constants.PREF_IS_ADULT_CONTENT_VISIBLE, true]
        set(value) {
            sharedPreferences[Constants.PREF_IS_ADULT_CONTENT_VISIBLE] = value
        }

    var contentPreference: Int
        get() = sharedPreferences[Constants.PREF_CONTENT_PREFERENCE, 0]
        set(value) {
            sharedPreferences[Constants.PREF_CONTENT_PREFERENCE] = value
        }

    var pushToken: String
        get() = sharedPreferences[Constants.PREF_PUSH_TOKEN, ""]
        set(value) {
            sharedPreferences[Constants.PREF_PUSH_TOKEN] = value
        }

    var isFollowedCreatorLive: Boolean
        get() = sharedPreferences[Constants.PREF_IS_FOLLOWED_CREATOR_LIVE, false]
        set(value) {
            sharedPreferences[Constants.PREF_IS_FOLLOWED_CREATOR_LIVE] = value
        }

    var isContentPlayLoop: Boolean
        get() = sharedPreferences[Constants.PREF_IS_CONTENT_PLAY_LOOP, false]
        set(value) {
            sharedPreferences[Constants.PREF_IS_CONTENT_PLAY_LOOP] = value
        }

    var notShowingEventPopupId: Long
        get() = sharedPreferences[Constants.PREF_NOT_SHOWING_EVENT_POPUP_ID, 0]
        set(value) {
            sharedPreferences[Constants.PREF_NOT_SHOWING_EVENT_POPUP_ID] = value
        }

    var isViewedOnboardingTutorial: Boolean
        get() = sharedPreferences[Constants.PREF_IS_VIEWED_ON_BOARDING_TUTORIAL, false]
        set(value) {
            sharedPreferences[Constants.PREF_IS_VIEWED_ON_BOARDING_TUTORIAL] = value
        }

    var noChatRoomList: List<Long>
        get() {
            val list = sharedPreferences[Constants.PREF_NO_CHAT_ROOM, ""]
            val gson = Gson()
            val listType = object : TypeToken<List<Long>>() {}.type
            val myList = gson.fromJson<List<Long>>(list, listType)
            return myList ?: emptyList()
        }
        set(value) {
            val gson = Gson()
            val listJson = gson.toJson(value)
            sharedPreferences[Constants.PREF_NO_CHAT_ROOM] = listJson
        }
}
