package com.aiosman.riderpro.data.api

import com.aiosman.riderpro.data.AccountFavourite
import com.aiosman.riderpro.data.AccountFollow
import com.aiosman.riderpro.data.AccountLike
import com.aiosman.riderpro.data.AccountNotice
import com.aiosman.riderpro.data.AccountProfile
import com.aiosman.riderpro.data.Comment
import com.aiosman.riderpro.data.DataContainer
import com.aiosman.riderpro.data.ListContainer
import com.aiosman.riderpro.data.Moment
import com.aiosman.riderpro.entity.ChatNotification
import com.google.gson.annotations.SerializedName
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Multipart
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Path
import retrofit2.http.Query

data class RegisterRequestBody(
    @SerializedName("username")
    val username: String,
    @SerializedName("password")
    val password: String
)

data class LoginUserRequestBody(
    @SerializedName("username")
    val username: String? = null,
    @SerializedName("password")
    val password: String? = null,
    @SerializedName("googleId")
    val googleId: String? = null,
    @SerializedName("captcha")
    val captcha: CaptchaInfo? = null,
)

data class GoogleRegisterRequestBody(
    @SerializedName("idToken")
    val idToken: String
)

data class AuthResult(
    @SerializedName("code")
    val code: Int,
    @SerializedName("expire")
    val expire: String,
    @SerializedName("token")
    val token: String
)

data class ValidateTokenResult(
    @SerializedName("id")
    val id: Int,
)

data class CommentRequestBody(
    @SerializedName("content")
    val content: String,
    @SerializedName("parentCommentId")
    val parentCommentId: Int? = null,
    @SerializedName("replyUserId")
    val replyUserId: Int? = null,
    @SerializedName("replyCommentId")
    val replyCommentId: Int? = null,
)

data class ChangePasswordRequestBody(
    @SerializedName("currentPassword")
    val oldPassword: String = "",
    @SerializedName("newPassword")
    val newPassword: String = ""
)

data class UpdateNoticeRequestBody(
    @SerializedName("lastLookLikeTime")
    val lastLookLikeTime: String? = null,
    @SerializedName("lastLookFollowTime")
    val lastLookFollowTime: String? = null,
    @SerializedName("lastLookFavoriteTime")
    val lastLookFavouriteTime: String? = null
)

data class RegisterMessageChannelRequestBody(
    @SerializedName("client")
    val client: String,
    @SerializedName("identifier")
    val identifier: String,
)

data class UnRegisterMessageChannelRequestBody(
    @SerializedName("client")
    val client: String,
    @SerializedName("identifier")
    val identifier: String,
)
data class ResetPasswordRequestBody(
    @SerializedName("username")
    val username: String,
)

data class UpdateUserLangRequestBody(
    @SerializedName("language")
    val lang: String,
    @SerializedName("timeOffset")
    val timeOffset: Int,
    @SerializedName("timezone")
    val timezone: String,
)

data class TrtcSignResponseBody(
    @SerializedName("sig")
    val sig: String,
    @SerializedName("userId")
    val userId: String,
)

data class AppConfig(
    @SerializedName("trtcAppId")
    val trtcAppId: Int,
)

data class DictItem(
    @SerializedName("key")
    val key: String,
    @SerializedName("value")
    val value: String,
    @SerializedName("desc")
    val desc: String,
)

data class CaptchaRequestBody(
    @SerializedName("source")
    val source: String,
)

data class CaptchaResponseBody(
    @SerializedName("id")
    val id: Int,
    @SerializedName("thumb_base64")
    val thumbBase64: String,
    @SerializedName("master_base64")
    val masterBase64: String,
    @SerializedName("count")
    val count: Int,
)

data class CheckLoginCaptchaRequestBody(
    @SerializedName("username")
    val username: String,
)

data class GenerateLoginCaptchaRequestBody(
    @SerializedName("username")
    val username: String,
)

data class DotPosition(
    @SerializedName("index")
    val index: Int,
    @SerializedName("x")
    val x: Int,
    @SerializedName("y")
    val y: Int,
)

data class CaptchaInfo(
    @SerializedName("id")
    val id: Int,
    @SerializedName("dot")
    val dot: List<DotPosition>
)


data class UpdateChatNotificationRequestBody(
    @SerializedName("targetUserId")
    val targetUserId: Int,
    @SerializedName("strategy")
    val strategy: String,
)

interface RiderProAPI {
    @POST("register")
    suspend fun register(@Body body: RegisterRequestBody): Response<Unit>

    @POST("login")
    suspend fun login(@Body body: LoginUserRequestBody): Response<AuthResult>

    @GET("auth/token")
    suspend fun checkToken(): Response<ValidateTokenResult>

    @GET("auth/refresh_token")
    suspend fun refreshToken(
        @Query("token") token: String
    ): Response<AuthResult>

    @GET("posts")
    suspend fun getPosts(
        @Query("page") page: Int = 1,
        @Query("pageSize") pageSize: Int = 20,
        @Query("timelineId") timelineId: Int? = null,
        @Query("authorId") authorId: Int? = null,
        @Query("contentSearch") contentSearch: String? = null,
        @Query("postUser") postUser: Int? = null,
        @Query("trend") trend: String? = null,
        @Query("favouriteUserId") favouriteUserId: Int? = null,
    ): Response<ListContainer<Moment>>

    @Multipart
    @POST("posts")
    suspend fun createPost(
        @Part image: List<MultipartBody.Part>,
        @Part("textContent") textContent: RequestBody,
    ): Response<DataContainer<Moment>>

    @GET("post/{id}")
    suspend fun getPost(
        @Path("id") id: Int
    ): Response<DataContainer<Moment>>

    @POST("post/{id}/like")
    suspend fun likePost(
        @Path("id") id: Int
    ): Response<Unit>

    @POST("post/{id}/dislike")
    suspend fun dislikePost(
        @Path("id") id: Int
    ): Response<Unit>

    @POST("post/{id}/favorite")
    suspend fun favoritePost(
        @Path("id") id: Int
    ): Response<Unit>

    @POST("post/{id}/unfavorite")
    suspend fun unfavoritePost(
        @Path("id") id: Int
    ): Response<Unit>

    @POST("post/{id}/comment")
    suspend fun createComment(
        @Path("id") id: Int,
        @Body body: CommentRequestBody
    ): Response<DataContainer<Comment>>

    @POST("comment/{id}/like")
    suspend fun likeComment(
        @Path("id") id: Int
    ): Response<Unit>

    @POST("comment/{id}/dislike")
    suspend fun dislikeComment(
        @Path("id") id: Int
    ): Response<Unit>

    @POST("comment/{id}/read")
    suspend fun updateReadStatus(
        @Path("id") id: Int
    ): Response<Unit>


    @GET("comments")
    suspend fun getComments(
        @Query("page") page: Int = 1,
        @Query("postId") postId: Int? = null,
        @Query("pageSize") pageSize: Int = 20,
        @Query("postUser") postUser: Int? = null,
        @Query("selfNotice") selfNotice: Int? = 0,
        @Query("order") order: String? = null,
        @Query("parentCommentId") parentCommentId: Int? = null,
    ): Response<ListContainer<Comment>>

    @GET("account/my")
    suspend fun getMyAccount(): Response<DataContainer<AccountProfile>>

    @Multipart
    @PATCH("account/my/profile")
    suspend fun updateProfile(
        @Part avatar: MultipartBody.Part?,
        @Part banner: MultipartBody.Part?,
        @Part("nickname") nickname: RequestBody?,
        @Part("bio") bio: RequestBody?,
    ): Response<Unit>

    @POST("account/my/password")
    suspend fun changePassword(
        @Body body: ChangePasswordRequestBody
    ): Response<Unit>

    @GET("account/my/notice/like")
    suspend fun getMyLikeNotices(
        @Query("page") page: Int = 1,
        @Query("pageSize") pageSize: Int = 20,
    ): Response<ListContainer<AccountLike>>

    @GET("account/my/notice/follow")
    suspend fun getMyFollowNotices(
        @Query("page") page: Int = 1,
        @Query("pageSize") pageSize: Int = 20,
    ): Response<ListContainer<AccountFollow>>

    @GET("account/my/notice/favourite")
    suspend fun getMyFavouriteNotices(
        @Query("page") page: Int = 1,
        @Query("pageSize") pageSize: Int = 20,
    ): Response<ListContainer<AccountFavourite>>

    @GET("account/my/notice")
    suspend fun getMyNoticeInfo(): Response<DataContainer<AccountNotice>>

    @POST("account/my/notice")
    suspend fun updateNoticeInfo(
        @Body body: UpdateNoticeRequestBody
    ): Response<Unit>

    @POST("account/my/messaging")
    suspend fun registerMessageChannel(
        @Body body: RegisterMessageChannelRequestBody
    ): Response<Unit>

    @POST("account/my/messaging/unregister")
    suspend fun unRegisterMessageChannel(
        @Body body: UnRegisterMessageChannelRequestBody
    ): Response<Unit>

    @GET("profile/{id}")
    suspend fun getAccountProfileById(
        @Path("id") id: Int
    ): Response<DataContainer<AccountProfile>>

    @GET("profile/trtc/{id}")
    suspend fun getAccountProfileByTrtcUserId(
        @Path("id") id: String
    ): Response<DataContainer<AccountProfile>>

    @POST("user/{id}/follow")
    suspend fun followUser(
        @Path("id") id: Int
    ): Response<Unit>

    @POST("user/{id}/unfollow")
    suspend fun unfollowUser(
        @Path("id") id: Int
    ): Response<Unit>

    @GET("users")
    suspend fun getUsers(
        @Query("page") page: Int = 1,
        @Query("pageSize") pageSize: Int = 20,
        @Query("nickname") search: String? = null,
        @Query("followerId") followerId: Int? = null,
        @Query("followingId") followingId: Int? = null,
    ): Response<ListContainer<AccountProfile>>

    @POST("register/google")
    suspend fun registerWithGoogle(@Body body: GoogleRegisterRequestBody): Response<AuthResult>

    @DELETE("post/{id}")
    suspend fun deletePost(
        @Path("id") id: Int
    ): Response<Unit>

    @DELETE("comment/{id}")
    suspend fun deleteComment(
        @Path("id") id: Int
    ): Response<Unit>

    @POST("account/my/password/reset")
    suspend fun resetPassword(
        @Body body: ResetPasswordRequestBody
    ): Response<Unit>

    @GET("comment/{id}")
    suspend fun getComment(
        @Path("id") id: Int
    ): Response<DataContainer<Comment>>

    @PATCH("account/my/extra")
    suspend fun updateUserExtra(
        @Body body: UpdateUserLangRequestBody
    ): Response<Unit>

    @GET("account/my/chat/sign")
    suspend fun getChatSign(): Response<DataContainer<TrtcSignResponseBody>>

    @GET("app/info")
    suspend fun getAppConfig(): Response<DataContainer<AppConfig>>

    @GET("dict")
    suspend fun getDict(
        @Query("key") key: String
    ): Response<DataContainer<DictItem>>

    @POST("captcha/generate")
    suspend fun generateCaptcha(
        @Body body: CaptchaRequestBody
    ): Response<DataContainer<CaptchaResponseBody>>

    @POST("login/needCaptcha")
    suspend fun checkLoginCaptcha(
        @Body body: CheckLoginCaptchaRequestBody
    ): Response<DataContainer<Boolean>>

    @POST("captcha/login/generate")
    suspend fun generateLoginCaptcha(
        @Body body: GenerateLoginCaptchaRequestBody
    ): Response<DataContainer<CaptchaResponseBody>>

    @GET("chat/notification")
    suspend fun getChatNotification(
        @Query("targetTrtcId") targetTrtcId: String
    ): Response<DataContainer<ChatNotification>>

    @POST("chat/notification")
    suspend fun updateChatNotification(
        @Body body: UpdateChatNotificationRequestBody
    ): Response<DataContainer<ChatNotification>>
}

