#pragma once

#include "stdafx.h"

#include "Includes.h"

#include "Defines.h"

#include "Dialog.h"

#include "Dialog.cpp"

#include "TextFileDefineFunctions.cpp"

#include "GUI_DefineFunctions.cpp"

using namespace std;

int SaveArrayToDataFile()
{
	Debug << "SaveArrayToDataFile() fired" << '\n';
   ofstream Rules(txt_fname,ios::in|ios::out|ios::trunc);
   if (Rules.good()) {
		Rules << '"' << ver_info << '"' << '\n' << '\n';
      for (int i = 0;i < rec_t_cnt;i++) {
			Debug << "SaveArrayToDataFile() i loop " << i <<  '\n';
			if (ReturnViableSections(i)) {
				WriteLableRules(Rules,directive[i]);
				for (int j = 0; j < rec_count[i];j++) {
					Debug << "SaveArrayToDataFile() j loop " << j <<  '\n';
					for (int Step = 0;Step < step_max[i];Step++) {
						Debug << "SaveArrayToDataFile() Step loop " << Step <<  '\n';
						int Distance = GetSpaces(i,j,Step);
						if (Step == 0) {
							Rules << '"' << GetArrayValue(i,j,Step) << '"';
							for (unsigned int k = 0;k < (50 - strlen(GetArrayValue(i,j,Step)));k++) {
								Rules << ' ';
							}
						} else if (Step <= step_max[i] - 1) {
							for (unsigned int k = 0;k < (Distance - strlen(GetArrayValue(i,j,Step)));k++) {
								Rules << ' ';
							}  
							Rules << GetArrayValue(i,j,Step);
						}
						if (i == 31) Debug << "GetArrayValue(i,j,Step)*************************31" << GetArrayValue(i,j,Step) <<  '\n';
						if (Step == step_max[i] - 1) {
							Rules << ';' << '\n';
						} else {
							Rules << ',';
						}
					}
				}
				WriteLableRules(Rules,directive[rec_t_cnt]);
				Rules << '\n';
				m_progress->StepIt();
				wait ( 1 );
			}
      }
   } else {
		CallMessageBox("Load of ",txt_fname," Failed.");
   }

   Rules.close();

   return 0;
}