/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.community.grid.wrapper;

import cn.hutool.json.JSONUtil;
import org.springblade.common.constant.RequestUrlConstant;
import org.springblade.community.entity.Agency;
import org.springblade.community.entity.Residential;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.community.grid.entity.GridResource;
import org.springblade.community.grid.vo.GridResourceVO;
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.client.RestTemplate;

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

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-03-23
 */
public class GridResourceWrapper extends BaseEntityWrapper<GridResource, GridResourceVO>  {

	public static GridResourceWrapper build() {
		return new GridResourceWrapper();
 	}

	@Override
	public GridResourceVO entityVO(GridResource gridResource) {
		GridResourceVO gridResourceVO = Objects.requireNonNull(BeanUtil.copy(gridResource, GridResourceVO.class));


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

		if(gridResource.getResourseIds() != null){
			if(gridResource.getLevel() == 1){//园区

				ParameterizedTypeReference<R<List<Agency>>> reference = new ParameterizedTypeReference<R<List<Agency>>>() {
				};

				String url = RequestUrlConstant.AGENCYLIST_BYIDS+"?agencyIds="+gridResource.getResourseIds();
				URI uri = URI.create(url);

				responseEntity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, reference);
				gridResourceVO.setAgencyList((List<Agency>)responseEntity.getBody());
			}else if(gridResource.getLevel() == 2){//小区
				ParameterizedTypeReference<R<List<Residential>>> reference = new ParameterizedTypeReference<R<List<Residential>>>() {
				};

				String url = RequestUrlConstant.RESIDENTIALLIST_BYIDS+"?residentialIds="+gridResource.getResourseIds();
				URI uri = URI.create(url);

				responseEntity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, reference);
				gridResourceVO.setResidentialList((List<Residential>)responseEntity.getBody());
			}
		}


		return gridResourceVO;
	}

}
