/*
 * EVE Swagger Interface
 * An OpenAPI for EVE Online
 *
 *
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */

package net.troja.eve.esi.api;

import java.util.ArrayList;
import java.util.List;
import net.troja.eve.esi.ApiException;
import net.troja.eve.esi.model.CharacterOrdersResponse;
import net.troja.eve.esi.model.CorporationOrdersResponse;
import net.troja.eve.esi.model.MarketGroupResponse;
import net.troja.eve.esi.model.MarketHistoryResponse;
import net.troja.eve.esi.model.MarketOrdersResponse;
import net.troja.eve.esi.model.MarketPricesResponse;
import net.troja.eve.esi.HeaderUtil;
import net.troja.eve.esi.model.CharacterOrdersHistoryResponse;
import net.troja.eve.esi.model.CorporationOrdersHistoryResponse;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

/**
 * API tests for MarketApi
 */
public class MarketApiTest extends GeneralApiTest {

    private final MarketApi api = new MarketApi();

    @Before
    public void setUp() {
        api.setApiClient(apiClient);
    }

    /**
     * List open orders from a character
     *
     * List open market orders placed by a character  ---  This route is cached for up to 1200 seconds  SSO Scope: esi-markets.read_character_orders.v1
     *
     * @throws ApiException
     *             if the Api call fails
     */
    @Test
    public void getCharactersCharacterIdOrdersTest() throws ApiException {
        final List<CharacterOrdersResponse> response = api.getCharactersCharacterIdOrders(characterId, DATASOURCE, null, null);

        assertThat(response, notNullValue());
    }

    /**
     * List historical orders by a character
     *
     * List cancelled and expired market orders placed by a character up to 90 days in the past.  ---  This route is cached for up to 3600 seconds  SSO Scope: esi-markets.read_character_orders.v1
     *
     * @throws ApiException
     *          if the Api call fails
     */
    @Test
    public void getCharactersCharacterIdOrdersHistoryTest() throws ApiException {
        Integer page = null;
        List<CharacterOrdersHistoryResponse> response = api.getCharactersCharacterIdOrdersHistory(characterId, DATASOURCE, null, page, null);

        // TODO: test validations
    }

    /**
     * List open orders from a corporation
     *
     * List open market orders placed on behalf of a corporation  ---  This route is cached for up to 1200 seconds  --- Requires one of the following EVE corporation role(s): Accountant, Trader  SSO Scope: esi-markets.read_corporation_orders.v1
     *
     * @throws ApiException
     *          if the Api call fails
     */
    @Test
    @Ignore("Needs corporation with read access")
    public void getCorporationsCorporationIdOrdersTest() throws ApiException {
        Integer corporationId = null;
        Integer page = null;
        List<CorporationOrdersResponse> response = api.getCorporationsCorporationIdOrders(corporationId, DATASOURCE, null, page, null);

        // TODO: test validations
    }

    /**
     * List historical orders from a corporation
     *
     * List cancelled and expired market orders placed on behalf of a corporation up to 90 days in the past.  ---  This route is cached for up to 3600 seconds  --- Requires one of the following EVE corporation role(s): Accountant, Trader  SSO Scope: esi-markets.read_corporation_orders.v1
     *
     * @throws ApiException
     *          if the Api call fails
     */
    @Test
    @Ignore("Needs corporation with read access")
    public void getCorporationsCorporationIdOrdersHistoryTest() throws ApiException {
        Integer corporationId = null;
        Integer page = null;
        List<CorporationOrdersHistoryResponse> response = api.getCorporationsCorporationIdOrdersHistory(corporationId, DATASOURCE, null, page, null);

        // TODO: test validations
    }

    /**
     * Get item groups
     *
     * Get a list of item groups  ---  This route expires daily at 11:05
     *
     * @throws ApiException
     *             if the Api call fails
     */
    @Test
    public void getMarketsGroupsTest() throws ApiException {
        final List<Integer> response = api.getMarketsGroups(DATASOURCE, null);

        assertThat(response.size(), greaterThan(0));
    }

    /**
     * Get item group information
     *
     * Get information on an item group  ---  This route expires daily at 11:05
     *
     * @throws ApiException
     *             if the Api call fails
     */
    @Test
    public void getMarketsGroupsMarketGroupIdTest() throws ApiException {
        final Integer marketGroupId = 2;
        final MarketGroupResponse response = api.getMarketsGroupsMarketGroupId(marketGroupId, LANGUAGE, DATASOURCE, null, LANGUAGE);

        assertThat(response, notNullValue());
        assertThat(response.getName(), equalTo("Blueprints & Reactions"));
    }

    /**
     * List market prices
     *
     * Return a list of prices  ---  This route is cached for up to 3600 seconds
     *
     * @throws ApiException
     *             if the Api call fails
     */
    @Test
    public void getMarketsPricesTest() throws ApiException {
        final List<MarketPricesResponse> response = api.getMarketsPrices(DATASOURCE, null);

        assertThat(response.size(), greaterThan(0));
        final MarketPricesResponse market = response.get(0);
        assertThat(market.getTypeId(), greaterThan(0));
    }

    /**
     * List historical market statistics in a region
     *
     * Return a list of historical market statistics for the specified type in a region  ---  This route expires daily at 11:05
     *
     * @throws ApiException
     *             if the Api call fails
     */
    @Test
    public void getMarketsRegionIdHistoryTest() throws ApiException {
        final List<MarketHistoryResponse> response = api.getMarketsRegionIdHistory(REGION_ID_THE_FORGE, TYPE_ID_VELDSPAR, DATASOURCE, null);

        assertThat(response.size(), greaterThan(0));
        final MarketHistoryResponse historyResponse = response.get(0);
        assertThat(historyResponse.getOrderCount(), greaterThan(0l));
    }

    /**
     * List orders in a region
     *
     * Return a list of orders in a region  ---  This route is cached for up to 300 seconds
     *
     * @throws ApiException
     *             if the Api call fails
     */
    @Test
    public void getMarketsRegionIdOrdersTest() throws ApiException {
        final String orderType = "buy";
        final Integer page = null;
        final List<MarketOrdersResponse> response = api.getMarketsRegionIdOrders(orderType, REGION_ID_THE_FORGE, DATASOURCE, null, page, TYPE_ID_VELDSPAR);

        assertThat(response.size(), greaterThan(0));
        final MarketOrdersResponse ordersResponse = response.get(0);
        assertThat(ordersResponse.getMinVolume(), greaterThan(0));
    }

    /**
     * List type IDs relevant to a market
     *
     * Return a list of type IDs that have active orders in the region, for efficient market indexing.  ---  This route is cached for up to 600 seconds
     *
     * @throws ApiException
     *          if the Api call fails
     */
    @Test
    public void getMarketsRegionIdTypesTest() throws ApiException {
        Integer regionId = 10000002;
        Integer page = null;
        List<Integer> response = api.getMarketsRegionIdTypes(regionId, DATASOURCE, null, page);

        assertThat(response, notNullValue());
        assertThat(response.size(), greaterThan(0));
    }

    /**
     * List orders in a structure
     *
     * Return all orders in a structure  ---  This route is cached for up to 300 seconds  SSO Scope: esi-markets.structure_markets.v1
     *
     * @throws ApiException
     *             if the Api call fails
     */
    @Test
    @Ignore("No static structure to use here")
    public void getMarketsStructuresStructureIdTest() throws ApiException {
    }

    /**
     * X-Pages example
     * @throws ApiException
     */
    @Test
    public void pagingExample() throws ApiException {
        final String orderType = "all";
        //Save all results in this List
        final List<MarketOrdersResponse> result = new ArrayList<MarketOrdersResponse>();

        //Step 1: Get first page

        //Get market orders
        List<MarketOrdersResponse> response = api.getMarketsRegionIdOrders(orderType, REGION_ID_THE_FORGE, DATASOURCE, null, null, null);
        result.addAll(response);

        //Step 2: Safely get X-Pages header

        Integer xPages = HeaderUtil.getXPages(apiClient.getResponseHeaders());
        if (xPages == null || xPages < 2) { //Better safe than sorry
            return;
        }

        //Step 3: Get the rest of the pages

        //For each page greater than one. This can be done in threads, but, require a new ApiClient and MarketApi for each thread
        for (int page = 2; page <= xPages; page++) {
            //Get market orders
            List<MarketOrdersResponse> pageResponse = api.getMarketsRegionIdOrders(orderType, REGION_ID_THE_FORGE, DATASOURCE, null, page, null);
            result.addAll(pageResponse);
        }

        assertThat(result, notNullValue());
        assertThat(result.size(), greaterThan(0));
    }
}
