package com.gh.common.util

import android.content.Context
import android.text.TextUtils
import com.gh.gamecenter.eventbus.EBConcernChanged
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.RequestBody
import okhttp3.ResponseBody
import org.greenrobot.eventbus.EventBus
import org.json.JSONArray
import retrofit2.HttpException

/**
 * Created by khy on 2016/8/24.
 * croncern 工具类
 */
object ConcernUtils {

    /**
     * autoConcern:是否自动关注'关联关注'
     */
    fun postConcernGameId(
        context: Context,
        gameId: String,
        listener: onConcernListener?,
        autoConcern: Boolean = false
    ) {
        val mode = if (autoConcern) "auto" else "manual"

        val userId = UserManager.getInstance().userId
        if (TextUtils.isEmpty(userId)) return

        RetrofitManager.getInstance().api
            .postConcern(UserManager.getInstance().userId, gameId, mode)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    listener?.onSuccess()
                    EventBus.getDefault().post(EBConcernChanged(gameId, true))
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    listener?.onError()
                }
            })
    }

    fun deleteConcernData(context: Context, gameId: String, listener: onConcernListener?) {
        RetrofitManager.getInstance().api
            .deleteConcern(UserManager.getInstance().userId, gameId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    listener?.onSuccess()
                    EventBus.getDefault().post(EBConcernChanged(gameId, false))
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    listener?.onError()
                }
            })
    }

    fun updateConcernData(context: Context, data: JSONArray) {
        val body = RequestBody.create(
            MediaType.parse("application/json"),
            data.toString()
        )
        RetrofitManager.getInstance().api
            .putConcern(UserManager.getInstance().userId, body)
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.io())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    EventBus.getDefault().post(EBConcernChanged())
                }
            })
    }

    fun deleteConcernQuestions(context: Context, questionsId: String, listener: onConcernListener?) {
        RetrofitManager.getInstance().api
            .deleteConcernQuestions(UserManager.getInstance().userId, questionsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    listener?.onSuccess()
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    listener?.onError()
                    var errorString: String? = null
                    try {
                        errorString = e?.response()?.errorBody()?.string()
                    } catch (e1: Exception) {
                        e1.printStackTrace()
                    }
                    ErrorHelper.handleError(context, errorString, false)
                }
            })
    }

    fun postConcernQuestions(context: Context, questionsId: String, listener: onConcernListener?) {
        RetrofitManager.getInstance().api
            .postConcernQuestions(UserManager.getInstance().userId, questionsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    listener?.onSuccess()
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    listener?.onError()
                    var errorString: String? = null
                    try {
                        errorString = e?.response()?.errorBody()?.string()
                    } catch (e1: Exception) {
                        e1.printStackTrace()
                    }
                    ErrorHelper.handleError(context, errorString, false)
                }
            })
    }

    interface onConcernListener {
        fun onSuccess()
        fun onError()
    }

}

