package org.springblade.community.grid.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.RequestUrlConstant;
import org.springblade.common.constant.ServicesName;
import org.springblade.community.entity.Agency;
import org.springblade.community.grid.entity.GridResource;
import org.springblade.community.grid.service.IGridResourceService;
import org.springblade.core.tool.api.R;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

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

/**
 * Created By lidexi in 2021/3/23
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/grid-community")
@Api( tags = "网格园区接口")
public class CommunityController {
	private IGridResourceService gridResourceService;

	/**
	 * 根据区code获取园区列表
	 */
	@GetMapping("/getAgencysByAreaCode")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "根据区code获取园区列表", notes = "传入areaCode")
	public R<List<Agency>> getAgencysByAreaCode(@RequestParam String areaCode){
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity responseEntity = null;
		HttpHeaders headers = new HttpHeaders();
//		headers.add(HttpHeaders.CONTENT_TYPE, "text/plain");
		Map<String, Object> map = new HashMap<>();
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(map), headers);
		ParameterizedTypeReference<R<List<Agency>>> reference = new ParameterizedTypeReference<R<List<Agency>>>() {
		};
		String url = RequestUrlConstant.AGENCY_BYAREACODE+"?areaCode="+areaCode;
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, reference);
		return (R<List<Agency>>)responseEntity.getBody();
	}

	/**
	 * 自定义网格获取园区列表
	 **/
	@GetMapping("/getAgencyListForCustom")
	@ApiOperation("自定义网格获取园区列表")
	public R<List<Agency>> getAgencyListForCustom(@RequestParam String gridId){
		GridResource detail = gridResourceService.getOne(new QueryWrapper<GridResource>().lambda().eq(GridResource::getGridId, gridId));
		Assert.notNull(detail);
		String res = HttpUtil.get(RequestUrlConstant.AGENCYLIST_BYIDS, new HashMap<String, Object>() {{
			put(CommunityController.getGridResourceLevelName(detail.getLevel()), detail.getResourseIds());
		}});
		return (R<List<Agency>>) JSON.parseObject(res,R.class);
	}

	public static String getGridResourceLevelName(Integer level){
		switch (level){
			case 1: return "agencyIds";
			case 2: return "residentialIds";
			default: return "";
		}
	}
}
