package com.greenorange.promotion.controller.wechat;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.greenorange.promotion.annotation.RequiresPermission;
import com.greenorange.promotion.common.BaseResponse;
import com.greenorange.promotion.common.ErrorCode;
import com.greenorange.promotion.common.ResultUtils;
import com.greenorange.promotion.config.WxAccessToken;
import com.greenorange.promotion.constant.UserConstant;
import com.greenorange.promotion.model.dto.CommonStringRequest;
import com.greenorange.promotion.service.wechat.WechatGetQrcodeService;
import com.greenorange.promotion.utils.QRCodeUtil;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@Tag(name = "获取二维码模块")
@RequestMapping("/qrcode")
public class WechatGetQrcodeController {



    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Resource
    private WechatGetQrcodeService wechatGetQrcodeService;



    /**
     * (小程序端)获取接口调用凭据
     */
    @Hidden
    @GetMapping("/get/token")
    @Operation(summary = "(小程序端)获取接口调用凭据", description = "参数：无, 权限：所有人, 方法名：getAccessToken")
    public BaseResponse<WxAccessToken> getAccessToken() {
        WxAccessToken wxAccessToken = wechatGetQrcodeService.getApiAccessToken();
        return ResultUtils.success(wxAccessToken);
    }


    /**
     * 微信小程序获取二维码
     * @return
     * @throws IOException
     */
    @Hidden
    @PostMapping("/get/qrcode")
    @Operation(summary = "微信小程序获取二维码", description = "参数：无, 权限：所有人, 方法名：getQrcode")
//    @RequiresPermission(mustRole = UserConstant.DEFAULT_ROLE)
    public BaseResponse<String> getQrcode(@Valid @RequestBody CommonStringRequest commonStringRequest) throws IOException {
        String inviteCode = commonStringRequest.getTemplateString();
        String view = wechatGetQrcodeService.getWxQrCode(inviteCode);
        return ResultUtils.success(view);
    }






}
