/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package plots;

import com.sun.j3d.utils.applet.MainFrame;
import java.awt.Frame;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 * 
 * @author Adam
 */
public class ParserFor3DPlots {
   
    //_axes is arranged as close, other, query
    
    private PlotPyramid _plotPyramidInstance;
    
    private ArrayList<ArrayList<Float>> _axes = new ArrayList<ArrayList<Float>>();
    
    private ArrayList<Float> _closeAxisX = new ArrayList<Float>();
    private ArrayList<Float> _closeAxisY = new ArrayList<Float>();
    private ArrayList<Float> _closeAxisZ = new ArrayList<Float>();
    
    private ArrayList<Float> _otherAxisX = new ArrayList<Float>();
    private ArrayList<Float> _otherAxisY = new ArrayList<Float>();
    private ArrayList<Float> _otherAxisZ = new ArrayList<Float>();
    
    private ArrayList<Float> _queryAxisX = new ArrayList<Float>();
    private ArrayList<Float> _queryAxisY = new ArrayList<Float>();
    private ArrayList<Float> _queryAxisZ = new ArrayList<Float>();
    
    
    private float _maxX, _maxY, _maxZ;
    private float _minX, _minY, _minZ;
    private float absMax;
   
    
    public ParserFor3DPlots()
    {
        parseText();
    }
            
    
    public void parseText()
    {
        try{

        FileReader closeRead = new FileReader("close.txt");
        FileReader otherRead = new FileReader("other.txt");
        FileReader queryRead = new FileReader("query.txt"); 
        Scanner closeScan  = new Scanner(closeRead);
        Scanner otherScan = new Scanner(otherRead);
        Scanner queryScan = new Scanner(queryRead);
  
        extractAxes(closeScan, "close"); //gets arraylist of all axes
        extractAxes(otherScan, "other");
        extractAxes(queryScan, "query");
        
        /*FIX
       _maxX = getMax(_closeAxisX); //gets max and min values of each axis
       _maxY = getMax(_closeAxisY);
       _maxZ = getMax(_closeAxisZ);
       
       _minX = getMin(_closeAxisX);
       _minY = getMin(_closeAxisY);
       _minZ = getMin(_closeAxisZ);
       
       _maxX = getMax(_otherAxisX); //gets max and min values of each axis
       _maxY = getMax(_otherAxisY);
       _maxZ = getMax(_otherAxisZ);
       
       _minX = getMin(_otherAxisX);
       _minY = getMin(_otherAxisY);
       _minZ = getMin(_otherAxisZ);
       
       _maxX = getMax(_queryAxisX); //gets max and min values of each axis
       _maxY = getMax(_queryAxisY);
       _maxZ = getMax(_queryAxisZ);
       
       _minX = getMin(_queryAxisX);
       _minY = getMin(_queryAxisY);
       _minZ = getMin(_queryAxisZ);
         **/
       
       float[] pointsToEval = {_maxX, _maxY, _maxZ, _minX, _minY, _minZ};
       
       absMax = getResizeValue(pointsToEval); //FIX!
       
       _axes.add(_closeAxisX);
       _axes.add(_otherAxisX);
       _axes.add(_queryAxisX);
       _axes.add(_closeAxisY);
       _axes.add(_otherAxisY);
       _axes.add(_queryAxisY);
       _axes.add(_closeAxisZ);
       _axes.add(_otherAxisZ);
       _axes.add(_queryAxisZ);
       
       System.out.println("The max abs value is: "+absMax);
 
       //Frame frame = new MainFrame(new PlotPyramid(_axes, absMax), 800, 800); //CHANGE
//       _plotPyramidInstance = new PlotPyramid(_axes, absMax);
      
        }catch(IOException e){
            //System.out.println("Problem reading files.");
            e.printStackTrace();
        }       
    }
    
    private void extractAxes(Scanner scan, String type)
    {
         while(scan.hasNext())
        {
             if(type.equals("close"))
             {
               _closeAxisX.add((float)scan.nextDouble());   //add all x's, y's and z's
               _closeAxisY.add((float)scan.nextDouble());
               _closeAxisZ.add((float)scan.nextDouble());
             
             }else if(type.equals("other"))
             {
               _otherAxisX.add((float)scan.nextDouble());
               _otherAxisY.add((float)scan.nextDouble());
               _otherAxisZ.add((float)scan.nextDouble());
               
             }else
             {
               _queryAxisX.add((float)scan.nextDouble());
               _queryAxisY.add((float)scan.nextDouble());
               _queryAxisZ.add((float)scan.nextDouble());
                 
             }
             
        }  
    }
    
    private float getMin(ArrayList<Float> axis)
    {
        float min = 0;
        
        for(int i = 0; i < axis.size(); i++)
        {
            if(i == 0){min = axis.get(i);}
            if(axis.get(i) < min)
            {
                min = axis.get(i);
            }
        }      
        return min;
    }
    
    private float getMax(ArrayList<Float> axis)
    {
         float max = 0;
        
        for(int i = 0; i < axis.size(); i++)
        {
            if(i == 0){max = axis.get(i);}
            if(axis.get(i) > max)
            {
                max = axis.get(i);
            }
        }
        return max;
    }
    
    private float getResizeValue(float[] axesValue)
    {
         float largestAxisPoint = 0;
        
        for(int i = 0; i < axesValue.length; i++)
        {
            if(i == 0){largestAxisPoint = Math.abs(axesValue[i]);}
            if(Math.abs(axesValue[i]) > largestAxisPoint)
            {
                largestAxisPoint = Math.abs(axesValue[i]);
            }
        }
        return largestAxisPoint;
        
    }
    
    private float getCubeMultiple(float min, float max)
    {
      return 2/(max-min);  
    }

    public PlotPyramid getPlotPyramidInstance() {
        return _plotPyramidInstance;
    }

    public void setPlotPyramidInstance(PlotPyramid plotPyramidInstance) {
        this._plotPyramidInstance = plotPyramidInstance;
    }

}
