package com.swago.baseswago.im

import com.alibaba.fastjson.util.ParameterizedTypeImpl
import com.google.gson.Gson
import com.swago.baseswago.im.ImConstant.anchor_close_room
import com.swago.baseswago.im.ImConstant.cancel_forbid_speak
import com.swago.baseswago.im.ImConstant.forbid_speak
import com.swago.baseswago.im.ImConstant.lucky_gift
import com.swago.baseswago.im.ImConstant.remove_room
import com.swago.baseswago.im.ImConstant.room_chat_text
import com.swago.baseswago.im.ImConstant.svg_gift
import com.swago.baseswago.im.ImConstant.user_exit_room
import com.swago.baseswago.im.ImConstant.user_join_room
import com.swago.baseswago.model.im.AnchorRoomClosedBean
import com.swago.baseswago.model.im.CusNewMsgBean
import com.swago.baseswago.model.im.RoomChatMsgBean
import com.swago.baseswago.model.im.UserJoinRoomBean
import com.swago.baseswago.model.live.ReceiveModel
import com.swago.baseswago.model.live.gift.IMGiftModel
import com.swago.baseswago.util.LogUtil
import com.tencent.imsdk.v2.V2TIMMessage
import org.json.JSONObject
import java.lang.Exception
import java.lang.reflect.Type

/**
 *@date 2021/10/23 17:15
 *description:
 */
class GroupMsgParser : IGroupNewMsgParser {

    val gson by lazy {
        Gson()
    }

    override suspend fun parserGroupMsg(v2TIMMessage: V2TIMMessage): IGroupNewMsgBean? {

        val customElem = v2TIMMessage.customElem ?: return null

        val result = customElem.data
        if (result.isEmpty()){
            return null
        }

        var parserModel : CusNewMsgBean<*>? = null

        try {
            val resultString = String(result)
            val jsonObject = JSONObject(resultString)
            LogUtil.d("SwagoIM",resultString)
            var parameterizedTypeImpl: ParameterizedTypeImpl? = null
            when(jsonObject.getInt("type")){
                room_chat_text -> {
                    parameterizedTypeImpl = ParameterizedTypeImpl(
                        arrayOf<Type>(RoomChatMsgBean::class.java),
                        CusNewMsgBean::class.java,
                        CusNewMsgBean::class.java
                    )
                    parserModel = gson.fromJson(resultString,parameterizedTypeImpl)
                }

                user_join_room, user_exit_room -> {
                    parameterizedTypeImpl = ParameterizedTypeImpl(
                        arrayOf<Type>(UserJoinRoomBean::class.java),
                        CusNewMsgBean::class.java,
                        CusNewMsgBean::class.java
                    )
                    parserModel = gson.fromJson(resultString,parameterizedTypeImpl)
                }

                anchor_close_room -> {
                    parameterizedTypeImpl = ParameterizedTypeImpl(
                        arrayOf<Type>(AnchorRoomClosedBean::class.java),
                        CusNewMsgBean::class.java,
                        CusNewMsgBean::class.java
                    )
                    parserModel = gson.fromJson(resultString,parameterizedTypeImpl)
                }

                svg_gift, lucky_gift -> {
                    parameterizedTypeImpl = ParameterizedTypeImpl(
                        arrayOf<Type>(IMGiftModel::class.java),
                        CusNewMsgBean::class.java,
                        CusNewMsgBean::class.java
                    )
                    parserModel = gson.fromJson(resultString,parameterizedTypeImpl)
                }

                remove_room,forbid_speak,cancel_forbid_speak -> {
                    parameterizedTypeImpl = ParameterizedTypeImpl(
                        arrayOf<Type>(ReceiveModel::class.java),
                        CusNewMsgBean::class.java,
                        CusNewMsgBean::class.java
                    )
                    parserModel = gson.fromJson(resultString,parameterizedTypeImpl)
                }
            }
            parserModel?.v2TIMMessage = v2TIMMessage
        }catch (e:Exception){
            e.printStackTrace()
        }

        return parserModel
    }
}