package com.remy.graphql.resolvers.query;

import com.remy.common.module.GoodsDTO;
import com.remy.common.module.OrderDTO;
import com.remy.common.module.UserDTO;
import com.remy.graphql.evn.RestAPIEnv;
import graphql.kickstart.tools.GraphQLQueryResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import static java.util.concurrent.CompletableFuture.supplyAsync;

@Component
@Slf4j
public class OrderQuery implements GraphQLQueryResolver {

    @Autowired
    RestAPIEnv apiEnv;

    public CompletableFuture<List<OrderDTO>> orderList() {
        log.info("start query orderList");
        return supplyAsync(() ->
                new RestTemplate().exchange(
                        apiEnv.getOrdersEndpoint(),
                        HttpMethod.GET,
                        new HttpEntity(null),
                        new ParameterizedTypeReference<List<OrderDTO>>() {
                        }).getBody());
    }

    public CompletableFuture<List<OrderDTO>> orderQuery(String goodsSerialNo) {
        log.info("start query order by goods serial no");
        return supplyAsync(() ->
                new RestTemplate().exchange(
                        apiEnv.getOrdersEndpoint() + "query?goodsSerialNo=" + goodsSerialNo,
                        HttpMethod.GET,
                        new HttpEntity(null),
                        new ParameterizedTypeReference<List<OrderDTO>>() {
                        }).getBody());
    }

    public CompletableFuture<OrderDTO> orderInfo(String orderNo) {
        log.info("start query order by order no");
        return supplyAsync(() ->
                new RestTemplate().getForObject(apiEnv.getOrdersEndpoint() + orderNo, OrderDTO.class));
    }
}
