package com.gh.common.util

import com.gh.gamecenter.eventbus.EBCollectionChanged
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.ResponseBody
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject
import retrofit2.HttpException

/**
 * Created by khy on 26/07/17.
 */
object CollectionUtils {

    enum class CollectionType {
        TOOLKIT, ARTICLE, ANSWER, COMMUNITY_ARTICLE
    }

    fun postCollection(content: String, type: CollectionType, listener: OnCollectionListener) {
        val api = RetrofitManager.getInstance().api

        val postCollection = when (type) {
            CollectionType.ARTICLE -> api.postCollectionArticle(UserManager.getInstance().userId, content)
            CollectionType.TOOLKIT -> api.postCollectionTools(UserManager.getInstance().userId, content)
            CollectionType.ANSWER -> api.postCollectionAnswer(UserManager.getInstance().userId, content)
            else -> return
        }
        postCollection
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    listener.onSuccess()
                    EventBus.getDefault().post(EBCollectionChanged(content, true, type))
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    if (e != null) {
                        try {
                            val string = e.response()?.errorBody()?.string() ?: ""
                            val errorBody = JSONObject(string)
                            if (errorBody.getInt("code") == 403009) {
                                listener.onSuccess()
                                return
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                    listener.onError()
                }
            })
    }

    fun deleteCollection(id: String, type: CollectionType, listener: OnCollectionListener) {
        val api = RetrofitManager.getInstance().api

        val postCollection = when (type) {
            CollectionType.ARTICLE -> api.deletaCollectionArticle(UserManager.getInstance().userId, id)
            CollectionType.TOOLKIT -> api.deleteCollectionTools(UserManager.getInstance().userId, id)
            CollectionType.ANSWER -> api.deleteCollectionAnswer(UserManager.getInstance().userId, id)
            else -> return
        }
        postCollection
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    listener.onSuccess()
                    EventBus.getDefault().post(EBCollectionChanged(id, false, type))
                }

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


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

}