package com.kaixed.kchat.repository

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.kaixed.kchat.model.login.Login
import com.kaixed.kchat.model.register.Register
import com.kaixed.kchat.model.register.RegisterRequest
import com.kaixed.kchat.model.user.UserList
import com.kaixed.kchat.network.NetworkInterface.SERVER_URL
import com.kaixed.kchat.network.NetworkInterface.USER_LIST
import com.kaixed.kchat.network.NetworkInterface.USER_LOGIN
import com.kaixed.kchat.network.NetworkInterface.USER_REGISTER
import com.kaixed.kchat.network.NetworkRequest
import kotlinx.serialization.json.Json
import okhttp3.Call
import okhttp3.Callback
import okhttp3.FormBody
import okhttp3.Response
import java.io.IOException

/**
 * @Author: kaixed
 * @Date: 2024/10/23 20:21
 */
class UserRepo {

    fun register(
        username: String,
        password: String,
        avatarUrl: String,
        signature: String,
        nickname: String
    ): MutableLiveData<Register?> {
        val registerMutableLiveData = MutableLiveData<Register?>()

        val registerRequest = RegisterRequest(
            username = username,
            password = password,
            avatarUrl = avatarUrl,
            signature = signature,
            nickname = nickname
        )

        val json = Json.encodeToString(RegisterRequest.serializer(), registerRequest)

        NetworkRequest().postAsync(
            "$SERVER_URL$USER_REGISTER",
            json,
            object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    registerMutableLiveData.postValue(null)
                }

                override fun onResponse(call: Call, response: Response) {
                    response.body?.string()?.let { responseBody ->
                        val register = Json.decodeFromString<Register?>(responseBody)
                        registerMutableLiveData.postValue(register)
                    }
                }
            }
        )
        return registerMutableLiveData
    }

    fun login(username: String, password: String): MutableLiveData<Login?> {
        val loginMutableLiveData = MutableLiveData<Login?>()

        val requestBody = FormBody.Builder()
            .add("username", username)
            .add("password", password)
            .build()

        NetworkRequest().postAsync(
            "$SERVER_URL$USER_LOGIN",
            requestBody,
            object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    loginMutableLiveData.postValue(null)
                }

                override fun onResponse(call: Call, response: Response) {
                    response.body?.string()?.let { responseBody ->
                        val login = Gson().fromJson(responseBody, Login::class.java)
                        loginMutableLiveData.postValue(login)
                    }
                }
            }
        )
        return loginMutableLiveData
    }

    fun getUserListByNickname(username: String): MutableLiveData<UserList?> {
        val listMutableLiveData = MutableLiveData<UserList?>()
        NetworkRequest().getAsync(
            "$SERVER_URL$USER_LIST$username",
            object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    listMutableLiveData.postValue(null)
                }

                override fun onResponse(call: Call, response: Response) {
                    response.body?.string()?.let { responseBody ->
                        val userList = Gson().fromJson(responseBody, UserList::class.java)
                        listMutableLiveData.postValue(userList)
                    }
                }
            }
        )
        return listMutableLiveData
    }
}