package melody.offline.music.http

import android.content.Context
import okhttp3.Call
import okhttp3.Response
import org.json.JSONObject
import melody.offline.music.sp.AppStore
import melody.offline.music.util.AnalysisUtil
import melody.offline.music.util.LogTag
import java.io.IOException

class UploadEventName {
    private val TAG = LogTag.VO_API_LOG

    companion object {
        val shared: UploadEventName by lazy { UploadEventName() }
    }

    fun init(context: Context) {
        if (MyHttpUtil.mInstance.isNetworkAvailable(context)) {
            initFirstOpen(context)
            initStartProgram(context)
        }
    }

    private fun initFirstOpen(context: Context) {
        if (!AppStore(context).firstOpenIsSucceed) {
            try {
                MyHttpUtil.mInstance.postSaveAppData(
                    BaseApiUtil.shared.postJson(context, "first_open"),
                    object : okhttp3.Callback {
                        override fun onFailure(call: Call, e: IOException) {
                            val jsonObject = JSONObject()
                            jsonObject.put("fail", e.toString())
                            val map = mutableMapOf(Pair(AnalysisUtil.PARAM_VALUE, jsonObject.toString()))
                            AnalysisUtil.logEvent(AnalysisUtil.APP_FIRST_OPEN_FAIL, map)
                        }

                        override fun onResponse(call: Call, response: Response) {
                            try {
                                LogTag.LogD(TAG, "first_open response->${response}")
                                if (response.code == 200) {
                                    AnalysisUtil.logEvent(AnalysisUtil.APP_FIRST_OPEN_SUCCESS)
                                    val responseData = response.body?.string()
                                    if (responseData != null) {
                                        val jsonObject = JSONObject(responseData)
                                        val status = jsonObject.getString("status")
                                        LogTag.LogD(TAG, "first_open jsonObject->${jsonObject}")
                                        LogTag.LogD(TAG, "first_open status->${status}")
                                        if (status.equals("Success")) {
                                            AppStore(context).firstOpenIsSucceed = true
                                        }
                                    }
                                }else{
                                    val jsonObject = JSONObject()
                                    jsonObject.put("fail", response)
                                    val map = mutableMapOf(Pair(AnalysisUtil.PARAM_VALUE, jsonObject.toString()))
                                    AnalysisUtil.logEvent(AnalysisUtil.APP_FIRST_OPEN_FAIL, map)
                                }

                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                    })
            } catch (e: Exception) {
                LogTag.LogD(TAG, e.toString())
            }
        }
    }

    private fun initStartProgram(context: Context) {
        try {
            MyHttpUtil.mInstance.postSaveAppData(
                BaseApiUtil.shared.postJson(context, "app_open"),
                object : okhttp3.Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        val jsonObject = JSONObject()
                        jsonObject.put("fail", e.toString())
                        val map = mutableMapOf(Pair(AnalysisUtil.PARAM_VALUE, jsonObject.toString()))
                        AnalysisUtil.logEvent(AnalysisUtil.APP_OPEN_FAIL, map)
                    }

                    override fun onResponse(call: Call, response: Response) {
                        try {
                            LogTag.LogD(TAG, "start_program response->${response}")
                            if (response.code == 200) {
                                AnalysisUtil.logEvent(AnalysisUtil.APP_OPEN_SUCCESS)
                                val responseData = response.body?.string()
                                if (responseData != null) {
                                    val jsonObject = JSONObject(responseData)
                                    val status = jsonObject.getString("status")
                                    LogTag.LogD(TAG, "start_program jsonObject->${jsonObject}")
                                    LogTag.LogD(TAG, "start_program status->${status}")
                                }
                            }else{
                                val jsonObject = JSONObject()
                                jsonObject.put("fail", response)
                                val map = mutableMapOf(Pair(AnalysisUtil.PARAM_VALUE, jsonObject.toString()))
                                AnalysisUtil.logEvent(AnalysisUtil.APP_OPEN_FAIL, map)
                            }

                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                })
        } catch (e: Exception) {
            LogTag.LogE(TAG, e.toString())
        }
    }

    fun initAppEventData(json: JSONObject) {
        try {
            MyHttpUtil.mInstance.postSaveAppData(
                json,
                object : okhttp3.Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        LogTag.LogD(TAG, "AppEventData onFailure->${e}")
                    }

                    override fun onResponse(call: Call, response: Response) {
                        try {
                            LogTag.LogD(TAG, "AppEventData response->${response}")
                            if (response.code == 200) {
                                val responseData = response.body?.string()
                                if (responseData != null) {
                                    val jsonObject = JSONObject(responseData)
                                    val status = jsonObject.getString("status")
                                    LogTag.LogD(TAG, "AppEventData jsonObject->${jsonObject}")
                                    LogTag.LogD(TAG, "AppEventData status->${status}")
                                }
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                })
        } catch (e: Exception) {
            LogTag.LogE(TAG, e.toString())
        }
    }

}