/*
 * Copyright (C) 2014 Information Management Services, Inc.
 */
package com.imsweb.seerapi.client.ndc;

import java.io.IOException;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;

import retrofit2.Response;

import com.imsweb.seerapi.client.SeerApi;
import com.imsweb.seerapi.client.ndc.NdcSeerInfo.Category;

import static org.assertj.core.api.Assertions.assertThat;

public class NdcTest {

    private static NdcService _NDC;

    @BeforeClass
    public static void setup() {
        _NDC = new SeerApi.Builder().connect().ndc();
    }

    @Test
    public void testNdcByCode() throws IOException {
        NdcProduct product = _NDC.getByCode("0002-3227").execute().body();

        assertThat(product).isNotNull();
        assertThat(product.getNdc()).isEqualTo("0002-3227");
        assertThat(product.getTypeName()).isEqualTo("HUMAN PRESCRIPTION DRUG");
        assertThat(product.getProprietaryName()).isEqualTo("Strattera");
        assertThat(product.getProprietaryNameSuffix()).isNull();
        assertThat(product.getNonProprietaryName()).containsExactly("Atomoxetine hydrochloride");
        assertThat(product.getDosageFormName()).isEqualTo("CAPSULE");
        assertThat(product.getRouteName()).containsExactly("ORAL");
        assertThat(product.getStartMarketingDate()).isEqualTo("20021126");
        assertThat(product.getEndMarketingDate()).isNull();
        assertThat(product.getMarketingCategoryName()).isEqualTo("NDA");
        assertThat(product.getApplicationNumber()).isEqualTo("NDA021411");
        assertThat(product.getLabelerName()).isEqualTo("Eli Lilly and Company");
        assertThat(product.getDeaSchedule()).isNull();
        assertThat(product.getSubstances()).hasSize(1);

        NdcSubstance substance = product.getSubstances().get(0);
        assertThat(substance.getName()).isEqualTo("ATOMOXETINE HYDROCHLORIDE");
        assertThat(substance.getStrengthNumber()).isEqualTo("10");
        assertThat(substance.getStrengthUnit()).isEqualTo("mg/1");

        assertThat(product.getPharmClass()).containsExactly("Norepinephrine Reuptake Inhibitor [EPC]", "Norepinephrine Uptake Inhibitors [MoA]");

        assertThat(product.getPackages()).isNotEmpty();
        assertThat(product.getPackages().get(0).getCode()).isEqualTo("30");
        assertThat(product.getPackages().get(0).getDescription()).isEqualToIgnoringWhitespace("30 CAPSULE in 1 BOTTLE (0002-3227-30)");

        assertThat(product.getDateAdded()).isNotNull();
        assertThat(product.getDateModified()).isNotNull();
        assertThat(product.getDateRemoved()).isNull();

        // test one with "seer" added information (0002-4483)
        product = _NDC.getByCode("0002-4483").execute().body();
        assertThat(product.getNdc()).isEqualTo("0002-4483");
        assertThat(product.getProprietaryName()).isEqualTo("Verzenio");
        assertThat(product.getSeerInfo().getCategories()).containsExactly(Category.CHEMOTHERAPY);
    }

    @Test
    public void testNdcSearch() throws IOException {
        NdcSearch search = new NdcSearch();
        search.setIncludeRemoved(true);

        Response<List<NdcProduct>> response = _NDC.search(search.paramMap()).execute();

        // hold onto total number (including "removed")
        Integer totalIncludingRemoved = Integer.valueOf(response.headers().get("X-Total-Count"));
        assertThat(totalIncludingRemoved).isGreaterThan(100000);

        List<NdcProduct> products = response.body();
        assertThat(products).hasSize(25);

        search.setQuery("daklinza");
        products = _NDC.search(search.paramMap()).execute().body();
        assertThat(products.size()).isGreaterThan(1);

        search.setRemovedSince("2016-07-21");
        products = _NDC.search(search.paramMap()).execute().body();
        assertThat(products).isEmpty();

        // test removed
        search = new NdcSearch();
        search.setIncludeRemoved(false);
        response = _NDC.search(search.paramMap()).execute();
        Integer totalExcludingRemoved = Integer.valueOf(response.headers().get("X-Total-Count"));

        assertThat(totalIncludingRemoved).isGreaterThan(totalExcludingRemoved);
    }

    @Test
    public void testNdcSearchByCategory() throws IOException {
        NdcSearch search = new NdcSearch();
        search.setCategory(Category.CHEMOTHERAPY);

        Response<List<NdcProduct>> response = _NDC.search(search.paramMap()).execute();

        assertThat(Integer.valueOf(response.headers().get("X-Total-Count"))).isGreaterThan(0);

        List<NdcProduct> products = response.body();
        assertThat(products).isNotEmpty();

        NdcProduct product = products.get(0);
        assertThat(product.getSeerInfo()).as("must have 'seerinfo'").isNotNull();
        assertThat(product.getSeerInfo().getCategories()).contains(Category.CHEMOTHERAPY);
    }

}

@@ -2860,24 +2860,106 @@
 Verzenio%22);%0A
+        assertThat(product.getSeerInfo()).as(%22must have 'seerinfo'%22).isNotNull();%0A
         asse

Support new NDC information