/*
 * 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.util.ArrayList;

/**
 *
 * @author Adam
 */
public class Parse {
   
    //_axes is arranged as close, other, query
    
    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 Parse(ArrayList<ArrayList<PlotVal>> plotList)
    {
        parseText(plotList);
    }
            
    
    public void parseText(ArrayList<ArrayList<PlotVal>> plotList)
    {
        try{

        //as of now, plotlist's indices are considered: close, other, query
        
         extractAxes(plotList);
        
        /*FIXXXXX
       _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);
        
        
        }catch(Exception e){
            //System.out.println("Problem reading files.");
            e.printStackTrace();
        }       
    }

    
    private void addValToAxisList(PlotVal val)
    {
            if(val.getType().equalsIgnoreCase("close"))
                {
                    if(val.getAxis().equalsIgnoreCase("x"))
                    {
                         _closeAxisX.add((float)val.getValue());
                    }
                    else if(val.getAxis().equalsIgnoreCase("y"))
                    {   
                         _closeAxisY.add((float)val.getValue());
                    }
                    else if(val.getAxis().equalsIgnoreCase("z"))
                    {
                         _closeAxisZ.add((float)val.getValue());
                    }
                }
            else if(val.getType().equalsIgnoreCase("other"))
            {
                 if(val.getAxis().equalsIgnoreCase("x"))
                    {
                        _otherAxisX.add((float)val.getValue());
                    }
                    else if(val.getAxis().equalsIgnoreCase("y"))
                    {   
                        _otherAxisY.add((float)val.getValue());
                    }
                    else if(val.getAxis().equalsIgnoreCase("z"))
                    {
                        _otherAxisZ.add((float)val.getValue());
                    }
            }else if(val.getType().equalsIgnoreCase("query"))
            {
                if(val.getAxis().equalsIgnoreCase("x"))
                    {
                       _queryAxisX.add((float)val.getValue()); 
                    }
                    else if(val.getAxis().equalsIgnoreCase("y"))
                    {   
                       _queryAxisY.add((float)val.getValue());
                    }
                    else if(val.getAxis().equalsIgnoreCase("z"))
                    {
                       _queryAxisZ.add((float)val.getValue());
                    } 
            }
    }
    
    private void extractAxes(ArrayList<ArrayList<PlotVal>> plotList)
    {
         for(ArrayList<PlotVal> list : plotList)
        {
             for(PlotVal val : list)
             {
                addValToAxisList(val);
             }
        }  
    }
    
    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 static void main(String[] args)
    {
        ArrayList<ArrayList<PlotVal>> plotList = null;
        new Parse(plotList);
    }
}
