package com.kaixed.kchat.repository

import androidx.lifecycle.MutableLiveData
import com.kaixed.kchat.model.request.RegisterRequest
import com.kaixed.kchat.model.request.UserRequest
import com.kaixed.kchat.model.response.login.Login
import com.kaixed.kchat.model.response.register.Register
import com.kaixed.kchat.model.response.search.UserList
import com.kaixed.kchat.model.response.user.ChangeNickname
import com.kaixed.kchat.network.NetworkInterface.SERVER_URL
import com.kaixed.kchat.network.NetworkInterface.UPDATE_USER_INFO
import com.kaixed.kchat.network.NetworkInterface.USER_LIST
import com.kaixed.kchat.network.NetworkInterface.USER_LOGIN_BY_TELEPHONE
import com.kaixed.kchat.network.NetworkInterface.USER_LOGIN_BY_USERNAME
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(
        password: String,
        avatarUrl: String,
        signature: String,
        nickname: String,
        telephone: String
    ): MutableLiveData<Register?> {
        val registerMutableLiveData = MutableLiveData<Register?>()

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

        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,
        loginByUsername: Boolean
    ): MutableLiveData<Login?> {
        val loginMutableLiveData = MutableLiveData<Login?>()

        val url = "$SERVER_URL${
            if (loginByUsername) USER_LOGIN_BY_USERNAME else USER_LOGIN_BY_TELEPHONE
        }"


        val requestBody = FormBody.Builder()
            .add(if (loginByUsername) "username" else "telephone", username)
            .add("password", password)
            .build()

        NetworkRequest().postAsync(
            url,
            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 = Json.decodeFromString<Login?>(responseBody)
                        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 = Json.decodeFromString<UserList?>(responseBody)
                        listMutableLiveData.postValue(userList)
                    }
                }
            }
        )
        return listMutableLiveData
    }

    fun changeNickname(username: String, nickname: String): MutableLiveData<Boolean> {
        val listMutableLiveData = MutableLiveData<Boolean>()

        val userRequest = UserRequest(
            username = username,
            nickname = nickname,
        )

        val json =
            Json.encodeToString(UserRequest.serializer(), userRequest)

        NetworkRequest().postAsync(
            "$SERVER_URL$UPDATE_USER_INFO",
            json,
            object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    listMutableLiveData.postValue(false)
                }

                override fun onResponse(call: Call, response: Response) {
                    response.body?.string()?.let { responseBody ->
                        val changeNickname = Json.decodeFromString<ChangeNickname>(responseBody)
                        when (changeNickname.code) {
                            "A0203" -> listMutableLiveData.postValue(true)
                            else -> listMutableLiveData.postValue(false)
                        }
                    }
                }

            }
        )
        return listMutableLiveData
    }
}
