package kr.co.vividnext.sodalive.live

import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.core.Single
import kr.co.vividnext.sodalive.common.ApiResponse
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.live.reservation.MakeLiveReservationRequest
import kr.co.vividnext.sodalive.live.reservation_status.CancelLiveReservationRequest
import kr.co.vividnext.sodalive.live.room.CancelLiveRequest
import kr.co.vividnext.sodalive.live.room.EnterOrQuitLiveRoomRequest
import kr.co.vividnext.sodalive.live.room.LiveRoomStatus
import kr.co.vividnext.sodalive.live.room.SetManagerOrSpeakerOrAudienceRequest
import kr.co.vividnext.sodalive.live.room.StartLiveRequest
import kr.co.vividnext.sodalive.live.room.create.CreateLiveRoomResponse
import kr.co.vividnext.sodalive.live.room.detail.GetRoomDetailResponse
import kr.co.vividnext.sodalive.live.room.donation.DeleteLiveRoomDonationMessage
import kr.co.vividnext.sodalive.live.room.donation.LiveRoomDonationRequest
import kr.co.vividnext.sodalive.live.room.donation.LiveRoomDonationResponse
import kr.co.vividnext.sodalive.live.room.kick_out.LiveRoomKickOutRequest
import kr.co.vividnext.sodalive.live.room.like.LiveRoomLikeHeartRequest
import kr.co.vividnext.sodalive.live.room.menu.MenuApi
import kr.co.vividnext.sodalive.user.CreatorFollowRequestRequest
import kr.co.vividnext.sodalive.user.UserApi
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.util.TimeZone

class LiveRepository(
    private val api: LiveApi,
    private val userApi: UserApi,
    private val menuApi: MenuApi
) {
    fun roomList(
        dateString: String? = null,
        status: LiveRoomStatus,
        page: Int,
        size: Int,
        token: String
    ): Flowable<ApiResponse<List<GetRoomListResponse>>> {
        return api.roomList(
            timezone = TimeZone.getDefault().id,
            dateString = dateString,
            status = status,
            isAdultContentVisible = SharedPreferenceManager.isAdultContentVisible,
            page = page - 1,
            size = size,
            authHeader = token
        )
    }

    fun getRoomDetail(roomId: Long, token: String): Single<ApiResponse<GetRoomDetailResponse>> {
        return api.getRoomDetail(
            roomId,
            timezone = TimeZone.getDefault().id,
            authHeader = token
        )
    }

    fun getRecentRoomInfo(token: String) = api.getRecentRoomInfo(authHeader = token)

    fun createRoom(
        coverImage: MultipartBody.Part? = null,
        request: RequestBody,
        token: String
    ): Single<ApiResponse<CreateLiveRoomResponse>> {
        return api.createRoom(
            coverImage,
            request,
            authHeader = token
        )
    }

    fun startLive(
        request: StartLiveRequest,
        token: String
    ) = api.startLive(
        request,
        authHeader = token
    )

    fun cancelLive(
        request: CancelLiveRequest,
        token: String
    ) = api.cancelLive(request, authHeader = token)

    fun enterRoom(
        request: EnterOrQuitLiveRoomRequest,
        token: String
    ) = api.enterRoom(
        request,
        authHeader = token
    )

    fun makeReservation(
        request: MakeLiveReservationRequest,
        token: String
    ) = api.makeReservation(
        request,
        authHeader = token
    )

    fun editLiveRoomInfo(
        roomId: Long,
        coverImage: MultipartBody.Part? = null,
        request: RequestBody? = null,
        token: String
    ) = api.editLiveRoomInfo(
        id = roomId,
        coverImage = coverImage,
        request = request,
        authHeader = token
    )

    fun getRoomInfo(roomId: Long, token: String) = api.getRoomInfo(roomId, authHeader = token)

    fun getDonationMessageList(
        roomId: Long,
        token: String
    ) = api.getDonationMessageList(roomId = roomId, authHeader = token)

    fun deleteDonationMessage(
        roomId: Long,
        uuid: String,
        token: String
    ) = api.deleteDonationMessage(
        request = DeleteLiveRoomDonationMessage(
            roomId,
            messageUUID = uuid
        ),
        authHeader = token
    )

    fun getUserProfile(roomId: Long, userId: Long, token: String) = api.getUserProfile(
        roomId = roomId,
        userId = userId,
        authHeader = token
    )

    fun getTotalDonationCan(
        roomId: Long,
        token: String
    ) = api.getDonationTotal(roomId, authHeader = token)

    fun setSpeaker(roomId: Long, userId: Long, token: String): Single<ApiResponse<Any>> {
        return api.setSpeaker(
            request = SetManagerOrSpeakerOrAudienceRequest(roomId, memberId = userId),
            authHeader = token
        )
    }

    fun setListener(roomId: Long, userId: Long, token: String): Single<ApiResponse<Any>> {
        return api.setListener(
            request = SetManagerOrSpeakerOrAudienceRequest(roomId, memberId = userId),
            authHeader = token
        )
    }

    fun kickOut(roomId: Long, userId: Long, token: String): Single<ApiResponse<Any>> {
        return api.kickOut(
            request = LiveRoomKickOutRequest(roomId, userId),
            authHeader = token
        )
    }

    fun donation(
        roomId: Long,
        can: Int,
        message: String,
        isSecret: Boolean,
        token: String
    ): Single<ApiResponse<LiveRoomDonationResponse>> {
        return api.donation(
            request = LiveRoomDonationRequest(
                roomId = roomId,
                can = can,
                isSecret = isSecret,
                message = message,
                container = "aos"
            ),
            authHeader = token
        )
    }

    fun refundDonation(roomId: Long, token: String): Single<ApiResponse<Any>> {
        return api.refundDonation(
            id = roomId,
            authHeader = token
        )
    }

    fun quitRoom(roomId: Long, token: String): Single<ApiResponse<Any>> {
        return api.quitRoom(roomId = roomId, authHeader = token)
    }

    fun setManager(roomId: Long, userId: Long, token: String) = api.setManager(
        request = SetManagerOrSpeakerOrAudienceRequest(roomId, memberId = userId),
        authHeader = token,
    )

    fun creatorFollow(
        creatorId: Long,
        token: String
    ) = userApi.creatorFollow(
        request = CreatorFollowRequestRequest(creatorId = creatorId),
        authHeader = token
    )

    fun creatorUnFollow(
        creatorId: Long,
        token: String
    ) = userApi.creatorUnFollow(
        request = CreatorFollowRequestRequest(creatorId = creatorId),
        authHeader = token
    )

    fun donationStatus(
        roomId: Long,
        token: String
    ) = api.donationStatus(roomId, authHeader = token)

    fun recentVisitRoomUsers(token: String) = api.recentVisitRoomUsers(authHeader = token)

    fun getReservations(
        active: Boolean,
        token: String
    ) = api.getReservations(TimeZone.getDefault().id, active, authHeader = token)

    fun getReservation(
        reservationId: Long,
        token: String
    ) = api.getReservation(
        id = reservationId,
        timezone = TimeZone.getDefault().id,
        authHeader = token
    )

    fun cancelReservation(
        request: CancelLiveReservationRequest,
        token: String
    ) = api.cancelReservation(request, authHeader = token)

    fun getAllMenu(creatorId: Long, token: String) = menuApi.getAllMenu(
        creatorId = creatorId,
        authHeader = token
    )

    fun likeHeart(roomId: Long, token: String) = api.likeHeart(
        request = LiveRoomLikeHeartRequest(
            roomId = roomId,
            container = "aos"
        ),
        authHeader = token
    )

    fun getTotalHeartCount(roomId: Long, token: String) = api.getTotalHeartCount(
        roomId,
        authHeader = token
    )

    fun heartStatus(roomId: Long, token: String) = api.heartStatus(
        roomId,
        authHeader = token
    )
}
