package com.hdkj.lt.si.service.remote.impl;

import com.hdkj.lt.core.rest.model.AbstractCallResponseEntity;
import com.hdkj.lt.core.rest.service.impl.AbstractDefaultRemoteCallService;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author Annie
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CityFeederTopoRemoteCallServiceImpl extends AbstractDefaultRemoteCallService {

    private static final String SUCCESS_CODE = "200";

    @Value("${remote.topo.request-uri}")
    private String requestUri;

    @Override
    protected CallConfig buildCallConfig() {
        return CallConfig.builder()
                .requestUri(this.requestUri)
                .successCode(SUCCESS_CODE)
                .responseType(CityFeederTopoRemoteCallServiceImpl.DataManagerCallResponseEntity.class)
                .build();
    }

    @Override
    protected <B> HttpEntity<B> createHttpEntity(B body) {
        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        headers.set(HttpHeaders.ACCEPT_CHARSET, Charsets.UTF_8.toString());
        return new HttpEntity<>(body, headers);
    }

    @Getter
    @Setter
    public static class DataManagerCallResponseEntity extends AbstractCallResponseEntity {

        private String code; //响应状态码

        private Object data; //响应数据

        private String message; //响应信息

        private String errMsg; //错误信息

        private Object stackTraceElement; //堆栈跟踪元素

        @Override
        public Object getCode() {
            return code;
        }

        @Override
        public Object getData() {
            return data;
        }

        @Override
        public String getMsg() {
            return Optional.ofNullable(errMsg).orElse(message);
        }

    }

}
