package com.gh.gamecenter.forum.home

import android.app.Application
import androidx.lifecycle.*
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.utils.clearHtmlFormatCompletely
import com.gh.gamecenter.common.utils.observableToMain
import com.gh.gamecenter.common.utils.removeInsertedContent
import com.gh.gamecenter.common.utils.removeVideoContent
import com.gh.gamecenter.qa.entity.ArticleDetailEntity
import com.gh.gamecenter.feature.entity.ArticleEntity
import com.gh.gamecenter.qa.entity.QuestionsDetailEntity
import com.gh.gamecenter.feature.entity.TimeEntity
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.feature.entity.ForumVideoEntity
import com.gh.gamecenter.feature.entity.UserEntity
import com.gh.gamecenter.retrofit.RetrofitManager

class CommunityHomeViewModel(application: Application) : AndroidViewModel(application) {

    private val mApi = RetrofitManager.getInstance().api
    val articleLiveData = MediatorLiveData<ArticleEntity>()

    fun getArticleData(articleId: String) {
        mApi.getCommunityArticleDetail(articleId)
            .compose(observableToMain())
            .subscribe(object : Response<ArticleDetailEntity>() {
                override fun onResponse(response: ArticleDetailEntity?) {
                    response?.run {
                        articleLiveData.postValue(convertArticleDetailToArticle(response))
                    }

                }
            })
    }

    fun getQuestionDetail(questionId: String) {
        mApi.getQuestionsById(questionId)
            .compose(observableToMain())
            .subscribe(object : Response<QuestionsDetailEntity>() {
                override fun onResponse(response: QuestionsDetailEntity?) {
                    response?.run {
                        articleLiveData.postValue(convertQuestionDetailToArticle(response))
                    }
                }
            })
    }

    fun getVideoDetail(videoId: String) {
        mApi.getBbsVideoDetail(videoId)
            .compose(observableToMain())
            .subscribe(object : Response<ForumVideoEntity>() {
                override fun onResponse(response: ForumVideoEntity?) {
                    response?.run {
                        articleLiveData.postValue(convertVideoDetailToArticle(response))
                    }
                }
            })
    }

    fun convertArticleDetailToArticle(articleDetailEntity: ArticleDetailEntity): ArticleEntity {
        val articleEntity = ArticleEntity()

        articleEntity.id = articleDetailEntity.id
        articleEntity.brief =
            articleDetailEntity.content.removeVideoContent().removeInsertedContent().clearHtmlFormatCompletely()
        articleEntity.count = articleDetailEntity.count
        articleDetailEntity.community.id = articleDetailEntity.communityId
        articleEntity.community = articleDetailEntity.community
        articleEntity.bbs = articleDetailEntity.community
        articleEntity.time = articleDetailEntity.time
        articleEntity.title = articleDetailEntity.title
        articleEntity.user = articleDetailEntity.user
        articleEntity.images = articleDetailEntity.images
        articleEntity.imagesInfo = articleDetailEntity.imagesInfo
        articleEntity.videos = articleDetailEntity.videos
        articleEntity.status = articleDetailEntity.status ?: ""
        articleEntity.type = "community_article"

        return articleEntity
    }

    fun convertQuestionDetailToArticle(questionDetailEntity: QuestionsDetailEntity): ArticleEntity {
        val articleEntity = ArticleEntity()

        articleEntity.id = questionDetailEntity.id ?: ""
        articleEntity.brief =
            questionDetailEntity.description?.removeVideoContent()?.removeInsertedContent()?.clearHtmlFormatCompletely()
                ?: ""
        articleEntity.count = questionDetailEntity.count
        articleEntity.community = questionDetailEntity.community
        articleEntity.bbs = questionDetailEntity.community
        articleEntity.time = questionDetailEntity.time
        articleEntity.title = questionDetailEntity.title ?: ""
        articleEntity.user = questionDetailEntity.user
        articleEntity.images = questionDetailEntity.images
        articleEntity.imagesInfo = questionDetailEntity.imagesInfo
        articleEntity.videos = questionDetailEntity.videos
        articleEntity.status = questionDetailEntity.status
        articleEntity.type = "question"

        return articleEntity
    }

    fun convertVideoDetailToArticle(forumVideoEntity: ForumVideoEntity): ArticleEntity {
        val articleEntity = ArticleEntity()

        articleEntity.id = forumVideoEntity.id
        articleEntity.title = forumVideoEntity.title
        articleEntity.des = forumVideoEntity.des
        articleEntity.url = forumVideoEntity.url
        articleEntity.status = forumVideoEntity.status
        articleEntity.poster = forumVideoEntity.poster
        articleEntity.length = forumVideoEntity.length
        articleEntity.videoInfo = forumVideoEntity.videoInfo
        articleEntity.count = forumVideoEntity.count
        articleEntity.bbs = forumVideoEntity.bbs ?: CommunityEntity()
        articleEntity.time = TimeEntity(upload = forumVideoEntity.time.upload)
        articleEntity.title = forumVideoEntity.title
        forumVideoEntity.user.run {
            articleEntity.user = UserEntity(
                id = id,
                name = name,
                icon = icon,
                auth = auth,
                badge = badge,
                border = border
            )
        }
        articleEntity.type = "video"

        return articleEntity
    }
}