package com.sptg.third.biz.grid.controller;

import cn.hutool.json.JSONUtil;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.entity.Device;
import com.sptg.estate.api.entity.AccessRecords;
import com.sptg.third.api.contants.UrlContants;
import com.sptg.third.api.entity.grid.GridConfig;
import com.sptg.third.api.feigns.grid.GridDoorDeviceFeign;
import com.sptg.third.api.feigns.grid.GridEstateFeign;
import com.sptg.third.biz.grid.service.GridConfigServiceInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.RestController;
import org.springframework.web.client.RestTemplate;

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

/**
 * Created By lidexi in 2021/1/13
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class GridDoorDeviceController implements GridDoorDeviceFeign {

    private final GridConfigServiceInterface gridConfigServiceInterface;
    @Override
    public ResultBean<String> gridDevicePush(Device device) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);
        Map resultMap = getRequestResult(device,gridConfig, UrlContants.Grid.DOOR_DEVICE_SYNC);
        ResultBean resultBean = new ResultBean();
        if(Integer.valueOf(resultMap.get("code").toString())==200 && (boolean)resultMap.get("success")){
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    @Override
    public ResultBean<String> gridAccessrecordPush(AccessRecords accessRecords) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);
        Map resultMap = getRequestResult(accessRecords,gridConfig,UrlContants.Grid.DOOR_DEVICE_ACCESS_SYNC);
        ResultBean resultBean = new ResultBean();
        if(Integer.valueOf(resultMap.get("code").toString())==200 && (boolean)resultMap.get("success")){
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    /**
     * 获取请求结果
     * @param object  请求体
     * @param gridConfig host+port
     * @param apiPath 请求路径
     * @return
     */
    private Map getRequestResult(Object object, GridConfig gridConfig, String apiPath){
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity responseEntity = null;
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE,"application/json;charset=UTF-8");

        HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(object), headers);
        ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
        };
        String url = gridConfig.getGridHost()+":"+gridConfig.getGridPort()+apiPath;
        URI uri = URI.create(url);
        responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
        return (Map) responseEntity.getBody();
    }
}
