package org.springblade.community.wx.controller;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.community.wx.entity.OpenIdDto;
import org.springblade.community.wx.entity.SendDto;
import org.springblade.core.tool.api.R;
import org.springblade.community.wx.entity.AccessToKenDto;
import org.springblade.community.wx.entity.DataDto;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "微信后台模块")
@RequestMapping("/wx")
public class WxController {


	@ApiOperation("获取开发数据")
	@PostMapping("/getOpenData")
	public R getOpenData ( DataDto dataDto){
		byte[] key = Base64.getDecoder().decode(dataDto.getSessionKey().replaceAll(" ","+"));
		byte[] iv = Base64.getDecoder().decode(dataDto.getIV().replaceAll(" ","+"));
		byte[] content = Base64.getDecoder().decode(dataDto.getEncryptedData().replaceAll(" ","+"));
		AES aes = new AES(Mode.CBC, Padding.PKCS5Padding,key,iv);
		return R.data(new String(aes.decrypt(content)));
	}

	@ApiOperation("获取accesstoken")
	@GetMapping("/getAccessToken")
	public R getAccessToken (AccessToKenDto accessToKenDto){
		HttpResponse response = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token"+"?appid="+accessToKenDto.getAppId()+"&secret="+accessToKenDto.getSecret()+"&grant_type="+accessToKenDto.getGrantType()).execute();
		return R.data(response.body());
	}

	@ApiOperation("获取openId")
	@GetMapping("/getOpenId")
	public R getOpenId (OpenIdDto openIdDto){
		HttpResponse response = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session"+"?appid="+openIdDto.getAppId()+"&secret="+openIdDto.getSecret()+"&js_code="+openIdDto.getJsCode()+"&grant_type="+openIdDto.getGrantType()).execute();
		return R.data(response.body());
	}

	@ApiOperation("消息推送")
	@PostMapping("/msgSend")
	public R msgSend (@RequestBody SendDto sendDto){
		RestTemplate restTemplate = new RestTemplate();
//		HttpHeaders httpHeaders =
		ResponseEntity responseEntity = null;
		HttpHeaders headers = new HttpHeaders();
		headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
		System.out.println(sendDto.toString());
		Map<String, Object> map = new HashMap<>();
		map.put("touser", sendDto.getTouser());
		map.put("template_id", sendDto.getTemplateId());
		map.put("page", sendDto.getPage());
		map.put("data", sendDto.getData());
		map.put("miniprogram_state", sendDto.getMiniprogramState());
		map.put("lang", sendDto.getLang());

		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(map), headers);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+sendDto.getAccessToken();
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
		return R.data(responseEntity);
	}

}
