package com.sambath.kunkhmer.app

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.sambath.kunkhmer.base.BaseViewModel
import com.sambath.kunkhmer.remote.*
import com.sambath.kunkhmer.remote.service.EventListener
import com.sambath.kunkhmer.remote.service.ApiService
import com.sambath.kunkhmer.remote.service.ConnectivityStates
import com.sambath.kunkhmer.remote.service.SocketService
import com.sambath.kunkhmer.util.PrefHelper
import org.json.JSONObject
import java.net.URISyntaxException

class ShareViewModel(
    private val apiService: ApiService,
    private val prefHelper: PrefHelper,
    private val socketService: SocketService
) : BaseViewModel(), EventListener {
    private val gson = Gson()

    //Socket Data LiveData
    private val newCreatedBetting = MutableLiveData<JSONObject>()
    val getBettingCreated: LiveData<JSONObject> get() = newCreatedBetting

    //Socket Connection Live Data
    private val connectivityStateLD = MutableLiveData<Int>()
    val getConnectivityStates: LiveData<Int> get() = connectivityStateLD

    // Remote
    fun startListenSocket(token: String, channelType: Int) {
        try {
            socketService.registerListener(this)
            socketService.startListening(token, channelType)
        } catch (e: URISyntaxException) {
            e.printStackTrace()
        }
    }

    fun emitCfCreateBetting(createBettingObject: JSONObject) {
        socketService.startEmitCfCreateBetting(createBettingObject)
    }

    fun stopListeningToSocketServer() {
        socketService.unregisterListener(this)
        socketService.stopListening()
    }

    // Server callbacks

    // Server callbacks
    override fun onConnect() {
        connectivityStateLD.postValue(ConnectivityStates.STATE_CONNECTED)
    }

    override fun onReconnectionTimeout() {
        connectivityStateLD.postValue(ConnectivityStates.STATE_TIMEOUT)
    }

    override fun onConnectionError() {
        connectivityStateLD.postValue(ConnectivityStates.STATE_NOT_CONNECTED)
    }

    override fun onReconnect() {
        connectivityStateLD.postValue(ConnectivityStates.STATE_RECONNECTING)
    }

    override fun onDisconnect() {
        connectivityStateLD.postValue(ConnectivityStates.STATE_DISCONNECTED)
    }

    private val fakeBetting = MutableLiveData<String>()
    val getFakeBetting: LiveData<String> = fakeBetting

    private val viewResult = MutableLiveData<String>()
    val getViewResult: LiveData<String> = viewResult

    private val infoBalanceBetting = MutableLiveData<String>()
    val getInfoBalanceBetting: LiveData<String> = infoBalanceBetting

    private val updateChanelDeveloper = MutableLiveData<String>()
    val onUpdateChanelDeveloper: LiveData<String> = updateChanelDeveloper

    private val summaryBetting = MutableLiveData<String>()
    val getSummaryBetting: LiveData<String> = summaryBetting

    private val updateChannelInfo = MutableLiveData<String>()
    val getUpdateChannelInfo: LiveData<String> = updateChannelInfo
    override fun onUpdateChannelInfo(data: String?) {
        updateChannelInfo.postValue(data)
    }

    private val definedResultAndChannel = MutableLiveData<String>()
    val getDefinedResultAndChannel: LiveData<String> = definedResultAndChannel
    override fun onDefinedResultAndChannel(data: String?) {
        definedResultAndChannel.postValue(data)
    }

    private val updateUserInfo = MutableLiveData<String>()
    val getUpdateUserInfo: LiveData<String> = updateUserInfo
    override fun onUpdateUserInfo(data: String?) {
        updateUserInfo.postValue(data)
    }

    private val updateBettingAmount = MutableLiveData<String>()
    val onUpdateBettingAmount: LiveData<String> = updateBettingAmount
    override fun onUpdateBettingAmount(data: String?) {
        updateBettingAmount.postValue(data)
    }

    private val updatePayout = MutableLiveData<String>()
    val getUpdatePayout: LiveData<String> = updatePayout
    override fun onUpdatePayout(data: String?) {
        updatePayout.postValue(data)
    }

    private val reverseRseult = MutableLiveData<String>()
    val onReverseRseult: LiveData<String> = reverseRseult
    override fun onReverseRseult(data: String?) {
        reverseRseult.postValue(data)
    }

    private val resetAmount = MutableLiveData<String>()
    val getResetAmount: LiveData<String> = resetAmount
    override fun onResetAmount(data: String?) {
        resetAmount.postValue(data)
    }

    private val currentBetting = MutableLiveData<Boolean>()
    val getUpdateFlagMain: LiveData<Boolean> get() = currentBetting
    fun setUpdateFlagMain(isUpdate: Boolean) {
        currentBetting.postValue(isUpdate)
    }

//    private val channel = MutableLiveData<ChannelData>()
//    val getChannel: LiveData<ChannelData> get() = channel
//
//    fun setChannel(channelObj: ChannelData) {
//        channel.postValue(channelObj)
//    }

    private val channelInfo = MutableLiveData<ChannelData>()
    val getChannelInfo: LiveData<ChannelData> get() = channelInfo

    fun setChannelInfo(channelObj: ChannelData) {
        channelInfo.postValue(channelObj)
    }

    private val result = MutableLiveData<String>()
    val getResult: LiveData<String> get() = result

    fun setResult(res: String) {
        result.postValue(res)
    }

    private val updateData = MutableLiveData<JSONObject>()
    val getUpdateData: LiveData<JSONObject> get() = updateData

    fun setUpdateData(channelObj: JSONObject) {
        updateData.postValue(channelObj)
    }
}