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

import cn.hutool.json.JSONUtil;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.estate.api.dto.BaseUser;
import com.sptg.estate.api.entity.*;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.third.api.contants.UrlContants;
import com.sptg.third.api.entity.grid.GridConfig;
import com.sptg.third.api.feigns.grid.GridEstateFeign;
import com.sptg.third.biz.grid.service.GridConfigServiceInterface;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.lettuce.core.models.role.RedisSlaveInstance;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

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

/**
 * @author zwx
 * @create 2020/11/28 20:23
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class GridEstateController implements GridEstateFeign {

    private final GridConfigServiceInterface gridConfigServiceInterface;
    private final ConfFeign confFeign;

    /**
     * 推送社区到网格
     *
     * @param agency
     * @return
     */
    @Override
    @PostMapping(value = "/third/grid/estate/agency/push")
    public ResultBean<String> gridAgencyPush(@RequestBody Agency agency) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);
        Map resultMap = getRequestResult(agency, gridConfig, UrlContants.Grid.AGENT_SYNC);
        ResultBean resultBean = new ResultBean();
        if (Integer.valueOf(resultMap.get("code").toString()) == 200 && (boolean) resultMap.get("success")) {
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    /**
     * 推送小区到网格
     *
     * @param residential
     * @return
     */
    @Override
    @PostMapping(value = "/third/grid/estate/residential/push")
    public ResultBean<String> gridResidentialPush(@RequestBody Residential residential) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);

        Map resultMap = getRequestResult(residential, gridConfig, UrlContants.Grid.RESIDENTIAL_SYNC);
        ResultBean resultBean = new ResultBean();
        if (Integer.valueOf(resultMap.get("code").toString()) == 200 && (boolean) resultMap.get("success")) {
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    /**
     * 推送楼栋到网格
     *
     * @param building
     * @return
     */
    @Override
    @PostMapping(value = "/third/grid/estate/building/push")
    public ResultBean<String> gridBuildingPush(@RequestBody Building building) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);

        Map resultMap = getRequestResult(building, gridConfig, UrlContants.Grid.BUILDING_SYNC);
        ResultBean resultBean = new ResultBean();
        if (Integer.valueOf(resultMap.get("code").toString()) == 200 && (boolean) resultMap.get("success")) {
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    /**
     * 推送楼层到网格
     *
     * @param floor
     * @return
     */
    @Override
    @PostMapping(value = "/third/grid/estate/floor/push")
    public ResultBean<String> gridFloorPush(@RequestBody Floor floor) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);

        Map resultMap = getRequestResult(floor, gridConfig, UrlContants.Grid.FLOOR_SYNC);
        ResultBean resultBean = new ResultBean();
        if (Integer.valueOf(resultMap.get("code").toString()) == 200 && (boolean) resultMap.get("success")) {
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    /**
     * 推送单元到网格
     *
     * @param unit
     * @return
     */
    @Override
    @PostMapping(value = "/third/grid/estate/unit/push")
    public ResultBean<String> gridUnitPush(@RequestBody Unit unit) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);
        Map resultMap = getRequestResult(unit, gridConfig, UrlContants.Grid.UNIT_SYNC);
        ResultBean resultBean = new ResultBean();
        if (Integer.valueOf(resultMap.get("code").toString()) == 200 && (boolean) resultMap.get("success")) {
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    /**
     * 推送房间到网格
     *
     * @param room
     * @return
     */
    @Override
    @PostMapping(value = "/third/grid/estate/room/push")
    public ResultBean<String> gridRoomPush(@RequestBody Room room) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);

        Map resultMap = getRequestResult(room, gridConfig, UrlContants.Grid.ROOM_SYNC);
        ResultBean resultBean = new ResultBean();
        if (Integer.valueOf(resultMap.get("code").toString()) == 200 && (boolean) resultMap.get("success")) {
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    /**
     * 推送物业报修到网格
     *
     * @param estateRepair
     * @return
     */
    @Override
    @PostMapping(value = "/third/grid/estate/repair/push")
    public ResultBean<String> gridEstateRepairPush(@RequestBody EstateRepair estateRepair) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);
        Map resultMap = getRequestResult(estateRepair, gridConfig, UrlContants.Grid.ESTATE_REPAIR_SYNC);
        ResultBean resultBean = new ResultBean();
        if (Integer.valueOf(resultMap.get("code").toString()) == 200 && (boolean) resultMap.get("success")) {
            resultBean.setData("操作成功");
        }

        return resultBean;
    }

    /**
     * 推送人员到网格
     *
     * @param applyUser
     * @return
     */
    @Override
    @PostMapping(value = "/third/grid/estate/user/push")
    public ResultBean<String> gridUserPush(@RequestBody ApplyUser applyUser) {
        GridConfig gridConfig = this.gridConfigServiceInterface.getById(1L);

        String fileServerHost = this.confFeign.getFileServerHost();
        applyUser.setImageUri(fileServerHost + applyUser.getImageUri());
        Map resultMap = getRequestResult(applyUser, gridConfig, UrlContants.Grid.BASEUSER_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();
    }
}
