/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gr.hua.dit.oop2.project1.NYT;

import gr.hua.dit.oop2.project1.NYT.api.ApiCaller;
import gr.hua.dit.oop2.project1.NYT.api.JsonParser;
import gr.hua.dit.oop2.project1.NYT.basics.Article;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author Christos Sardianos <sardianos@hua.gr>
 */
public class DemoMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        ArrayList<Article> NewYorkTimes_articles = new ArrayList<>();
        
        String apiUrl = "https://api.nytimes.com/svc/search/v2/articlesearch.json";
        String apiKey = "f812fa3556e745639e378dfc9f7b4fda";
        String queryParam = "obama";
        String fromDate = "20180422";
        String toDate = "20180423";

        ApiCaller http = new ApiCaller();
        
        try {
            System.out.println("Testing 1 - Send Http GET request");
            InputStream apiResponse = http.sendGet(apiUrl, apiKey, queryParam, fromDate, toDate);
            JsonParser jp = new JsonParser();
            JSONObject jsonObject = jp.readAsJson(apiResponse);
            
            JSONObject resp = (JSONObject) jsonObject.get("response");
            JSONArray docs = (JSONArray) resp.get("docs");
            
            for (Iterator it = docs.iterator(); it.hasNext();) {
                JSONObject article = (JSONObject) it.next();
                String id = (String) article.get("_id");
                String snippet = (String) article.get("snippet");
                JSONObject headline = (JSONObject) article.get("headline");
                String mainTitle = (String) headline.get("main");
                String pubDate = (String) article.get("pub_date");
                Long wordCount = (Long) article.get("word_count");
                Article curArticle = new Article(id, snippet, mainTitle, pubDate, wordCount);
                System.out.println(curArticle);
                System.out.println("-----");
                
                NewYorkTimes_articles.add(curArticle);
            }
            
        } catch (Exception ex) {
            Logger.getLogger(DemoMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
