package com.cclotus.register.controller.wxapp

import com.cclotus.register.domain.WXUser
import com.cclotus.register.service.WXLoginService
import com.cclotus.register.service.WXUserService
import com.cclotus.register.vo.LoginForm
import com.ruoyi.common.core.domain.AjaxResult
import net.glxn.qrgen.QRCode
import net.glxn.qrgen.image.ImageType
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.util.Assert
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/user")
class UserController {

    @Autowired
    lateinit var loginService: WXLoginService

    @Autowired
    lateinit var wxUserService: WXUserService

    private val URL = "https://pingyiban.com/qrcode?userId="

    /**
     * 业务用户微信登录
     *
     * @return 登录结果
     */
    @PostMapping("/login")
    fun login(@RequestBody form: LoginForm): AjaxResult {
        Assert.hasText(form.code, "登录获取code不可为空")
        Assert.hasText(form.appId, "登录获取appId不可为空")
        val loginResult = loginService.login(form.code!!, form.appId!!)
        return AjaxResult.success(loginResult)
    }

    /**
     * 微信用户信息注册
     * @param user 用户信息
     * @param openId 此用户openId
     * @return 用户登录token
     */
    @PostMapping("/register")
    fun register(@Validated @RequestBody user: WXUser, openId: String?): AjaxResult {
        Assert.hasText(openId, "用户openId不可为空")
        user.openid = openId
        return AjaxResult.success(loginService.register(user))
    }

    /**
     * 微信用户更新用户信息
     * @param userId 用户ID
     * @param user 用户信息
     * @return 用户修改后的信息
     */
    @PostMapping("/info")
    fun update(@Validated @RequestBody user: WXUser, userId: String?): AjaxResult {
        Assert.hasText(userId, "用户ID不可为空")
        return AjaxResult.success(wxUserService.updateUser(userId!!, user))
    }

    /**
     * 获取用户信息
     * @param userId 用户ID
     * @return 用户详细信息
     */
    @GetMapping("/info")
    fun info(userId: String?): AjaxResult {
        Assert.hasText(userId, "用户ID不可为空")
        return AjaxResult.success(wxUserService.selectWXUserByUserId(userId!!))
    }

    /**
     * 用户注销
     *
     * @param userId 待注销的用户ID
     * @return
     */
    @PostMapping("/unregister")
    fun logout(userId: String?): AjaxResult {
        Assert.hasText(userId, "用户ID不可为空")
        return AjaxResult.success(loginService.unregister(userId!!))
    }

    /**
     * 生成推荐码
     * @param userId 用户ID
     */
    @GetMapping(path = ["/recommend/{userId}.jpg"], produces = [MediaType.IMAGE_JPEG_VALUE])
    fun recommend(@PathVariable("userId") userId: String?, width: Int?, height: Int?): ByteArray {
        Assert.hasText(userId, "用户ID不可为空")
        // Generate QR code image
        val qrCode: QRCode = QRCode.from(URL + userId).withSize(width ?: 256, height ?: 256)
        val bufferedImage = qrCode.to(ImageType.JPG).stream()
        return bufferedImage.toByteArray()
    }

}
