package com.cxyxiaomo.epp.access.controller;

import com.cxyxiaomo.epp.access.pojo.UnlimitedQRCodeParam;
import com.cxyxiaomo.epp.access.service.WeChatTokenServiceImpl;
import com.cxyxiaomo.epp.common.response.Res;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/access/wechat")
public class WeChatTokenController {

    @Autowired
    private WeChatTokenServiceImpl weChatTokenService;

    @GetMapping("/getAccessToken")
    @ResponseBody
    public Res getAccessToken() {
        String accessToken = weChatTokenService.getAccessToken();
        return Res.success(accessToken);
    }

    @GetMapping(value = "/getUnlimitedQRCode", produces = MediaType.IMAGE_PNG_VALUE)
    @ResponseBody
    @SneakyThrows
    public byte[] getUnlimitedQRCode(@RequestParam(value = "envVersion", required = false, defaultValue = "develop") String envVersion,
                                     @RequestParam(value = "page", required = false, defaultValue = "pages/index/index") String page,
                                     @RequestParam(value = "width", required = false, defaultValue = "430") Integer width,
                                     @RequestParam(value = "autoColor", required = false, defaultValue = "false") Boolean autoColor,
                                     @RequestParam(value = "isHyaline", required = false, defaultValue = "false") Boolean isHyaline,
                                     @RequestParam(value = "scene", required = true) String scene) {
        String accessToken = weChatTokenService.getAccessToken();
        UnlimitedQRCodeParam unlimitedQRCodeParam = new UnlimitedQRCodeParam();
        unlimitedQRCodeParam.setScene(scene);
        unlimitedQRCodeParam.setPage(page);
        unlimitedQRCodeParam.setEnvVersion(envVersion);
        unlimitedQRCodeParam.setWidth(width);
        unlimitedQRCodeParam.setAutoColor(autoColor);
        unlimitedQRCodeParam.setIsHyaline(isHyaline);
        okhttp3.ResponseBody responseBody = weChatTokenService.getUnlimitedQRCodeFromApi(accessToken, unlimitedQRCodeParam);

        return responseBody.bytes();
    }
}
