package com.tapjoy.easyapp

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tapjoy.TJSegment
import com.tapjoy.TJSetUserIDListener
import com.tapjoy.TJStatus
import com.tapjoy.Tapjoy
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class UserViewModel : ViewModel() {

    private val tjPrivacyPolicy = Tapjoy.getPrivacyPolicy()

    private val _userIdInput = MutableStateFlow("")
    private val _userLevelInput = MutableStateFlow("")
    private val _maxLevelInput = MutableStateFlow("")
    private val _userTagInput = MutableStateFlow("")
    private val _usPrivacyInput = MutableStateFlow("")

    private val _segmentSelected = MutableStateFlow("Not set")
    private val _gdprSelected = MutableStateFlow("Not set")
    private val _consentSelected = MutableStateFlow("Not set")
    private val _belowAgeSelected = MutableStateFlow("Not set")
    private val _usageStatsSelected = MutableStateFlow("Not set")
    private val _optOutOfAdvertisingId = MutableStateFlow(false)

    var userIdInput = _userIdInput.asStateFlow()
    var userLevelInput = _userLevelInput.asStateFlow()
    val maxLevelInput = _maxLevelInput.asStateFlow()
    val userTagInput = _userTagInput.asStateFlow()
    val usPrivacyInput = _usPrivacyInput.asStateFlow()

    val segmentSelected = _segmentSelected.asStateFlow()
    val gdprSelected = _gdprSelected.asStateFlow()
    val consentSelected = _consentSelected.asStateFlow()
    val belowAgeSelected = _belowAgeSelected.asStateFlow()
    val usageStatsSelected = _usageStatsSelected.asStateFlow()
    val optOutOfAdvertisingId = _optOutOfAdvertisingId.asStateFlow()

    val segmentList = listOf("Not set", "Non-Payer", "Payer", "VIP")
    val privacyList = listOf("Not set", "True", "False")

    private val _toastMessage = MutableSharedFlow<String>()
    val toastMessage = _toastMessage.asSharedFlow()

    fun setUserInfo() {
        Tapjoy.setUserID(
            userIdInput.value,
            object : TJSetUserIDListener {
                override fun onSetUserIDSuccess() {
                    viewModelScope.launch {
                        _toastMessage.emit("User ID set")
                    }
                }
                override fun onSetUserIDFailure(code: Int, error: String?) {
                    viewModelScope.launch {
                        _toastMessage.emit("User ID failed: $error code: $code")
                    }
                }
            },
        )
        Tapjoy.setUserLevel(userLevelInput.value.toIntOrNull() ?: -1)
        Tapjoy.setMaxLevel(maxLevelInput.value.toIntOrNull() ?: -1)
    }

    fun clearUserInput() {
        Tapjoy.setUserLevel(-1)
        Tapjoy.setMaxLevel(-1)
        _userIdInput.value = ""
        _userLevelInput.value = ""
        _maxLevelInput.value = ""
        viewModelScope.launch {
            _toastMessage.emit("Inputs cleared")
        }
    }

    fun addUserTag() {
        Tapjoy.addUserTag(userTagInput.value)
        _userTagInput.value = ""
    }

    fun removeUserTag() {
        Tapjoy.removeUserTag(userTagInput.value)
        _userTagInput.value = ""
    }

    fun clearUserTags() {
        Tapjoy.clearUserTags()
        _userTagInput.value = ""
    }

    fun initUserDetails(context: Context) {
        when (Tapjoy.getUserSegment()) {
            TJSegment.NON_PAYER -> _segmentSelected.value = segmentList[1]
            TJSegment.PAYER -> _segmentSelected.value = segmentList[2]
            TJSegment.VIP -> _segmentSelected.value = segmentList[3]
            else -> _segmentSelected.value = segmentList[0]
        }

        val userLevel = Tapjoy.getUserLevel()
        _userLevelInput.value = userLevel.toString() + ""

        val maxLevel = Tapjoy.getMaxLevel()
        _maxLevelInput.value = maxLevel.toString() + ""

        val userId = Tapjoy.getUserID()
        _userIdInput.value = userId ?: ""

        _optOutOfAdvertisingId.value = Tapjoy.getOptOutAdvertisingID(context)

        when (tjPrivacyPolicy.subjectToGDPR) {
            TJStatus.TRUE -> _gdprSelected.value = privacyList[1]
            TJStatus.FALSE -> _gdprSelected.value = privacyList[2]
            else -> _gdprSelected.value = privacyList[0]
        }
        when (tjPrivacyPolicy.userConsent) {
            TJStatus.TRUE -> _consentSelected.value = privacyList[1]
            TJStatus.FALSE -> _consentSelected.value = privacyList[2]
            else -> _consentSelected.value = privacyList[0]
        }
        when (tjPrivacyPolicy.belowConsentAge) {
            TJStatus.TRUE -> _belowAgeSelected.value = privacyList[1]
            TJStatus.FALSE -> _belowAgeSelected.value = privacyList[2]
            else -> _belowAgeSelected.value = privacyList[0]
        }
        when (tjPrivacyPolicy.usageStatsConsent) {
            TJStatus.TRUE -> _usageStatsSelected.value = privacyList[1]
            TJStatus.FALSE -> _usageStatsSelected.value = privacyList[2]
            else -> _usageStatsSelected.value = privacyList[0]
        }
        if (tjPrivacyPolicy.usPrivacy == null) return
        _usPrivacyInput.value = tjPrivacyPolicy.usPrivacy
    }

    fun setUSPrivacy() {
        val usPrivacyValue: String = usPrivacyInput.value
        tjPrivacyPolicy.usPrivacy = usPrivacyValue
        viewModelScope.launch {
            if (usPrivacyValue.isEmpty()) {
                _toastMessage.emit("Removed US Privacy Value.")
            } else {
                _toastMessage.emit("Saved US Privacy Value.")
            }
        }
    }

    fun updateUserIdInput(input: String) {
        _userIdInput.value = input
    }

    fun updateUserLevelInput(input: String) {
        _userLevelInput.value = input
    }

    fun updateMaxLevelInput(input: String) {
        _maxLevelInput.value = input
    }

    fun updateUserTagInput(input: String) {
        _userTagInput.value = input
    }

    fun updateUSPrivacyInput(input: String) {
        _usPrivacyInput.value = input
    }

    fun updateSegmentSelected(selected: String) {
        _segmentSelected.value = selected
        when (segmentList.indexOf(selected)) {
            1 -> Tapjoy.setUserSegment(TJSegment.NON_PAYER)
            2 -> Tapjoy.setUserSegment(TJSegment.PAYER)
            3 -> Tapjoy.setUserSegment(TJSegment.VIP)
            else -> Tapjoy.setUserSegment(TJSegment.UNKNOWN)
        }
    }

    fun updateGDPRSelected(selected: String) {
        _gdprSelected.value = selected
        when (privacyList.indexOf(selected)) {
            1 -> tjPrivacyPolicy.subjectToGDPR = TJStatus.TRUE
            2 -> tjPrivacyPolicy.subjectToGDPR = TJStatus.FALSE
            else -> tjPrivacyPolicy.subjectToGDPR = TJStatus.UNKNOWN
        }
    }

    fun updateConsentSelected(selected: String) {
        _consentSelected.value = selected
        when (privacyList.indexOf(selected)) {
            1 -> tjPrivacyPolicy.userConsent = TJStatus.TRUE
            2 -> tjPrivacyPolicy.userConsent = TJStatus.FALSE
            else -> tjPrivacyPolicy.userConsent = TJStatus.UNKNOWN
        }
    }

    fun updateUsageStatsConsentSelected(selected: String) {
        _usageStatsSelected.value = selected
        when (privacyList.indexOf(selected)) {
            1 -> tjPrivacyPolicy.usageStatsConsent = TJStatus.TRUE
            2 -> tjPrivacyPolicy.usageStatsConsent = TJStatus.FALSE
            else -> tjPrivacyPolicy.usageStatsConsent = TJStatus.UNKNOWN
        }
    }

    fun updateOptOutAdvertisingId(context: Context, selected: Boolean) {
        Tapjoy.optOutAdvertisingID(context, selected)
        _optOutOfAdvertisingId.value = selected
    }

    fun updateBelowAgeSelected(selected: String) {
        _belowAgeSelected.value = selected
        when (privacyList.indexOf(selected)) {
            1 -> tjPrivacyPolicy.belowConsentAge = TJStatus.TRUE
            2 -> tjPrivacyPolicy.belowConsentAge = TJStatus.FALSE
            else -> tjPrivacyPolicy.belowConsentAge = TJStatus.UNKNOWN
        }
    }
}
