#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;

int ReturnViableSections(int i)
{
	//if (i == 40) return 0;

	return 1;
}

int GetEditControlID(int i,int j,int Step)
{
	if (i == 0) return ItemPowersEditControlID[Step];
	if (i == 1) return BuildingsEditControlID[Step];
	if (i == 2) return SpellsEditControlID[Step];
	if (i >= 3 && i <= 19) return UnitsEditControlID[Step];
	if (i == 20) return RaceDataEditControlID[Step];
	if (i == 21) return EnchantmentEditControlID[Step];
	if (i == 22) return HeroSpecialEditControlID[Step];
	if (i == 26) return Wizard_Picks_EditControlID[j][Step];
	if (i == 27) return Item_Over_Books_EditControlID[Step];
	if (i == 28) return Minimum_City_Distance_EditControlID[Step];
	if (i == 29) return Retort_Effects_EditControlID[Step];
	if (i == 30) return Create_Spell_Charges_EditControlID[Step];
	if (i == 31) return Create_Base_Costs_EditControlID[j][Step];
	if (i == 32) return Create_Caps_EditControlID[Step];
	if (i == 33) return Destroy_Items_EditControlID[Step];
	if (i == 34) return Spells_Per_Book_EditControlID[Step];
	if (i == 35) return Random_Events_EditControlID[Step];
	if (i == 36) return Map_Generation_EditControlID[j][Step];
	if (i == 37) return Research_From_Books_EditControlID[j][Step];
	if (i == 38) return Hero_Join_EditControlID[j][Step];
	if (i == 39) return Merc_Join_EditControlID[j][Step];
	if (i == 41) return Misc_EditControlID[j][Step];
	if (i == 42) return Food_Bonuses_EditControlID[j][Step];

   return 0;
}

int GetHelpControlID(int i,int Step)
{
	if (i == 0) return ItemPowersHelpControlID[Step];
	if (i == 1) return BuildingsHelpControlID[Step];
	if (i == 2) return SpellsHelpControlID[Step];
	if (i >= 3 && i <= 19) return UnitsHelpControlID[Step];
	if (i == 20) return RaceDataHelpControlID[Step];
	if (i == 21) return EnchantmentHelpControlID[Step];
	if (i == 22) return HeroSpecialHelpControlID[Step];
	if (i == 26) Wizard_Picks_HelpControlID[Step];
	if (i == 27) return Item_Over_Books_HelpControlID[Step];
	if (i == 28) return Minimum_City_Distance_HelpControlID[Step];
	if (i == 29) return Retort_Effects_HelpControlID[Step];
	if (i == 30) return Create_Spell_Charges_HelpControlID[Step];
	if (i == 31) return Create_Base_Costs_HelpControlID[Step];
	if (i == 32) return Create_Caps_HelpControlID[Step];
	if (i == 33) return Destroy_Items_HelpControlID[Step];
	if (i == 34) return Spells_Per_Book_HelpControlID[Step];
	if (i == 35) return Random_Events_HelpControlID[Step];
	if (i == 36) return Map_Generation_HelpControlID[Step];
	if (i == 37) return Research_From_Books_HelpControlID[Step];
	if (i == 38) return Hero_Join_HelpControlID[Step];
	if (i == 39) return Merc_Join_HelpControlID[Step];
	if (i == 41) return Misc_HelpControlID[Step];
	if (i == 42) return Food_Bonuses_HelpControlID[Step];

   return 0;
}

char* GetControlHelpNames(int i,int Step)
{
	if (i == 0) return ItemPowersHelp[Step];
	if (i == 1) return BuildingsHelp[Step];
	if (i == 2) return SpellsHelp[Step];
	if (i >= 3 && i <= 19) return UnitsHelp[Step];
	if (i == 20) return RaceDataHelp[Step];
	if (i == 21) return EnchantmentHelp[Step];
	if (i == 22) return HeroSpecialHelp[Step];
	if (i == 26) return Wizard_Picks_Help[Step];
	if (i == 27) return Item_Over_Books_Help[Step];
	if (i == 28) return Minimum_City_Distance_Help[Step];
	if (i == 29) return Retort_Effects_Help[Step];
	if (i == 30) return Create_Spell_Charges_Help[Step];
	if (i == 31) return Create_Base_Costs_Help[Step];
	if (i == 32) return Create_Caps_Help[Step];
	if (i == 33) return Destroy_Items_Help[Step];
	if (i == 34) return Spells_Per_Book_Help[Step];
	if (i == 35) return Random_Events_Help[Step];
	if (i == 36) return Map_Generation_Help[Step];
	if (i == 37) return Research_From_Books_Help[Step];
	if (i == 38) return Hero_Join_Help[Step];
	if (i == 39) return Merc_Join_Help[Step];
	if (i == 41) return Misc_Help[Step];
	if (i == 42) return Food_Bonuses_Help[Step];

	return "Error";
}

int GetColumnWidth(int i,int Step)
{
	Debug << "GetColumnWidth i - Step " << i << " - " << Step << '\n';
	string Registry_Key = "List Control Settings\\";
	Registry_Key.append(directive[i]);
	Registry_Key.append("\\");
	Registry_Key.append(Int2Str(Step));
	Registry_Key.append("\\");

	Debug << " 1 " << '\n';
	int Width = AfxGetApp()->GetProfileInt(Registry_Key.c_str(),Int2CharArray(Step),-1);
	Debug << " 2 " << '\n';
	return Width;
}

int SaveColumnWidth(int i,int Step,int Width)
{
	Debug << "SaveColumnWidth Name" << '\n';
	string Registry_Key = "List Control Settings\\";
	Registry_Key.append(directive[i]);
	Registry_Key.append("\\");
	Registry_Key.append(Int2Str(Step));
	Registry_Key.append("\\");

	AfxGetApp()->WriteProfileInt(Registry_Key.c_str(),Int2CharArray(Step),Width);

	return 0;
}

char* GetArrayValue(int i,int j, int Step)
{
	return MoMEditorArray[i][j][Step];
}


int LoadArrayTable(HWND hDialog,int ControlID,int i,int Max_Entries = 0)
{
	Debug << "HWND LoadArrayTable(HWND hDialog,int ControlID,int i)" << '\n';
	SendDlgItemMessage(hDialog,ControlID,LVM_DELETEALLITEMS,(WPARAM)0,(LPARAM)0);
	for(int l = step_max[i]; l >= 0;l--) {
		SendDlgItemMessage(hDialog,ControlID,LVM_DELETECOLUMN,(WPARAM)l,(LPARAM)0);
	}

	LvItem.mask=LVIF_TEXT | LVIF_COLUMNS;   
	LvItem.iItem;          
	LvItem.iSubItem=0;       
	LvItem.state=NULL;
	LvItem.stateMask;
   LvItem.pszText;                                
   LvItem.cchTextMax=255;
	LvItem.iImage=NULL;
	LvItem.lParam=NULL;
	LvItem.iIndent=NULL;
	LvItem.iGroupId=NULL;
	LvItem.cColumns = step_max[i];
	LvItem.puColumns=NULL;
	LvItem.piColFmt=NULL;
	LvItem.iGroup=NULL;
	LvCol.mask=LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
	LvCol.fmt=NULL;
	LvCol.cx=0xB4;                                
	LvCol.pszText; 
	LvCol.cchTextMax; 
	LvCol.iSubItem=0;
	LvCol.iImage=NULL;
	LvCol.iOrder=NULL;
	LvCol.cxMin=NULL;
	LvCol.cxDefault=NULL;
	LvCol.cxIdeal=NULL;

	if (Max_Entries > 0) {
		for(int j = 0;j < rec_count[i];j++) 
		{ 
			for(int Step = 0;Step < Max_Entries;Step++) 
			{  

				if (j == 0) {
					LvCol.iSubItem=j;
					if (Step == 0) LvCol.pszText= "Spell Name";
					if (Step == 0) LvCol.cchTextMax=strlen("Spell Name") + 1;
					if (Step == 1) LvCol.pszText= "Spell ID";
					if (Step == 1) LvCol.cchTextMax=strlen("Spell ID") + 1;
					LvCol.cx=LvCol.cchTextMax * 15;
					SendDlgItemMessage(hDialog,ControlID,LVM_INSERTCOLUMN,(WPARAM)Step,(LPARAM)&LvCol);
				} 
				LvItem.iItem=j + 1;
				LvItem.iSubItem=Step;
				LvItem.cColumns = step_max[i];
				if (Step == 0) {
					LvItem.pszText=GetArrayValue(i,j,Step);
					SendDlgItemMessage(hDialog,ControlID,LVM_INSERTITEM,(WPARAM)0,(LPARAM)&LvItem);
				} else {
					LPSTR ID = Int2CharArray(j - 1);
					LvItem.pszText=ID;
					SendDlgItemMessage(hDialog,ControlID,LVM_SETITEMTEXT,(WPARAM)j,(LPARAM)&LvItem);
				}
			}
			Debug << "LoadArrayTable j " << j << '\n';
		}	
	} else {
		for(int j = 0;j < rec_count[i];j++) 
		{ 
			for(int Step = 0;Step < step_max[i];Step++) 
			{  

				if (j == 0) {
					LvCol.iSubItem=j;
					LvCol.pszText=GetControlHelpNames(i,Step);                                
					LvCol.cchTextMax=strlen(GetControlHelpNames(i,Step)) + 1;
					if (GetColumnWidth(i,Step) == -1) {
						LvCol.cx=LvCol.cchTextMax * 10;
					} else {
						LvCol.cx=GetColumnWidth(i,Step);
					}
					SendDlgItemMessage(hDialog,ControlID,LVM_INSERTCOLUMN,(WPARAM)Step,(LPARAM)&LvCol);
				} 
				LvItem.iItem=j + 1;
				LvItem.iSubItem=Step;
				LvItem.pszText=GetArrayValue(i,j,Step);
				LvItem.cColumns = step_max[i];
				if (Step == 0) {
					SendDlgItemMessage(hDialog,ControlID,LVM_INSERTITEM,(WPARAM)0,(LPARAM)&LvItem);
				} else {
					SendDlgItemMessage(hDialog,ControlID,LVM_SETITEMTEXT,(WPARAM)j,(LPARAM)&LvItem);
				}
			}
			Debug << "LoadArrayTable j " << j << '\n';
		}	
	}
	SendDlgItemMessage(hDialog,ControlID,LVM_SETEXTENDEDLISTVIEWSTYLE,0,LVS_EX_FULLROWSELECT);

	memset(&LvItem,0,sizeof(LvItem));
	memset(&LvCol,0,sizeof(LvCol)); 

	return 0;
}

void SetArrayValue(int i,int j, int Step,char* Value)
{
	MoMEditorArray[i][j][Step] = CopyCharArray(Value);
}

int SaveArrayTable(HWND hDialog,int ControlID,int i)
{
	Debug << "SaveArrayTable " << '\n';
	char Text[255]={0};

	LvItem.mask=LVIF_TEXT;
	LvItem.iSubItem=0;
	LvItem.pszText=Text;
	LvItem.cchTextMax=256;
                  
	for(int j = 0;j < rec_count[i];j++) 
	{ 
		for(int Step = 0;Step < step_max[i];Step++) 
		{ 
			int Width = SendDlgItemMessage(hDialog,ControlID,LVM_GETCOLUMNWIDTH, (WPARAM)(int)Step, (LPARAM)0);
			SaveColumnWidth(i,Step,Width);
			LvItem.iItem=j;
			LvItem.iSubItem=Step;			
			SendDlgItemMessage(hDialog,ControlID,LVM_GETITEMTEXT, j, (LPARAM)&LvItem);
			SetArrayValue(i,j,Step,Str2CharArray(Text));
		}
	}
	memset(&LvItem,0,sizeof(LvItem));
	memset(&LvCol,0,sizeof(LvCol)); 

	return 1;
}

void InitializeArray()
{
	for (int i = 0;i < rec_t_cnt - 1;i++) {
		if (ReturnViableSections(i)) {
			for (int j = 0;j < rec_count[i];j++) {
				for (int Step = 0;Step < step_max[i];Step++) {
					MoMEditorArray[i][j][Step] = CopyCharArray("Not Set");
				}
			}
		}
	}
}