package org.springblade.app.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springblade.app.dto.AccessTokenDto;
import org.springblade.app.dto.OpenIdDto;
import org.springblade.app.dto.SendDto;
import org.springblade.core.tool.api.R;
import org.springblade.system.expand.entity.WxConf;
import org.springblade.system.expand.feign.IWxConfClient;
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.HashMap;
import java.util.Map;

/**
 * @author lidexi
 * @date 2021/5/12 14:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app")
public class AppController {

	private final IWxConfClient wxConfClient;

	/**
	 * 根据配置id获取园区小程序配置
	 */
	@GetMapping("/wxConf/getById")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "通过id获取配置", notes = "传入ids")
	public R<WxConf> getById(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		WxConf searchEntity = new WxConf();
		searchEntity.setId(id);
		return R.data(wxConfClient.detail(searchEntity));
	}

	@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);
	}
}
