package com.remax.visualnovel.repository.api

import com.pengxr.modular.eventbus.generated.events.EventDefineOfChatSettingEvents
import com.pengxr.modular.eventbus.generated.events.EventDefineOfUserAIEvents
import com.pengxr.modular.eventbus.generated.events.EventDefineOfUserEvents
import com.remax.visualnovel.api.service.ChatService
import com.remax.visualnovel.entity.request.AIFeedback
import com.remax.visualnovel.entity.request.AIIDRequest
import com.remax.visualnovel.entity.request.AIIsShowDTO
import com.remax.visualnovel.entity.request.ChatAlbum
import com.remax.visualnovel.entity.request.ChatSetting
import com.remax.visualnovel.entity.request.HeartbeatBuy
import com.remax.visualnovel.entity.request.RTCRequest
import com.remax.visualnovel.entity.request.SearchPage
import com.remax.visualnovel.entity.request.SimpleDataDTO
import com.remax.visualnovel.entity.request.VoiceTTS
import com.remax.visualnovel.entity.response.Character
import com.remax.visualnovel.entity.response.ChatSet
import com.remax.visualnovel.repository.api.base.BaseRepository
import com.remax.visualnovel.ui.chat.message.events.model.ChatSetAutoPlayEvent
import com.remax.visualnovel.ui.chat.message.events.model.ChatSetBackgroundEvent
import com.remax.visualnovel.ui.wallet.manager.WalletManager
import javax.inject.Inject

/**
 * Created by HJW on 2022/11/15
 */
class ChatRepository @Inject constructor(private val chatService: ChatService) : BaseRepository() {

    suspend fun sendDialogueMsg(aiId: String) = executeHttp {
        chatService.sendDialogueMsg(AIIDRequest(aiId))
    }

    suspend fun viewAlbumImg(request: ChatAlbum) = executeHttp {
        chatService.viewAlbumImg(request)
    }

    suspend fun getIMAICharacterProfile(aiId: String) = executeHttp {
        chatService.getIMAICharacterProfile(Character(aiId = aiId))
    }

    suspend fun getMyFriends(request: SearchPage) = executeHttp {
        chatService.getMyFriends(request)
    }

    suspend fun getMyFriendRank() = executeHttp {
        chatService.getMyFriendRank()
    }

    suspend fun getPrompts(aiId: String) = executeHttp {
        chatService.getPrompts(AIIDRequest(aiId))
    }.transformResult({
        WalletManager.refreshWallet()
    })

    suspend fun aiFeedback(request: AIFeedback) = executeHttp {
        chatService.aiFeedback(request)
    }

    suspend fun getRTCToken(roomId: String) = executeHttp {
        chatService.getRTCToken(RTCRequest(roomId))
    }

    suspend fun voiceChatOpt(request: RTCRequest) = executeHttp {
        chatService.voiceChatOpt(request)
    }

    suspend fun getChatBackgroundList(aiId: String) = executeHttp {
        chatService.getChatBackgroundList(AIIDRequest(aiId))
    }

    suspend fun getChatSetting(aiId: String?) = executeHttp {
        chatService.getChatSetting(ChatSetting(aiId))
    }

    suspend fun setChatSetting(request: ChatSet) = executeHttp {
        chatService.setChatSetting(request)
    }.transformResult({
        EventDefineOfUserEvents.onUserInfoChanged().post(null)
    })

    suspend fun setChatBubble(request: ChatSetting) = executeHttp {
        chatService.setChatBubble(request)
    }

    suspend fun setChatModel(request: ChatSetting) = executeHttp {
        chatService.setChatModel(request)
    }

    suspend fun setChatAutoPlay(request: ChatSetting) = executeHttp {
        chatService.setChatAutoPlay(request)
    }.transformResult({
        EventDefineOfChatSettingEvents.settingChanged()
            .post(ChatSetAutoPlayEvent(request.aiId ?: "", if (request.isAutoPlayVoice == true) 1 else 0))
    })


    suspend fun setChatBackground(request: ChatSetting) = executeHttp {
        chatService.setChatBackground(request)
    }.transformResult({
        EventDefineOfChatSettingEvents.settingChanged()
            .post(ChatSetBackgroundEvent(request.aiId ?: "", request.backgroundImg))
    })

    suspend fun deleteChatBackground(request: ChatSetting) = executeHttp {
        chatService.deleteChatBackground(request)
    }

    suspend fun relationSwitch(request: AIIsShowDTO) = executeHttp {
        chatService.relationSwitch(request)
    }.transformResult({
        EventDefineOfUserAIEvents.onAIHeartIsOpenChanged().post(request)
    })

    suspend fun voiceASR(aiId: String, url: String?) = executeHttp {
        chatService.voiceASR(SimpleDataDTO(aiId = aiId, url = url))
    }.transformResult({
        WalletManager.refreshWallet()
    })

    suspend fun voiceTTS(request: VoiceTTS) = executeHttp {
        chatService.voiceTTS(request)
    }

    suspend fun getHeartbeatLevel(aiId: String) = executeHttp {
        chatService.getHeartbeatLevel(Character(aiId = aiId))
    }

    suspend fun buyHeartbeatVal(request: HeartbeatBuy) = executeHttp {
        chatService.buyHeartbeatVal(request)
    }

}