package com.example.modifier.data

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.provider.Settings
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.example.modifier.BuildConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlin.coroutines.coroutineContext

val Context.appPreferencesDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.appPreferences")

class AppPreferencesRepository(
    private val context: Context
) {

    private object PreferencesKeys {
        val SERVER = stringPreferencesKey("server")
        val ID = stringPreferencesKey("id")
        val NAME = stringPreferencesKey("name")
        val PREVENT_CLEAN = booleanPreferencesKey("prevent_clean")
        val PREVENT_REQUEST = booleanPreferencesKey("prevent_request")
        val PREVENT_RESET = booleanPreferencesKey("prevent_reset")
    }

    @SuppressLint("HardwareIds")
    val appPreferencesFlow = context.appPreferencesDataStore.data.map {
        val server = it[PreferencesKeys.SERVER] ?: "http://47.98.225.28"
        var id = it[PreferencesKeys.ID] ?: ""
        if (id.isEmpty()) {
            id = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
            context.appPreferencesDataStore.edit { preferences ->
                preferences[PreferencesKeys.ID] = id
            }
        }
        val name = it[PreferencesKeys.NAME] ?: Build.DEVICE
        val preventClean = it[PreferencesKeys.PREVENT_CLEAN] ?: false
        val preventRequest = it[PreferencesKeys.PREVENT_REQUEST] ?: false
        val preventReset = it[PreferencesKeys.PREVENT_RESET] ?: false
        AppPreferences(
            server = server,
            id = id,
            name = name,
            preventClean = preventClean,
            preventRequest = preventRequest,
            preventReset = preventReset
        )
    }

    suspend fun getAppPreferences() = appPreferencesFlow.stateIn(CoroutineScope(coroutineContext))

    suspend fun updateServer(server: String) {
        context.appPreferencesDataStore.edit { preferences ->
            preferences[PreferencesKeys.SERVER] = server
        }
    }

    suspend fun updateName(name: String) {
        context.appPreferencesDataStore.edit { preferences ->
            preferences[PreferencesKeys.NAME] = name
        }
    }

    suspend fun updatePreventClean(preventClean: Boolean) {
        context.appPreferencesDataStore.edit { preferences ->
            preferences[PreferencesKeys.PREVENT_CLEAN] = preventClean
        }
    }

    suspend fun updatePreventRequest(preventRequest: Boolean) {
        context.appPreferencesDataStore.edit { preferences ->
            preferences[PreferencesKeys.PREVENT_REQUEST] = preventRequest
        }
    }

    suspend fun updatePreventReset(preventReset: Boolean) {
        context.appPreferencesDataStore.edit { preferences ->
            preferences[PreferencesKeys.PREVENT_RESET] = preventReset
        }
    }
}