package com.cretin.webapi.controller
import com.cretin.entity.common.Resp
import com.cretin.entity.token.ObtainTokenModel
import com.cretin.webcore.config.AppConstants
import com.cretin.webcore.oss.QiniuAuthHelper
import com.cretin.webcore.utils.UUIDUtils
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("/helper")
@ResponseBody
class HelperController {

    /**
     * type 0 获取头像token
     * type 1 获取动态token
     */
    @GetMapping("/qiniu/token")
    fun getToken(@RequestParam(value = "filename", required = true) filename: String): Resp<ObtainTokenModel> {

        fun getPre(): String {
            return if (AppConstants.isDebug()) "tcl/dev" else "tcl/prod"
        }
        fun getTypePre(): String {
            return getPre() + "/logcat/"
        }
        val key = getTypePre() + filename
        return Resp(ObtainTokenModel(QiniuAuthHelper.getToken(key) ?: "",key))
    }

//    /**
//     * 获取qq群信息
//     */
//    @ApiOperation("获取qq群信息", notes = "获取添加qq群的信息")
//    @PostMapping("/qq_service")
//    fun getQQService(): Resp<QQInfoModel> {
//        val queryWrapper = QueryWrapper<TbAppConfig>()
//        queryWrapper.eq("type", 2)
//        val config = appConfigService?.getOne(queryWrapper, false)
//        if (config == null) {
//            return Resp(QQInfoModel(AppConstants.QQ_KEY, AppConstants.QQ_NUM))
//        } else {
//            val configItem = config.config.split(" ")
//            return Resp(QQInfoModel(configItem[1], configItem[0]))
//        }
//    }

//    /**
//     * 获取app配置
//     */
//    @ApiOperation("获取app的配置信息")
//    @PostMapping("/app_config")
//    fun getAppConfig(): Resp<AppConfigModel> {
//        val configs = appConfigService?.list()
//        val result = AppConfigModel("段子乐")
//        //获取广告配置
//        val adInfo = AppConfigModel.AdModel()
//        configs?.forEach {
//            try {
//                if (it.type == 3) {
//                    adInfo.appId = it.config.toInt()
//                }
//                if (it.type == 4) {
//                    adInfo.isUse = it.config == "true"
//                }
//                if (it.type == 5) {
//                    adInfo.fullVideoCode = it.config
//                }
//                if (it.type == 6) {
//                    adInfo.douyinCode = it.config
//                }
//                if (it.type == 7) {
//                    adInfo.feedCode = it.config
//                }
//                if (it.type == 8) {
//                    adInfo.readyCount = it.config.toInt()
//                }
//            } catch (e: Throwable) {
//
//            }
//        }
//        result.adInfo = adInfo
//        return Resp(result)
//    }

//    /**
//     * 获取热门搜索
//     */
//    @ApiOperation("获取热搜关键词")
//    @PostMapping("/hot_search")
//    fun getHotSearch(): Resp<MutableList<String>> {
//        val queryWrapper = QueryWrapper<TbAppConfig>()
//        queryWrapper.eq("type", 1)
//        val config = appConfigService?.getOne(queryWrapper, false)
//        if (config == null) {
//            return Resp(mutableListOf<String>("段子乐"))
//        } else {
//            return Resp(config.config.split(" ")?.toMutableList())
//        }
//    }
//
//    /**
//     * 获取jokes分享的内容
//     */
//    @ApiOperation("获取jokes分享的内容")
//    @PostMapping("/joke/share")
//    @ApiImplicitParams(ApiImplicitParam(name = "id", value = "被分享的段子id", required = true))
//    fun getJokeShare(@RequestParam(value = "id", required = true) id: Int): Resp<ShareInfoModel> {
//        val jokeItem = jokesItemService?.getById(id)
//        if (jokeItem == null) {
//            throw WholeException("被分享的内容不存在或被删除")
//        }
//        val realId = StringUtils.encodeId(id)
//        val result = ShareInfoModel()
//        result.share_url = AppConstants.getWebHostPre() + "/jokes/details?id=" + realId
//        result.share_content = jokeItem.content
//        result.share_title = "我给你分享了1篇精彩的帖子，快来一起看呀~"
//        if (jokeItem.type == 1) {
//            result.share_img = OssUrlAuthHelper.getAvatarUrl(getUserDefaultAvatar())
//        } else if (jokeItem.type == 2) {
//            val images = jokeItem.imageUrl.split(",")
//            if (images.size > 0) {
//                result.share_img = OssUrlAuthHelper.urlGet(images[0]) ?: ""
//            } else {
//                result.share_img = OssUrlAuthHelper.getAvatarUrl(getUserDefaultAvatar())
//            }
//        } else {
//            result.share_img = OssUrlAuthHelper.urlGet(jokeItem.thumbUrl) ?: ""
//        }
//        return Resp(result)
//    }
//
//    /**
//     * 获取分享用户的内容
//     */
//    @ApiOperation("获取分享用户的内容")
//    @PostMapping("/user/share")
//    fun getUserShare(httpServletRequest: HttpServletRequest): Resp<ShareInfoModel> {
//        val userId = tokenService?.getIdByToken(httpServletRequest, true) ?: 0
//        val realId = StringUtils.encodeId(userId)
//        return Resp(ShareInfoModel(AppConstants.getWebHostPre() + "/user/share?id=" + realId,
//                "啤酒小龙虾，段友是一家。", "我在段子乐收获了很多快乐，你也来加入吧~", OssUrlAuthHelper.getAvatarUrl(getUserDefaultAvatar())))
//    }
//
//    /**
//     * 段子分享成功计数
//     */
//    @ApiOperation("段子分享成功计数", notes = "分享段子成功之后需要调用此接口进行计数")
//    @ApiImplicitParams(ApiImplicitParam(name = "id", value = "被分享的段子id", required = true))
//    @PostMapping("/joke/share/count")
//    fun jokeShareCount(@RequestParam(value = "id", required = true) id: Int): Resp<Any> {
//        val queryWrapper = QueryWrapper<TbJokesInfo>()
//        queryWrapper.eq("joke_id", id)
//        val jokeInfo = jokesInfoService?.getOne(queryWrapper, false)
//        if (jokeInfo == null) {
//            //还没有相关数据
//            val jokeItem = jokesItemService?.getById(id)
//            if (jokeItem != null) {
//                //段子是存在的
//                val jokeInfoTemp = TbJokesInfo()
//                jokeInfoTemp.userId = jokeItem.userId
//                jokeInfoTemp.shareNum = 1
//                jokeInfoTemp.updateTime = LocalDateTime.now()
//                jokeInfoTemp.likeNum = 0
//                jokeInfoTemp.dislikeNum = 0
//                jokeInfoTemp.discussNum = 0
//                jokeInfoTemp.jokeId = id
//                jokesInfoService?.save(jokeInfoTemp)
//            }
//        } else {
//            jokeInfo.shareNum = jokeInfo.shareNum + 1
//            jokesInfoService?.updateById(jokeInfo)
//        }
//        return Resp.createSuccess()
//    }
//
//    /**
//     * 举报 0 段子 1 用户
//     */
//    @ApiOperation("举报内容", notes = "举报用户或者举报段子")
//    @PostMapping("/report")
//    @ApiImplicitParams(ApiImplicitParam(name = "type", value = "0 举报段子 1 举报用户", required = true),
//            ApiImplicitParam(name = "content", value = "举报的内容", required = true),
//            ApiImplicitParam(name = "target_id", value = "0 段子id 1 用户id", required = true),
//            ApiImplicitParam(name = "report_tips", value = "举报描述信息，预置的选择项", required = true))
//    @Transactional
//    fun report(@RequestParam(value = "type", required = true) type: Int,
//               @RequestParam(value = "content", defaultValue = "") content: String,
//               @RequestParam(value = "target_id", required = true) target_id: Int,
//               @RequestParam(value = "report_tips", required = true) report_tips: String,
//               httpServletRequest: HttpServletRequest): Resp<Any> {
//        val userId = tokenService?.getIdByToken(httpServletRequest)
//        if (type != 0 && type != 1) {
//            throw WholeException("类型异常")
//        }
//        val report = TbReport()
//        report.reportType = type
//        report.addTime = LocalDateTime.now()
//        report.reportContent = content
//        report.reportUserId = userId
//        report.reportTips = report_tips
//        report.targetId = target_id
//        val success = reportService?.save(report) ?: false
//        if (success) {
//            var jokeContent = ""
//            var userId = 0
//            if (type == 0) {
//                val jokes = jokesItemService?.getById(target_id)
//                jokeContent = jokes?.content ?: ""
//                userId = jokes?.userId ?: 0
//                if (jokeContent.length > 12) {
//                    jokeContent = jokeContent.substring(0, 12) + "..."
//                }
//            } else {
//                userId = target_id
//            }
//            //发送一条系统消息
//            val systemMsg = TbSystemMsg()
//            systemMsg.content =
//                    if (type == 0)
//                        "您的帖子【" + jokeContent + "】被用户举报，举报内容为：" + report_tips + "，系统已收录此举报，系统提醒您请注意保证发布的内容合理合法合规，共同维护良好社区环境"
//                    else
//                        "您的当前账户被其他用户举报，举报内容为：" + report_tips + "，系统已收录此举报，系统提醒您请注意保持个人账户合理合法合规，共同维护良好社区环境"
//            systemMsg.title = "举报提醒"
//            systemMsg.type = if (type == 0) 3 else 2
//            systemMsg.createTime = LocalDateTime.now()
//            systemMsg.isRead = 0
//            systemMsg.userId = userId
//            systemMsg.targetId = target_id
//            systemMsgService?.save(systemMsg)
//
//            //发送钉钉提醒
//            val markdown = "####  用户举报" + "\n" +
//                    "> 提醒类型：段子乐" + "\n\n" +
//                    "> 系统环境：" + (if (AppConstants.isDebug()) "测试环境" else "正式环境") + "\n\n" +
//                    "> 当前IP：" + IpUtils.getIpAddr(httpServletRequest) + "\n\n" +
//                    "> 设备信息：" + httpServletRequest.getAppInfo() + "\n\n" +
//                    "> 发送时间：" + DateTime.now().toString(AppConstants.COMM_TIME_FORMATER) + "\n\n" +
//                    "> 举报id：" + target_id + "\n\n" +
//                    "> 举报类型：" + (if (type == 0) "举报帖子" else "举报用户") + "\n\n"
//            dingdingService?.sendMarkdownMsg("段子乐用户举报", markdown)
//        } else {
//            throw WholeException("举报失败")
//        }
//        return Resp.createSuccess()
//    }

//    /**
//     * 获取更新信息
//     */
//    @ApiOperation("获取更新信息")
//    @PostMapping("/version/update")
//    fun versionUpdate(httpServletRequest: HttpServletRequest): Resp<Any> {
//        val channel = httpServletRequest.getChannel()
//        val appCode = httpServletRequest.getVersionCode()
//        if (appCode == -1) {
//            throw WholeException("非法请求")
//        }
//        val result = mutableMapOf<String, Any>()
//        val query = QueryWrapper<TbVersionUpdate>()
//        query.eq("channel", channel)
//        val versionItem = versionUpdateService?.getOne(query)
//        if (versionItem == null) {
//            return Resp.createSuccess("没有最新版本")
//        }
//        return Resp(versionItem)
//    }

//    /**
//     * 意见反馈
//     */
//    @ApiOperation("意见反馈")
//    @PostMapping("/feedback")
//    @ApiImplicitParams(
//            ApiImplicitParam(name = "pics", value = "图片链接，多图用,分割，图片请先上传至七牛云", required = true),
//            ApiImplicitParam(name = "content", value = "反馈的内容", required = true),
//            ApiImplicitParam(name = "version_name", value = "当前版本号", required = true),
//            ApiImplicitParam(name = "version_code", value = "当前版本code", required = true),
//            ApiImplicitParam(name = "brand", value = "设备品牌", required = true),
//            ApiImplicitParam(name = "product", value = "设备型号", required = true),
//            ApiImplicitParam(name = "release_version", value = "系统版本号", required = true),
//            ApiImplicitParam(name = "contact", value = "联系方式", required = true),
//    )
//    fun feedback(@RequestParam(value = "pics", defaultValue = "") pics: String,
//                 @RequestParam(value = "content", defaultValue = "") content: String,
//                 @RequestParam(value = "version_name", defaultValue = "") version_name: String,
//                 @RequestParam(value = "version_code", defaultValue = "") version_code: String,
//                 @RequestParam(value = "brand", defaultValue = "") brand: String,
//                 @RequestParam(value = "product", defaultValue = "") product: String,
//                 @RequestParam(value = "release_version", defaultValue = "") release_version: String,
//                 @RequestParam(value = "contact", defaultValue = "") contact: String,
//                 httpServletRequest: HttpServletRequest): Resp<Any> {
//        val userId = tokenService?.getIdByToken(httpServletRequest, true)
//        val feedback = TbFeedback()
//        feedback.brand = brand
//        feedback.content = content
//        feedback.createTime = LocalDateTime.now()
//        feedback.ip = IpUtils.getIpAddr(httpServletRequest)
//        feedback.userId = userId
//        feedback.versionName = version_name
//        feedback.versionCode = version_code
//        feedback.releaseVersion = release_version
//        feedback.product = product
//        feedback.pics = pics
//        feedback.contact = contact
//        feedback.deviceId = httpServletRequest.getUniqueId()
//        feedback.channel = httpServletRequest?.getChannel()
//        feedbackService?.save(feedback)
//
//        //发送钉钉提醒
//        val markdown = "####  用户意见反馈" + "\n" +
//                "> 提醒类型：段子乐" + "\n\n" +
//                "> 系统环境：" + (if (AppConstants.isDebug()) "测试环境" else "正式环境") + "\n\n" +
//                "> 当前IP：" + IpUtils.getIpAddr(httpServletRequest) + "\n\n" +
//                "> 设备信息：" + httpServletRequest.getAppInfo() + "\n\n" +
//                "> 发送时间：" + DateTime.now().toString(AppConstants.COMM_TIME_FORMATER) + "\n\n" +
//                "> 反馈内容：" + content + "\n\n" +
//                "> 用户ID：" + userId + "\n\n"
//        dingdingService?.sendMarkdownMsg("段子乐用户意见反馈", markdown)
//
//        return Resp.createSuccess()
//    }
}