// Joseph Garcia

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;

namespace MEMS_Visualizer
{
    public class FileLoader
    {
        private String fileName;
        private MovementData movementData { get; set; }

        public FileLoader(String fileName, MovementData movementData)
        {
            this.fileName = fileName;
            this.movementData = movementData;

            loadData();
        }

        // loads data from file "fileName" into movementData
        private void loadData()
        {
            bool vectorProduct = false;
            foreach(String line in File.ReadAllLines(fileName))
            {
                if (line.Contains("Vector Product"))
                {
                    vectorProduct = true;
                    continue;
                }
                    

                double[] rotations;
                if (vectorProduct)
                    rotations = new double[13];
                else
                    rotations = new double[7];

                int i = 0;
                foreach (String value in line.Split(new char[] { ',', ' ' }))
                {
                    if (value.Length > 0)
                    {
                        double n = 0;
                        if (double.TryParse(value, out n))
                        {
                            rotations[i] = Convert.ToDouble(value);
                            i++;
                        }   
                    }
                }
                if (vectorProduct)
                    movementData.addRotation(new Vector3D(rotations[0], rotations[1], rotations[2]), 
                        new Vector3D(rotations[3], rotations[4], rotations[5]), 
                        new Vector3D(rotations[6], rotations[7], rotations[8]), 
                        new Point3D(rotations[9], rotations[10], rotations[11]), 
                        rotations[12]);
                else
                    movementData.addRotation(new Vector3D(rotations[0], rotations[1], rotations[2]), 
                        new Vector3D(rotations[3], rotations[4], rotations[5]), 
                        rotations[6]);
            }
        }
    }
}
