// implemented by Joshua Dickinson, updated by Yoon Chung Han

import org.json.*;

//String baseURL = "http://api.nytimes.com/svc/search/v1/article"; //org
//String apiKey = "2c4094f9012ef6f5c40ba2c6f74c9d78:7:65423204"; //org

String baseURL = "http://api.nytimes.com/svc/books/v2/lists/best-sellers/history.json"; //"http://api.nytimes.com/svc/books/v2/lists.json"; //"http://api.nytimes.com/svc/search/v1/article";
String apiKey = "26ed917d909ee9d8adae7a5139cfab35:5:68842405";//"2c4094f9012ef6f5c40ba2c6f74c9d78:7:65423204";

// this works http://api.nytimes.com/svc/books/v2/lists.xml?list=hardcover-fiction&weeks-on-list=2&api-key=26ed917d909ee9d8adae7a5139cfab35:5:68842405
// this works http://api.nytimes.com/svc/books/v2/lists/best-sellers/history.xml?title=soul%20eater&api-key=26ed917d909ee9d8adae7a5139cfab35:5:68842405

PFont myFont = createFont("Helvetica", 14);
int lengthOfRainbow = 100; //for transition of colors
int rainbowOffset = 0;

void setup() {
smooth();
//size(500,600); //org
size(1400,1000); //org
background(0);
textFont(myFont);
textAlign(CENTER, CENTER);
//String[] words = {"apple", "microsoft", "samsung", "nokia", "sony"};
//String[] words = {"naruto","one%20piece","bleach","fairy tail","soul eater"};//{"apple", "microsoft", "samsung", "nokia", "sony"};
String [] words = {
  "Naruto",
  "Bleach", 
  "One Piece", 
  "Fairy Tail", 
  "Dengeki Daisy", 
  "Skip Beat", 
  "Vampire Knight", 
  "Black Bird", 
  "Soul Eater",// change to soul eater 
  "Rosario" 
 }; 
//colorMode(HSB, 360,100,100);// org
colorMode(RGB, 255,100,100);

int barSize = 80; //vertical spacing, org 100
String noofweeks = "2";
float startY = 80.0;

//String start = "20030101";
//String end = "20120101";//"20080101";
fill(360,0,60);
text("History of Manga Best-sellers vs Total Number of Manga Checkouts", width/2, 40);

for (int i = 0; i < words.length; i++) {
     int freq = getArticleKeywordCount( words[i]); //resturns total
     //noFill();
     fill(i*(lengthOfRainbow/words.length)+rainbowOffset, 100,100,90);
     stroke(i*(lengthOfRainbow/words.length)+rainbowOffset, 30,100,90);  
     //ellipse(width/2, startY + (barSize * i), sqrt(freq),sqrt(freq));
     ellipse(width/2, startY + (barSize * i), freq,freq);
     println(freq);
     
     
     //fill(i*(lengthOfRainbow/words.length)+rainbowOffset, 100,100,90); //org
     text(words[i], 100, startY + (barSize * i));
     //text(freq, width-100, startY + (barSize * i));// org
     text(freq, width/2+100, startY + (barSize * i));
     
     // Getting SPL Data
     println("reading "+words[i]+".txt"); // 
     String filestr = String.format("../query/%s.txt",words[i]);
     String [] textData = loadStrings(filestr); // loads all the strings line by line in the file
     int count1 = (int)Integer.parseInt(textData[1]); // year
     println("Count for " + words[i] +"="+ count1);
     
     // printing SPL
     fill(255,0,0);
     ellipse(width/2+400, startY +(barSize * i), sqrt(count1/10),sqrt(count1/10));
     
     
     /*int howManyCurves = freq;
     for(int j = 0; j < howManyCurves; j += 1) {
        noFill();
        stroke(i*(lengthOfRainbow/words.length)+rainbowOffset, 100,100,30); 
        bezier(100, startY + (barSize * i),  random(width), startY + (barSize * i) - 40, random(width),  startY + (barSize * i) - 40, 
        width/2+round(random(sqrt(freq))), startY + (barSize * i)+ round(random(sqrt(freq))));
     }*/
  }
}

void draw() {
//no draw loop.
// print copyright (bottomm right)
  textAlign(LEFT, BOTTOM);
  fill(0,0,255);
  text("Lakshman Nataraj | MAT259 Winter 2014", 0, 0, width-10, height-10);

};

int getArticleKeywordCount(String word) {
    word = word.replace(" ","%20");// replacing space by "%20"
    //String request = baseURL + "?query=" + word + "&begin_date=" + beginDate + "&end_date=" + endDate + "&api-key=" + apiKey;
    //String request = baseURL + "?=" + word + "&begin_date=" + beginDate + "&end_date=" + endDate + "&api-key=" + apiKey;
    String request = baseURL + "?title=" + word + "&api-key=" + apiKey;
    // this works http://api.nytimes.com/svc/books/v2/lists.xml?list=hardcover-fiction&weeks-on-list=2&api-key=26ed917d909ee9d8adae7a5139cfab35:5:68842405
    println(request);
    String result = join( loadStrings( request ), ""); // loads the json to string result
    
    int total = 0;
    
    try {
    JSONObject nytData = new JSONObject(join(loadStrings(request), "")); // initializing new json object
    JSONArray results = nytData.getJSONArray("results"); // getting the results from the "results" part of the json
    total = nytData.getInt("num_results"); //("total"); // getting the answer from field "total"
    println ("There were " + total + " occurences of the term " + word );
    }
    catch (JSONException e) {
    println ("There was an error parsing the JSONObject.");
    };
    
    return(total);
};
