package com.galaxis.manatee.manager.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.galaxis.manatee.constant.ChuanYunConstant;
import com.galaxis.manatee.entity.chuanyun.*;
import com.galaxis.manatee.manager.ChuanYunManager;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author zcj
 * @version 0.1
 * @date 2020/2/5 4:02 下午
 */
@Slf4j
@Service
public class DefaultChuanyunManagerImpl implements ChuanYunManager {

    private ObjectMapper objectMapper;

    public DefaultChuanyunManagerImpl(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public <T> ChuanyunFindResponse<T> find(String schemaCode, String bizObjectId) {
        @Data
        class Find{
            @JsonProperty(value = "ActionName")
            private String actionName="LoadBizObject";
            @JsonProperty(value = "SchemaCode")
            private String schemaCode;
            @JsonProperty(value = "BizObjectId")
            private String bizObjectId;
        }
        //构建请求体
        Find find=new Find();
        find.setSchemaCode(schemaCode);
        find.setBizObjectId(bizObjectId);
        //请求客户端
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("EngineCode",ChuanYunConstant.ENGINE_CODE);
        httpHeaders.add("EngineSecret",ChuanYunConstant.ENGINE_SECRET);
        HttpEntity<Find> httpEntity=new HttpEntity<>(find,httpHeaders);
        ChuanyunFindResponse<T> chuanyunFindResponse =new ChuanyunFindResponse<>();
        return restTemplate.postForObject(ChuanYunConstant.CHUAN_YUN_INVOKE_URL,httpEntity, chuanyunFindResponse.getClass());
    }

    @Override
    public <T> ChuanyunFindAllResponse<T> findAll(String schemaCode, Filter filter) throws JsonProcessingException {
        @Data
        class FindList{
            @JsonProperty(value = "ActionName")
            private String actionName="LoadBizObjects";
            @JsonProperty(value = "SchemaCode")
            private String schemaCode;
            @JsonProperty(value = "Filter")
            private String filter;

            public FindList(String schemaCode, String filter) {
                this.schemaCode = schemaCode;
                this.filter = filter;
            }
        }
        //构建请求体
        FindList findList=new FindList(schemaCode,objectMapper.writeValueAsString(filter));
        //请求客户端
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("EngineCode",ChuanYunConstant.ENGINE_CODE);
        httpHeaders.add("EngineSecret",ChuanYunConstant.ENGINE_SECRET);
        HttpEntity<FindList> httpEntity=new HttpEntity<>(findList,httpHeaders);
        ChuanyunFindAllResponse<T> chuanyunFindAllResponse =new ChuanyunFindAllResponse<>();
        return restTemplate.postForObject(ChuanYunConstant.CHUAN_YUN_INVOKE_URL,httpEntity, chuanyunFindAllResponse.getClass());
    }

    @Override
    public ChuanyunSaveResponse save(String schemaCode, String bizObject, Boolean isSubmit) {
        @Data
        class Save{
            @JsonProperty(value = "ActionName")
            private String actionName="CreateBizObject";
            @JsonProperty(value = "SchemaCode")
            private String schemaCode;
            @JsonProperty(value = "BizObject")
            private String bizObject;
            @JsonProperty(value = "IsSubmit")
            private Boolean isSubmit;

            public Save(String schemaCode, String bizObject, Boolean isSubmit) {
                this.schemaCode = schemaCode;
                this.bizObject = bizObject;
                this.isSubmit = isSubmit;
            }
        }
        Save save=new Save(schemaCode,bizObject,isSubmit);
        //请求客户端
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("EngineCode",ChuanYunConstant.ENGINE_CODE);
        httpHeaders.add("EngineSecret",ChuanYunConstant.ENGINE_SECRET);
        HttpEntity<Save> httpEntity=new HttpEntity<>(save,httpHeaders);
        ChuanyunSaveResponse chuanyunSaveResponse =new ChuanyunSaveResponse();
        return restTemplate.postForObject(ChuanYunConstant.CHUAN_YUN_INVOKE_URL,httpEntity, chuanyunSaveResponse.getClass());
    }

    @Override
    public ChuanyunSaveAllResponse saveAll(String schemaCode, List<String> bizObjectArray, Boolean isSubmit) {
        @Data
        class SaveAll{
            @JsonProperty(value = "ActionName")
            private String actionName="CreateBizObjects";
            @JsonProperty(value = "SchemaCode")
            private String schemaCode;
            @JsonProperty(value = "BizObjectArray")
            private List<String> bizObjectArray;
            @JsonProperty(value = "IsSubmit")
            private Boolean isSubmit;

            public SaveAll(String schemaCode, List<String> bizObjectArray, Boolean isSubmit) {
                this.schemaCode = schemaCode;
                this.bizObjectArray = bizObjectArray;
                this.isSubmit = isSubmit;
            }
        }
        SaveAll saveAll=new SaveAll(schemaCode,bizObjectArray,isSubmit);
        //请求客户端
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("EngineCode",ChuanYunConstant.ENGINE_CODE);
        httpHeaders.add("EngineSecret",ChuanYunConstant.ENGINE_SECRET);
        HttpEntity<SaveAll> httpEntity=new HttpEntity<>(saveAll,httpHeaders);
        ChuanyunSaveAllResponse chuanyunSaveAllResponse =new ChuanyunSaveAllResponse();
        return restTemplate.postForObject(ChuanYunConstant.CHUAN_YUN_INVOKE_URL,httpEntity, chuanyunSaveAllResponse.getClass());
    }
}
