package com.gh.common.history

import com.gh.gamecenter.common.utils.clearHtmlFormatCompletely
import com.gh.gamecenter.common.utils.removeInsertedContent
import com.gh.gamecenter.common.utils.removeVideoContent
import com.gh.gamecenter.common.utils.tryCatchInRelease
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.entity.*
import com.gh.gamecenter.qa.entity.AnswerDetailEntity
import com.gh.gamecenter.qa.entity.AnswerEntity
import com.gh.gamecenter.qa.entity.ArticleDetailEntity
import com.gh.gamecenter.qa.entity.ArticleEntity

object HistoryHelper {

    fun insertAnswerEntity(answerDetailEntity: AnswerDetailEntity) {
        val answerEntity = convertAnswerDetailToAnswer(answerDetailEntity)
        // 偶尔有设备会出现磁盘满了写不进数据库的问题 database or disk is full 异常，毕竟只是插入个本地历史，这里直接捕抓
        runOnIoThread { tryCatchInRelease { HistoryDatabase.instance.answerDao().addAnswer(answerEntity) } }
    }

    fun insertArticleEntity(articleDetailEntity: ArticleDetailEntity) {
        val articleEntity = convertArticleDetailToArticle(articleDetailEntity)
        runOnIoThread { tryCatchInRelease { HistoryDatabase.instance.articleDao().addArticle(articleEntity) } }
    }

    fun insertGamesCollectionEntity(gamesCollectionDetailEntity: GamesCollectionDetailEntity) {
        val gamesCollectionEntity = convertGamesCollectionDetailToGamesCollection(gamesCollectionDetailEntity)
        runOnIoThread {
            tryCatchInRelease {
                HistoryDatabase.instance.gamesCollectionDao().addGamesCollection(gamesCollectionEntity)
            }
        }
    }

    @JvmStatic
    fun insertGameEntity(gameEntity: GameEntity) {
        val historyGameEntity = convertGameEntityToHistoryGameEntity(gameEntity)
        runOnIoThread { tryCatchInRelease { HistoryDatabase.instance.gameDao().addGame(historyGameEntity) } }
    }

    @JvmStatic
    fun insertGameEntity(updateEntity: GameUpdateEntity) {
        val historyGameEntity = convertGameUpdateEntityToHistoryGameEntity(updateEntity)
        runOnIoThread { tryCatchInRelease { HistoryDatabase.instance.gameDao().addGame(historyGameEntity) } }
    }

    private fun convertGameUpdateEntityToHistoryGameEntity(updateEntity: GameUpdateEntity): HistoryGameEntity {
        val historyGame = HistoryGameEntity()

        historyGame.orderTag = System.currentTimeMillis()
        historyGame.id = updateEntity.id
        historyGame.brief = updateEntity.brief
        historyGame.des = ""
        historyGame.icon = updateEntity.rawIcon ?: updateEntity.icon
        historyGame.iconSubscript = updateEntity.iconSubscript
        historyGame.name = updateEntity.name
        historyGame.tagStyle = updateEntity.tagStyle
        return historyGame
    }

    private fun convertGameEntityToHistoryGameEntity(gameEntity: GameEntity): HistoryGameEntity {
        val historyGame = HistoryGameEntity()

        historyGame.orderTag = System.currentTimeMillis()
        historyGame.id = gameEntity.id
        historyGame.brief = gameEntity.brief
        historyGame.des = gameEntity.des
        historyGame.icon = gameEntity.rawIcon ?: gameEntity.icon
        historyGame.iconSubscript = gameEntity.iconSubscript
        historyGame.name = gameEntity.name
        historyGame.tagStyle = gameEntity.tagStyle
        historyGame.tag = gameEntity.getTag()
        historyGame.subtitle = gameEntity.subtitle
        historyGame.subtitleStyle = gameEntity.subtitleStyle
        return historyGame
    }

    @JvmStatic
    fun insertNewsEntity(newsEntity: NewsEntity) {
        newsEntity.orderTag = System.currentTimeMillis()
        runOnIoThread { tryCatchInRelease { HistoryDatabase.instance.newsDao().addNews(newsEntity) } }
    }

    @JvmStatic
    fun deleteNewsEntity(newsId: String) {
        runOnIoThread {
            tryCatchInRelease {
                HistoryDatabase.instance.newsDao().deleteNews(NewsEntity().apply { id = newsId })
            }
        }
    }

    @JvmStatic
    fun deleteGameEntity(gameId: String) {
        runOnIoThread {
            tryCatchInRelease {
                HistoryDatabase.instance.gameDao().deleteGame(HistoryGameEntity(id = gameId))
            }
        }
    }

    @JvmStatic
    fun deleteArticleEntity(articleId: String) {
        runOnIoThread {
            tryCatchInRelease {
                HistoryDatabase.instance.articleDao().deleteArticle(ArticleEntity(id = articleId))
            }
        }
    }

    @JvmStatic
    fun deleteAnswerEntity(answerId: String) {
        runOnIoThread {
            tryCatchInRelease {
                HistoryDatabase.instance.answerDao().deleteAnswer(AnswerEntity().apply { primaryKey = answerId })
            }
        }
    }

    @JvmStatic
    fun deleteVideoEntity(videoId: String) {
        runOnIoThread {
            tryCatchInRelease {
                HistoryDatabase.instance.videoHistoryDao().deleteVideo(MyVideoEntity().apply { id = videoId })
            }
        }
    }

    @JvmStatic
    fun deleteAttentionVideoRecord() {
        runOnIoThread { tryCatchInRelease { HistoryDatabase.instance.videoHistoryDao().deleteAttentionVideoRecord() } }
    }

    @JvmStatic
    fun deleteGamesCollectionEntity(gameCollectionId: String) {
        runOnIoThread {
            tryCatchInRelease {
                HistoryDatabase.instance.gamesCollectionDao()
                    .deleteGamesCollection(GamesCollectionEntity(id = gameCollectionId))
            }
        }
    }


    @JvmStatic
    fun emptyDatabase() {
        runOnIoThread { tryCatchInRelease { HistoryDatabase.instance.clearAllTables() } }
    }

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

        articleEntity.id = articleDetailEntity.id
        articleEntity.brief =
            articleDetailEntity.content.removeVideoContent().removeInsertedContent().clearHtmlFormatCompletely()
                .replace(" +".toRegex(), " ")
        articleEntity.count = articleDetailEntity.count
        articleDetailEntity.community.id = articleDetailEntity.communityId
        articleEntity.community = articleDetailEntity.community
        articleEntity.time = articleDetailEntity.time
        articleEntity.title = articleDetailEntity.title
        articleEntity.user = articleDetailEntity.user
        articleEntity.orderTag = System.currentTimeMillis()
        articleEntity.images = articleDetailEntity.images
        articleEntity.imagesInfo = articleDetailEntity.imagesInfo
        articleEntity.videos = articleDetailEntity.videos

        return articleEntity
    }

    private fun convertAnswerDetailToAnswer(answerDetailEntity: AnswerDetailEntity): AnswerEntity {
        val answerEntity = AnswerEntity()

        answerEntity.id = answerDetailEntity.id
        answerEntity.primaryKey = answerDetailEntity.id
        answerEntity.commentCount = answerDetailEntity.commentCount
        answerEntity.questions = answerDetailEntity.question
        answerEntity.vote = answerDetailEntity.vote
        answerEntity.user = answerDetailEntity.user
        answerEntity.orderTag = System.currentTimeMillis()
        answerEntity.brief =
            answerDetailEntity.content.removeVideoContent().removeInsertedContent().clearHtmlFormatCompletely()
                .replace(" +".toRegex(), " ")
        answerEntity.time = answerDetailEntity.time
        answerEntity.images = answerDetailEntity.images
        answerEntity.imagesInfo = answerDetailEntity.imagesInfo
        answerEntity.videos = answerDetailEntity.videos

        return answerEntity
    }

    private fun convertGamesCollectionDetailToGamesCollection(gamesCollectionDetailEntity: GamesCollectionDetailEntity): GamesCollectionEntity {
        val gamesCollectionEntity = GamesCollectionEntity()

        gamesCollectionEntity.id = gamesCollectionDetailEntity.id
        gamesCollectionEntity.tags = gamesCollectionDetailEntity.tags
        gamesCollectionEntity.activityTags = gamesCollectionDetailEntity.activityTags
        gamesCollectionDetailEntity.games?.take(3)?.map { it.toSimpleGame() }?.run {
            gamesCollectionEntity.games = ArrayList(this)
        }
        gamesCollectionEntity.title = gamesCollectionDetailEntity.title
        gamesCollectionEntity.intro = gamesCollectionDetailEntity.intro
        gamesCollectionEntity.cover = gamesCollectionDetailEntity.cover
        gamesCollectionEntity.display = gamesCollectionDetailEntity.display
        gamesCollectionEntity.orderTag = System.currentTimeMillis()
        gamesCollectionEntity.stamp = gamesCollectionDetailEntity.stamp
        gamesCollectionEntity.count = gamesCollectionDetailEntity.count
        gamesCollectionDetailEntity.user?.run {
            gamesCollectionEntity.user = User(
                id = id ?: "",
                name = name ?: "",
                icon = icon ?: "",
                badge = badge
            )
        }
        gamesCollectionEntity.me = gamesCollectionDetailEntity.me

        return gamesCollectionEntity
    }

}