#pragma once

#include "stdafx.h"

#include "C:\Game Files\Fallout 2\Scripting\sFall\Compile\OCL Common Includes\BringUpUtilities.cpp"

#include "C:\Game Files\Fallout 2\Scripting\sFall\Compile\OCL Common Includes\Utilities.cpp"

#include "BringUp.h"

#include "Defines.h"

#include "Includes.h"

using namespace std;

string GetTableValues(int i,int j,int Step,fstream& Rules)
{
   string Value = "";
   int TwoBytes = GetByteParameters(i,j,Step);
   int TotalInt = 0;
   size_t loc1 = 0;
   size_t loc2 = 0;
   size_t loc5 = 0;
   int NegativeNumber = 0;
   size_t Distance = GetSpaces(i,j,Step);
	Distance = Distance + 3;
   int NameStep = 0;
   if (Step == 0 && j == 0) {
      while ( getline(Rules, Line) ) {
         LineNumber++;
		   loc1 = Line.find(directive[i]);
	      if (loc1!=string::npos) {
            Debug << " Found Dirrective " << directive[i] << '\n';
            getline(Rules, Line);
            LineNumber++;
            loc4 = 0;
            break;
         }
         if(LineNumber > 100000) {
				CallMessageBox("Line Number : ",Int2Str(LineNumber),". Unexpected end of file.");
         }
      } 
   } else if (Step == 0 && j > 0) {
      getline(Rules, Line);
      LineNumber++;
      loc4 = 0;
      Debug << " bumped line in " << directive[i] << '\n';
      Debug << " Line " << Line << '\n';
      Debug << " LineNumber " << LineNumber << '\n';
   } 
   Debug << " Step " << Step << " i " << i << '\n';
	if (Step ==  0) {
      loc4 = 0;
      loc1 = Line.find('"');
      if (loc1!=string::npos) {
         loc2 = Line.find('"',loc1 + 1);
         if (loc2!=string::npos) {
            Value = Line.substr(loc1+1,loc2 - loc1 - 1);
            Debug << " added name in  " << directive[i] << " Line -> " << Line << '\n';
            loc4 = loc2;
            return Value;
         }
      }
   } else {
	   loc2 = Line.find(',',loc4);
      if (loc2!=string::npos) {
         Debug << " 1st Comma Found in  " << directive[i] << " Step -> " << Step << " j -> " << j << " i -> " << i << " Line.length() -> " << Line.length() << " loc2 -> " << loc2 << '\n';
         for (size_t l = 0; l < (loc2 + Distance); l++) {
            int One = 0;
            if ((IsCharInt(char(Line.at(l))) || (char(Line.at(l)) == '-')) && (l > loc2) && (One == 0)) {
               One = 1;
               Debug << " Found first number digit or negative sign at " << l << '\n';
               Debug << " Line " << Line << '\n';
               Debug << " LineNumber " << LineNumber << '\n';
               if ((Step < step_max[i] - 1)) {
                  loc5 = Line.find_first_of(',',l);
                  if (loc5!=string::npos) {
                     if (loc5 - l < 16 && TwoBytes == 2) {
                        Debug << " binary expected in  " << directive[i] << " Line -> " << Line << " Step -> " << Step << '\n';

                     }
                     Value = Line.substr(l,loc5 - l);
                     loc4 = l;
                     Debug << " Value " << Value << " Step " << Step << '\n';
                     return Value;
                  } else {
                     Debug <<  " Line -> " << Line << '\n';
                     Debug << " 2nd Comma Not Found in  " << directive[i] << " Step -> " << Step << " j -> " << j << " i -> " << i << " Line.length() -> " << Line.length() << '\n';
							CallMessageBox("Line Number : ",Int2Str(LineNumber),". 2nd Comma Not Found.");
                  }
               } else if ((Step == step_max[i] - 1)) {
                  loc5 = Line.find_first_of(';',l);
                  if (loc5!=string::npos) {
                     if (loc5 - l < 16 && TwoBytes == 2) {
                        Debug << " binary expected in  " << directive[i] << " Line -> " << Line << " Step -> " << Step << '\n';
								CallMessageBox("Line Number : ",Int2Str(LineNumber),". Binary string expected.");
                     }
                     Value = Line.substr(l,loc5 - l);
                     loc4 = 0;
                     Debug << " Value last step " << Value << '\n';
                     return Value;
                  } else {
                     Debug << " Semi-colon Not Found in  " << directive[i] << " Line -> " << Line << " Step -> " << Step << '\n';
							CallMessageBox("Line Number : ",Int2Str(LineNumber),". SemiColon Not Found.");
                  }
               }
            }
         }
         if (Value == "" ) {
            Debug << " Number Value Empty in  " << directive[i] << " Line -> " << Line << " Step -> " << Step << '\n';
				CallMessageBox("Line Number : ",Int2Str(LineNumber),". Number Value Empty.");
         } 
      } else {
         Debug <<  " Line -> " << Line << '\n';
         Debug << " 1st Comma Not Found in  " << directive[i] << " Step -> " << Step << " j -> " << j << " i -> " << i << '\n';
			CallMessageBox("Line Number : ",Int2Str(LineNumber),". 1st Comma Not Found.");
      }
   }
   return "Value Not Found";
}

void LoadDataFileToArray(int i,int j,int Step,fstream& Rules)
{
   Debug << "LoadDataFileToArray  i-j-Step - " << i << " - " << j << " - " << Step << '\n';
	string Value = GetTableValues(i,j,Step,Rules);
	SetArrayValue(i,j,Step,Str2CharArray(Value));
}

int LoadDataFileToArray_Step(int i,int j,fstream& Rules)
{
   Debug << "LoadDataFileToArray_Step ---- j ---> " << j <<  '\n';
   for (int Step = 0; Step < step_max[i];Step++) {
      LoadDataFileToArray(i,j,Step,Rules);
   }
   return 0;
}

int LoadDataFileToArray_j(int i,fstream& Rules)
{
	Debug << "LoadDataFileToArray_j i-> " << i << '\n';
   for (int j = 0; j < rec_count[i];j++) {
		LoadDataFileToArray_Step(i,j,Rules);
   }
   return 0;
}

int LoadDataFileToArray_i()
{
   LineNumber = 1;
	Debug << "LoadDataFileToArray_i" << '\n';
   fstream Rules(txt_fname,ios::binary|ios::in|ios::out);
   if (Rules.good()) {
      for (int i = 0;i < rec_t_cnt;i++) {
			if (ReturnViableSections(i)) {
				LoadDataFileToArray_j(i,Rules);
				m_progress->StepIt();
				wait ( 1 );
			}
      }
   } else {
		CallMessageBox("Load of ",txt_fname," Failed.");
   }
	Debug.close();

   return 0;
}


