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.beauty_game
import com.swago.baseswago.im.ImConstant.cancel_forbid_speak
import com.swago.baseswago.im.ImConstant.follow_anchor_success
import com.swago.baseswago.im.ImConstant.forbid_speak
import com.swago.baseswago.im.ImConstant.force_close_live_room
import com.swago.baseswago.im.ImConstant.fruit_game
import com.swago.baseswago.im.ImConstant.game_win_big_prize
import com.swago.baseswago.im.ImConstant.lucky_gift
import com.swago.baseswago.im.ImConstant.pk_process
import com.swago.baseswago.im.ImConstant.pk_punish
import com.swago.baseswago.im.ImConstant.pk_send
import com.swago.baseswago.im.ImConstant.pk_start
import com.swago.baseswago.im.ImConstant.pk_stop
import com.swago.baseswago.im.ImConstant.red_envelope
import com.swago.baseswago.im.ImConstant.red_envelope_broadcast
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.update_audience
import com.swago.baseswago.im.ImConstant.user_exit_room
import com.swago.baseswago.im.ImConstant.user_join_room
import com.swago.baseswago.model.RedEnvelope
import com.swago.baseswago.model.im.*
import com.swago.baseswago.model.live.ForceCloseModel
import com.swago.baseswago.model.live.ReceiveModel
import com.swago.baseswago.model.im.GamePrize
import com.swago.baseswago.model.live.RoomUserChangeModel
import com.swago.baseswago.model.live.gift.IMGiftModel
import com.swago.baseswago.model.live.pk.*
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)
                }

                update_audience -> {
                    parameterizedTypeImpl = ParameterizedTypeImpl(
                        arrayOf<Type>(RoomUserChangeModel::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)
                }

                follow_anchor_success -> {
                    parameterizedTypeImpl = ParameterizedTypeImpl(
                        arrayOf<Type>(FollowSuccessMsgBean::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)
                }

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

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

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

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

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

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

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

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

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

        return parserModel
    }
}