package kr.co.vividnext.sodalive.audio_content

import kr.co.vividnext.sodalive.audio_content.category.CategoryApi
import kr.co.vividnext.sodalive.audio_content.detail.PutAudioContentLikeRequest
import kr.co.vividnext.sodalive.audio_content.donation.AudioContentDonationRequest
import kr.co.vividnext.sodalive.audio_content.order.OrderRequest
import kr.co.vividnext.sodalive.audio_content.order.OrderType
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.settings.ContentType
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 AudioContentRepository(
    private val api: AudioContentApi,
    private val userApi: UserApi,
    private val categoryApi: CategoryApi
) {
    fun getAudioContentListByCurationId(
        curationId: Long,
        page: Int,
        size: Int,
        sort: AudioContentViewModel.Sort = AudioContentViewModel.Sort.NEWEST,
        token: String
    ) = api.getAudioContentListByCurationId(
        id = curationId,
        isAdultContentVisible = SharedPreferenceManager.isAdultContentVisible,
        contentType = ContentType.values()[SharedPreferenceManager.contentPreference],
        page = page - 1,
        size = size,
        sort = sort,
        authHeader = token
    )

    fun getAudioContentList(
        id: Long,
        categoryId: Long,
        page: Int,
        size: Int,
        sort: AudioContentViewModel.Sort,
        token: String
    ) = api.getAudioContentList(
        id = id,
        categoryId = categoryId,
        page = page - 1,
        size = size,
        sort = sort,
        authHeader = token
    )

    fun getAudioContentThemeList(token: String) = api.getAudioContentThemeList(token)

    fun uploadAudioContent(
        coverImage: MultipartBody.Part,
        contentFile: MultipartBody.Part,
        request: RequestBody,
        token: String
    ) = api.uploadAudioContent(
        coverImage = coverImage,
        contentFile = contentFile,
        request = request,
        authHeader = token
    )

    fun modifyAudioContent(
        coverImage: MultipartBody.Part? = null,
        request: RequestBody,
        token: String
    ) = api.modifyAudioContent(
        coverImage = coverImage,
        request = request,
        authHeader = token
    )

    fun deleteAudioContent(
        id: Long,
        token: String
    ) = api.deleteAudioContent(
        id = id,
        authHeader = token
    )

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

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

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

    fun orderContent(
        contentId: Long,
        orderType: OrderType,
        token: String
    ) = api.orderAudioContent(
        request = OrderRequest(
            contentId = contentId,
            orderType = orderType,
            container = "aos"
        ),
        authHeader = token
    )

    fun getAudioContentOrderList(
        orderType: OrderType? = null,
        page: Int,
        size: Int,
        token: String
    ) = api.getAudioContentOrderList(
        orderType = orderType,
        page = page - 1,
        size = size,
        authHeader = token
    )

    fun addAllPlaybackTracking(
        request: AddAllPlaybackTrackingRequest,
        token: String
    ) = api.addAllPlaybackTracking(request, authHeader = token)

    fun likeContent(
        request: PutAudioContentLikeRequest,
        token: String
    ) = api.likeContent(request, authHeader = token)

    fun getNewContentOfTheme(theme: String, token: String) = api.getNewContentOfTheme(
        theme = theme,
        isAdultContentVisible = SharedPreferenceManager.isAdultContentVisible,
        contentType = ContentType.values()[SharedPreferenceManager.contentPreference],
        authHeader = token
    )

    fun getNewContentAllOfTheme(
        theme: String,
        page: Int,
        size: Int,
        token: String
    ) = api.getNewContentAllOfTheme(
        theme = theme,
        isAdultContentVisible = SharedPreferenceManager.isAdultContentVisible,
        contentType = ContentType.values()[SharedPreferenceManager.contentPreference],
        page = page - 1,
        size = size,
        authHeader = token
    )

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

    fun donation(
        contentId: Long,
        can: Int,
        comment: String,
        token: String
    ) = api.donation(
        request = AudioContentDonationRequest(
            contentId = contentId,
            donationCan = can,
            comment = comment
        ),
        authHeader = token
    )

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

    fun getContentRanking(
        page: Int,
        size: Int,
        sortType: String = "매출",
        token: String
    ) = api.getContentRanking(
        page = page - 1,
        size = size,
        sortType = sortType,
        authHeader = token
    )

    fun getCurationList(page: Int, size: Int, token: String) = api.getCurationList(
        isAdultContentVisible = SharedPreferenceManager.isAdultContentVisible,
        contentType = ContentType.values()[SharedPreferenceManager.contentPreference],
        page = page - 1,
        size = size,
        authHeader = token
    )

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

    fun getMainBannerList(token: String) = api.getMainBannerList(authHeader = token)
    fun getMainOrderList(token: String) = api.getMainOrderList(authHeader = token)
    fun pinContent(
        audioContentId: Long,
        token: String
    ) = api.pinContent(audioContentId, authHeader = token)

    fun unpinContent(
        audioContentId: Long,
        token: String
    ) = api.unpinContent(audioContentId, authHeader = token)

    fun getCategoryList(
        creatorId: Long,
        token: String
    ) = categoryApi.getCategoryList(creatorId, authHeader = token)

    fun getAudioContentByTheme(
        themeId: Long,
        page: Int,
        size: Int,
        sort: AudioContentViewModel.Sort = AudioContentViewModel.Sort.NEWEST,
        token: String
    ) = api.getAudioContentByTheme(
        id = themeId,
        isAdultContentVisible = SharedPreferenceManager.isAdultContentVisible,
        contentType = ContentType.values()[SharedPreferenceManager.contentPreference],
        page = page - 1,
        size = size,
        sort = sort,
        authHeader = token
    )
}
