package com.yidian.shenghuoquan.newscontent.service

import android.graphics.Bitmap
import com.google.gson.Gson
import com.yidian.common.YdBaseApplication
import com.yidian.common.http.HttpResult
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.httpbean.IUploadFileCallback
import com.yidian.shenghuoquan.newscontent.http.httpbean.IUserInfoCallback
import com.yidian.shenghuoquan.newscontent.utils.UploadFileUtils
import com.yidian.xarc.xrouter.ResultResolver
import com.yidian.yac.core.zap.ZapService
import id.zelory.compressor.Compressor
import id.zelory.compressor.constraint.format
import id.zelory.compressor.constraint.quality
import id.zelory.compressor.constraint.resolution
import id.zelory.compressor.constraint.size
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.io.File

class RequestService : ZapService() {
    companion object {
        const val serviceName: String = "RequestService"
    }

    private lateinit var resolver: ResultResolver

    override fun onAction(name: String, action: String, params: Any, options: Any, resolver: ResultResolver) {
        this.resolver = resolver
        params as JSONObject
        val path = params.optString("path")
        var body = params.optJSONObject("body")
        if (body == null) {
            body = JSONObject()
        }
        when (action) {
            "request" -> requestApi(path, body)
            "download" -> download(path, body)
            "uploadImage" -> uploadFile(body)
        }
    }

    override fun onExit() {

    }

    override fun onInit() {

    }

    private fun download(path: String, params: JSONObject) {

    }

    private val userInfoImpl = object : IUserInfoCallback {
        override fun userInfoCallback(t: HttpResult<Any?>?) {
            val params = Gson().toJson(t)
            resolver.success(JSONObject(params))
        }

        override fun modifyNicknameCallback(t: HttpResult<Any?>?) {
            val params = Gson().toJson(t)
            resolver.success(JSONObject(params))
        }
    }

    private fun requestApi(path: String, params: JSONObject) {
//        when (path) {
//            URLs.videoCommentList -> {
//                val docId = params.optString("docid")
//                val pageSize = params.optInt("page_size")
//                val page = params.optInt("page")
//                val videoCommentRequest = VideoCommentBean.Request(docId, pageSize, page)
//                ApiService.videoCommentList(videoContentImpl, videoCommentRequest)
//            }
//            URLs.userInfo -> {
//                ApiService.userInfo(userInfoImpl)
//            }
//            URLs.modifyNickname -> {
//                val nickname = params.optString("nickname")
//                val requestMap = HashMap<String, String>()
//                requestMap["nickname"] = nickname
//                ApiService.modifyNickname(userInfoImpl, requestMap)
//            }
//            URLs.likeComment -> {
//                val docId = params.optString("docid")
//                val commentId = params.optString("comment_id")
//                val status = params.optInt("status")
//                val likeRequest = LikeCommentBean.Request(docId, commentId, status)
//                ApiService.likeComment(likeCommentImpl, likeRequest)
//            }
//            URLs.addSubscribe -> {
//                val type = params.optInt("type")
//                val subscribeId = params.optString("subscribe_id")
//                val subscribeBean = AddSubscribeBean.Request(type, subscribeId)
//                ApiService.addSubscribe(subscribeImpl, subscribeBean)
//            }
//            URLs.deleteSubscribe -> {
//                val type = params.optInt("type")
//                val subscribeIds = params.optString("subscribe_ids")
//                val subscribeBean = DeleteSubscribeBean.Request(type, subscribeIds)
//                ApiService.deleteSubscribe(subscribeImpl, subscribeBean)
//            }
//        }
    }

    private val uploadFileCallback = object : IUploadFileCallback {
        override fun uploadFileCallBack(t: HttpResult<Any?>?) {
            val params = Gson().toJson(t)
            resolver.success(JSONObject(params))
        }
    }

    private fun uploadFile(params: JSONObject) {
        if (YdBaseApplication.activityList.size <= 0) return
        val activity = YdBaseApplication.activityList[YdBaseApplication.activityList.size - 1]
        val filePath = params.optString("profile")
        val file = File(filePath)
        GlobalScope.launch(Dispatchers.Main) {
            GlobalScope.launch(Dispatchers.Main) {
                val compressedImageFile = Compressor.compress(activity, file) {
                    resolution(360, 360)
                    quality(80)
                    format(Bitmap.CompressFormat.PNG)
                    size(307200)
                }
                val requestBody = UploadFileUtils.fileToMultipartBodyPart("profile", compressedImageFile)
                ApiService.uploadFile(uploadFileCallback, requestBody)
            }
        }
    }
}