/***
 * The Example is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Example is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Neuroph. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neuroph.contrib.samples.stockmarket;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.learning.SupervisedTrainingElement;
import org.neuroph.core.learning.TrainingElement;
import org.neuroph.core.learning.TrainingSet;
import org.neuroph.nnet.MultiLayerPerceptron;
import org.neuroph.nnet.learning.LMS;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.HttpURLConnection;

/**
 *
 * @author Dr.V.Steinhauer
 */
public class Main {

    public static void main(String[] args) {
        System.out.println("Time stamp N1:" + new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss:MM").format(new Date()));

        int maxIterations = 10000;
        NeuralNetwork neuralNet = new MultiLayerPerceptron(4, 9, 1);
        ((LMS) neuralNet.getLearningRule()).setMaxError(0.001);//0-1
        ((LMS) neuralNet.getLearningRule()).setLearningRate(0.7);//0-1
        ((LMS) neuralNet.getLearningRule()).setMaxIterations(maxIterations);//0-1
        TrainingSet trainingSet = new TrainingSet();
        TextReader tr= new TextReader();
        double daxmax = 1000.0D;
        /*The Following Takes in 4 days worth of data and makes a neural network in respect to the 5th day.
         * This happens for a 90 day period
         */
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[0] / daxmax, tr.pastvals[1] / daxmax, tr.pastvals[2] / daxmax, tr.pastvals[3] / daxmax}, new double[]{tr.pastvals[4] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[5] / daxmax, tr.pastvals[6] / daxmax, tr.pastvals[7] / daxmax, tr.pastvals[8] / daxmax}, new double[]{tr.pastvals[9] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[10] / daxmax, tr.pastvals[11] / daxmax, tr.pastvals[12] / daxmax, tr.pastvals[13] / daxmax}, new double[]{tr.pastvals[14] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[15] / daxmax, tr.pastvals[16] / daxmax, tr.pastvals[17] / daxmax, tr.pastvals[18] / daxmax}, new double[]{tr.pastvals[19] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[20] / daxmax, tr.pastvals[21] / daxmax, tr.pastvals[22] / daxmax, tr.pastvals[23] / daxmax}, new double[]{tr.pastvals[24] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[25] / daxmax, tr.pastvals[26] / daxmax, tr.pastvals[27] / daxmax, tr.pastvals[28] / daxmax}, new double[]{tr.pastvals[29] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[30] / daxmax, tr.pastvals[31] / daxmax, tr.pastvals[32] / daxmax, tr.pastvals[33] / daxmax}, new double[]{tr.pastvals[34] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[35] / daxmax, tr.pastvals[36] / daxmax, tr.pastvals[37] / daxmax, tr.pastvals[38] / daxmax}, new double[]{tr.pastvals[39] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[40] / daxmax, tr.pastvals[41] / daxmax, tr.pastvals[42] / daxmax, tr.pastvals[43] / daxmax}, new double[]{tr.pastvals[44] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[45] / daxmax, tr.pastvals[46] / daxmax, tr.pastvals[47] / daxmax, tr.pastvals[48] / daxmax}, new double[]{tr.pastvals[49] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[50] / daxmax, tr.pastvals[51] / daxmax, tr.pastvals[52] / daxmax, tr.pastvals[53] / daxmax}, new double[]{tr.pastvals[54] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[55] / daxmax, tr.pastvals[56] / daxmax, tr.pastvals[57] / daxmax, tr.pastvals[58] / daxmax}, new double[]{tr.pastvals[59] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[60] / daxmax, tr.pastvals[61] / daxmax, tr.pastvals[62] / daxmax, tr.pastvals[63] / daxmax}, new double[]{tr.pastvals[64] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[65] / daxmax, tr.pastvals[66] / daxmax, tr.pastvals[67] / daxmax, tr.pastvals[68] / daxmax}, new double[]{tr.pastvals[69] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[70] / daxmax, tr.pastvals[71] / daxmax, tr.pastvals[72] / daxmax, tr.pastvals[73] / daxmax}, new double[]{tr.pastvals[74] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[75] / daxmax, tr.pastvals[76] / daxmax, tr.pastvals[77] / daxmax, tr.pastvals[78] / daxmax}, new double[]{tr.pastvals[79] / daxmax}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{tr.pastvals[80] / daxmax, tr.pastvals[81] / daxmax, tr.pastvals[82] / daxmax, tr.pastvals[83] / daxmax}, new double[]{tr.pastvals[84] / daxmax}));
        neuralNet.learnInSameThread(trainingSet);
        System.out.println("Time stamp N2:" + new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss:MM").format(new Date()));
               double points=Articlepinger(); 
            points=points*.001;
        TrainingSet testSet = new TrainingSet();
        testSet.addElement(new TrainingElement(new double[]{tr.pastvals[85] / daxmax, tr.pastvals[86] / daxmax, tr.pastvals[87] / daxmax, tr.pastvals[88] / daxmax}));
     

        for (TrainingElement testElement : testSet.trainingElements()) {
            neuralNet.setInput(testElement.getInput());
            neuralNet.calculate();
            Vector<Double> networkOutput = neuralNet.getOutput();
            System.out.print("Input: " + testElement.getInput()); //The input for the four days
            double outnum =networkOutput.get(0)+points;
             System.out.println(" Output: " + outnum);  //The output of the fifth day

        }

        //Experiments:
        //                   calculated
        //31;3;2009;4084,76 -> 4121 Error=0.01 Rate=0.7 Iterat=100
        //31;3;2009;4084,76 -> 4096 Error=0.01 Rate=0.7 Iterat=1000
        //31;3;2009;4084,76 -> 4093 Error=0.01 Rate=0.7 Iterat=10000
        //31;3;2009;4084,76 -> 4108 Error=0.01 Rate=0.7 Iterat=100000
        //31;3;2009;4084,76 -> 4084 Error=0.001 Rate=0.7 Iterat=10000

        System.out.println("Time stamp N3:" + new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss:MM").format(new Date()));
        
        System.exit(0);
    }
    
    public static double Articlepinger(){
    String stockticker="GOOG";
		String url= "http://api.nytimes.com/svc/search/v2/articlesearch.json?q="+stockticker+"&api-key=ee261058272fbacfa18eceed8f929887:16:67655074";
		URL APIurl=null;
		try {
			APIurl = new URL(url);
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		HttpURLConnection request=null;
		try {
			request = (HttpURLConnection) APIurl.openConnection();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			request.setRequestMethod("GET");
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BufferedReader br=null;
		try {
			br= new BufferedReader(new InputStreamReader(request.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String line=null;
		StringBuilder response=new StringBuilder();
		try {
			while((line=br.readLine()) != null){
				response.append(line + "\n");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(response.toString());
		
		String[] out = new String[500];
		if (response.toString().contains(":")){
			out = response.toString().split(":");
		}
		else {
			throw new IllegalArgumentException("Error!");
		}
		int i;
		for(i=0;i<out.length;i++){
		//System.out.println(out[i]);
		}
		//System.out.println(out.length);
		int val;
		val = Integer.parseInt(out[out.length - 3].substring(0,3));
		double point;
		point = (int) (val/100);
		System.out.println(point);
		return point;
    }
    
   
}
