// 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;
using System.Windows.Media.Media3D;

namespace MEMS_Visualizer
{
    class ObjFIleReader
    {
        // load data in handModel.txt (file is in obj format)
        string file = MEMS_Visualizer.Properties.Resources.handModel;
        
        // collection of hand mesh vertices
        private Point3DCollection handVs = new Point3DCollection();
        public Point3DCollection HandVs
        {
            get
            {
                return handVs;
            }
        }

        // collection of hand mesh face indices
        private Int32Collection handFs = new Int32Collection();
        public Int32Collection HandFs
        {
            get
            {
                return handFs;
            }
        }

        // collection of thumb mesh vertices
        private Point3DCollection thumbVs = new Point3DCollection();
        public Point3DCollection ThumbVs
        {
            get
            {
                return thumbVs;
            }
        }

        // collection of thumb mesh faces
        private Int32Collection thumbFs = new Int32Collection();
        public Int32Collection ThumbFs
        {
            get
            {
                return thumbFs;
            }
        }

        public ObjFIleReader()
        {
            readFile();
        }

        // read and load data from file
        private void readFile()
        {
            // stringReader to iterate by lines through
            StringReader stringReader = new StringReader(file);
            string line;

            bool isThumb = false; // marker for whether data is from thumb or hand

            while ((line = stringReader.ReadLine()) != null)
            {
                // switch betwwen thumb and hand accordingly
                if (line.StartsWith("o Thumb"))
                    isThumb = true;
                if (line.StartsWith("o Hand"))
                    isThumb = false;

                // line is vertex
                if (line.StartsWith("v"))
                {
                    string[] values = line.Split(' ');
                    double x = Double.Parse(values[1]);
                    double y = Double.Parse(values[2]);
                    double z = Double.Parse(values[3]);

                    if (isThumb)
                        thumbVs.Add(new Point3D(x, y, z));
                    else
                        handVs.Add(new Point3D(x, y, z));
                }

                // line is face
                if (line.StartsWith("f"))
                {
                    string[] values = line.Split(' ');
                    int f1 = Int32.Parse(values[1]) - 1;
                    int f2 = Int32.Parse(values[2]) - 1;
                    int f3 = Int32.Parse(values[3]) - 1;

                    if (isThumb)
                    {
                        thumbFs.Add(f1);
                        thumbFs.Add(f2);
                        thumbFs.Add(f3);
                    }
                    else
                    {
                        // subtract 8 to correct index because there are 8 initial thumb vertices
                        handFs.Add(f1 - 8);
                        handFs.Add(f2 - 8);
                        handFs.Add(f3 - 8);
                    }
                }
            }
        }
    }
}
