using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace Ornek2020
{
    class OBJLoader
    {
        public List<Object> ObjectList;
        public List<Vertex> VertexList;
        public List<Normal> NormalList;
        public List<TexCoord> TexCoordList;

        public System.Windows.Forms.Label MessageBox;

        public string OBJName;

        private int ObjectNum;
        private int VertexNum;
        private int NormalNum;
        private int FaceNum;
        private int TexCoordNum;
        private int TotalVertices, TotalFaces;

        void AddObject(string oname)
        {
            ObjectNum++;
            ObjectList.Add(new Object(oname));

            MessageBox.Text = "Loading part-" + ObjectNum + " (" + oname + ")";
            // + " : " + size1 +" vertices, "+ size2 + " Facees.";
        }

        public void AddVertex()
        {
            VertexNum++;
            VertexList.Add(new Vertex());
        }

        public void AddNormal()
        {
            NormalNum++;
            NormalList.Add(new Normal());
        }

        public void AddTexCoord()
        {
            TexCoordNum++;
            TexCoordList.Add(new TexCoord());
        }

        public OBJLoader()
        {
            ObjectList = new List<Object>();
            VertexList = new List<Vertex>();
            NormalList = new List<Normal>();
            TexCoordList = new List<TexCoord>();

            ObjectNum = VertexNum = NormalNum = TexCoordNum = -1;
            TotalVertices = TotalFaces = 0;
        }

        public void LoadOBJ(string fileName)
        {
            string fileContent;
            string[] input;
            try
            {
                fileContent = File.ReadAllText(fileName);
                input = fileContent.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);
            }
            catch (FileNotFoundException fnf)
            {
                return;
            }

            OBJName = fileName;
            string item;
            int index;
            float xx, yy, zz;
            ObjectNum = -1;
            index = 0;
            while (index < input.Length-1)
            {
                item = input[++index];
                if (item == "object")
                {
                    // İsmiyle bir obje açılır.
                    AddObject(input[++index]);
                    TotalFaces += (FaceNum + 1);
                    FaceNum = -1;
                }
                else if (item == "usemtl")
                {
                    // İsmiyle bir obje açılır.
                    ObjectList[ObjectNum].Material = input[++index];
                }
                else if (item == "v")
                {
                    AddVertex();

                    item = input[++index];
                    xx = float.Parse(item, CultureInfo.InvariantCulture.NumberFormat);
                    VertexList[VertexNum].X = xx;
                    item = input[++index];
                    yy = float.Parse(item, CultureInfo.InvariantCulture.NumberFormat);
                    VertexList[VertexNum].Y = yy;
                    item = input[++index];
                    zz = float.Parse(item, CultureInfo.InvariantCulture.NumberFormat);
                    VertexList[VertexNum].Z = zz;
                }
                else if (item == "vn")
                {
                    AddNormal();

                    item = input[++index];
                    xx = float.Parse(item, CultureInfo.InvariantCulture.NumberFormat);
                    NormalList[NormalNum].Nx = xx;
                    item = input[++index];
                    yy = float.Parse(item, CultureInfo.InvariantCulture.NumberFormat);
                    NormalList[NormalNum].Ny = yy;
                    item = input[++index];
                    zz = float.Parse(item, CultureInfo.InvariantCulture.NumberFormat);
                    NormalList[NormalNum].Nz = zz;
                }
                else if (item == "vt")
                {
                    AddTexCoord();

                    item = input[++index];
                    xx = float.Parse(item, CultureInfo.InvariantCulture.NumberFormat);
                    TexCoordList[TexCoordNum].S = xx;
                    item = input[++index];
                    yy = float.Parse(item, CultureInfo.InvariantCulture.NumberFormat);
                    TexCoordList[TexCoordNum].T = yy;
                    item = input[++index];
                }
                else if (item == "f")
                {
                    string[] indexNum;

                    FaceNum++;
                    ObjectList[ObjectNum].AddFace();

                    item = input[++index];
                    indexNum = item.Split('/');
                    ObjectList[ObjectNum].FaceList[FaceNum].VertexIndex[0] = int.Parse(indexNum[0]) - 1;
                    ObjectList[ObjectNum].FaceList[FaceNum].TexCoordIndex[0] = int.Parse(indexNum[1]) - 1;
                    ObjectList[ObjectNum].FaceList[FaceNum].NormalIndex[0] = int.Parse(indexNum[2]) - 1;

                    item = input[++index];
                    indexNum = item.Split('/');
                    ObjectList[ObjectNum].FaceList[FaceNum].VertexIndex[1] = int.Parse(indexNum[0]) - 1;
                    ObjectList[ObjectNum].FaceList[FaceNum].TexCoordIndex[1] = int.Parse(indexNum[1]) - 1;
                    ObjectList[ObjectNum].FaceList[FaceNum].NormalIndex[1] = int.Parse(indexNum[2]) - 1;

                    item = input[++index];
                    indexNum = item.Split('/');
                    ObjectList[ObjectNum].FaceList[FaceNum].VertexIndex[2] = int.Parse(indexNum[0]) - 1;
                    ObjectList[ObjectNum].FaceList[FaceNum].TexCoordIndex[2] = int.Parse(indexNum[1]) - 1;
                    ObjectList[ObjectNum].FaceList[FaceNum].NormalIndex[2] = int.Parse(indexNum[2]) - 1;
                }
            } // while

            TotalVertices += (VertexNum + 1);
            TotalFaces += (FaceNum + 1);
            MessageBox.Text = "Total : "+ TotalVertices + " vertices, "+ TotalFaces + " Faces.";
        }
    }
}
