﻿using System;
using System.Text;
using BabylonHelper.FileStructure;
using Newtonsoft.Json;

namespace BabylonHelper
{
    class Program
    {
        private const string NewLine = "\n";
        static void Main(string[] args)
        {

            if (args.Length == 0)
            {
                Console.WriteLine("Please provide the .babylon filename as the first argument");
                Console.ReadLine();
                return;
            }
            string fileAsString = FileUtils.ReadFile(args[0]);
            Babylon babylonObj = JsonConvert.DeserializeObject<Babylon>(fileAsString);

            GetMeshUtils(babylonObj);

            GetUpdateUI(babylonObj);

            Console.ReadLine();
        }

        static void GetUpdateUI(Babylon babylonObj)
        {
            StringBuilder sbUpdateUI = new StringBuilder();
            sbUpdateUI.Append("/*All individual components that need highlighting should be registered here*/");
            sbUpdateUI.Append(NewLine);
            sbUpdateUI.Append("function UpdateUIComponents(scene){");
            sbUpdateUI.Append(NewLine);
            if (babylonObj != null)
            {
                foreach (Mesh m in babylonObj.meshes)
                {
                    Console.WriteLine(m.id);
                    sbUpdateUI.Append(MeshUtils.GetUpdateUIFunctionAsString(m.id));
                    sbUpdateUI.Append(NewLine);
                }
            }
            sbUpdateUI.Append(NewLine);
            sbUpdateUI.Append("}");

            FileUtils.SaveFile(@"Js\MeshUtil.UpdateUI.js", sbUpdateUI.ToString());
        }


        static void GetMeshUtils(Babylon babylonObj)
        {
            StringBuilder sbMeshUtils = new StringBuilder();

            if (babylonObj != null)
            {
                foreach (Mesh m in babylonObj.meshes)
                {
                    Console.WriteLine(m.id);
                    sbMeshUtils.Append(MeshUtils.GetJsFunctionAsString(m.id));
                    sbMeshUtils.Append(NewLine);
                }
            }

            FileUtils.SaveFile(@"js\MeshUtil.Helper.js", sbMeshUtils.ToString());
        }
    }
}
