package com.tapjoy.easyapp

import android.content.Context
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import com.tapjoy.TJError
import com.tapjoy.TJOfferwallDiscoverListener
import com.tapjoy.TapjoyPluginAPI
import com.tapjoy.easyapp.extensions.findActivity
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class OfferwallDiscoverViewModel : ViewModel() {

    private val _statusMessage = MutableStateFlow("")
    private val _placementInput = MutableStateFlow("offerwall_discover")
    private val _requestClick = MutableStateFlow(false)
    private val _clearClick = MutableStateFlow(false)
    private val _isOfferShown = MutableStateFlow(false)
    private val _isChecked = MutableStateFlow(false)
    private val _heightInput = MutableStateFlow("")
    private val _widthInput = MutableStateFlow("")
    private val _leftInput = MutableStateFlow("")
    private val _topInput = MutableStateFlow("")
    private val _height = MutableStateFlow(0.dp)
    private val _width = MutableStateFlow(0.dp)

    val statusMessage = _statusMessage.asStateFlow()
    val placementInput = _placementInput.asStateFlow()
    val requestClick = _requestClick.asStateFlow()
    val clearClick = _clearClick.asStateFlow()
    val isOfferShown = _isOfferShown.asStateFlow()
    val isChecked = _isChecked.asStateFlow()
    val heightInput = _heightInput.asStateFlow()
    val widthInput = _widthInput.asStateFlow()
    val leftInput = _leftInput.asStateFlow()
    val topInput = _topInput.asStateFlow()
    val height = _height.asStateFlow()
    val width = _width.asStateFlow()

    fun requestOffers(context: Context) {
        _statusMessage.value = "Requested: " + placementInput.value
        if (isChecked.value) {
            val left = leftInput.value.toFloatOrNull() ?: 0f
            val top = topInput.value.toFloatOrNull() ?: 0f
            val width = widthInput.value.toFloatOrNull() ?: 0f
            val height = heightInput.value.toFloatOrNull() ?: 0f

            val offerwallDiscoverListener = object : TJOfferwallDiscoverListener {
                override fun requestSuccess() {
                    setRequestSuccess()
                }
                override fun requestFailure(error: TJError?) {
                    setRequestFailure(error)
                }
                override fun contentReady() {
                    setContentReady(context)
                }
                override fun contentError(error: TJError?) {
                    setContentError(error)
                }
            }
            if (left == 0f || top == 0f) {
                TapjoyPluginAPI.RequestOWDiscover(context.findActivity(), placementInput.value, height, offerwallDiscoverListener)
            } else {
                TapjoyPluginAPI.RequestOWDiscover(context.findActivity(), placementInput.value, left, top, width, height, offerwallDiscoverListener)
            }
        } else {
            _height.value = (heightInput.value.toIntOrNull() ?: 0).dp
            _width.value = (widthInput.value.toIntOrNull() ?: 0).dp
            _requestClick.value = true
        }
    }

    fun clearOffers() {
        if (isChecked.value) {
            TapjoyPluginAPI.DestroyOWDiscover()
        } else {
            _clearClick.value = true
            _isOfferShown.value = false
        }
        _statusMessage.value = "Cleared content"
    }

    fun resizeOffers() {
        _height.value = (heightInput.value.toIntOrNull() ?: 0).dp
        _width.value = (widthInput.value.toIntOrNull() ?: 0).dp
    }

    fun setRequestSuccess() {
        _statusMessage.value = "Request Success"
        _isOfferShown.value = true
    }

    fun setRequestFailure(error: TJError?) {
        _statusMessage.value = "Request Failed: ${error?.message}"
    }

    fun setContentReady(context: Context) {
        _statusMessage.value = "Content Ready"
        if (isChecked.value) {
            TapjoyPluginAPI.ShowOWDiscover(context.findActivity())
        }
    }

    fun setContentError(error: TJError?) {
        _statusMessage.value = "Content Error: ${error?.code} - ${error?.message}"
    }

    fun setPluginChecked(isChecked: Boolean) {
        _isChecked.value = isChecked
    }

    fun setLeftInput(input: String) {
        _leftInput.value = input
    }

    fun setTopInput(input: String) {
        _topInput.value = input
    }

    fun setWidthInput(input: String) {
        _widthInput.value = input
    }

    fun setHeightInput(input: String) {
        _heightInput.value = input
    }

    fun setPlacementInput(input: String) {
        _placementInput.value = input
    }

    fun resetClicks() {
        _requestClick.value = false
        _clearClick.value = false
    }
}
