/* Why the fuck is this called the item editor when it doesnt have anything to do with items it should be npc */
#include <fstream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <allegro.h>
#include "gstream.h"


BITMAP *vscreen = NULL;

class CNpcInfo
{
public:
       CNpcInfo();
       int id;
       std::string name;
       std::string text;
       
       std::string yesno;
       
       std::string yestext;
       std::string notext;
       
       int quest_idnum;
       int set_quest_idnum;
       std::string quest_text;
};

CNpcInfo::CNpcInfo()
{
  id = 1;
  name = "Not set";
  text = "0";
  yesno = "none";
  yestext = "0";
  notext = "0";

  quest_idnum = 0;
  set_quest_idnum = 0;
  quest_text = "0";
}


std::vector<CNpcInfo> npcinfo;

void SaveNpcs()
{
	std::ofstream fout;
	fout.open("./data/npcs.txt");
	char temp[1024];
	
	fout << npcinfo[0].id << std::endl;
	fout << npcinfo[0].name << std::endl;
	fout << npcinfo[0].text << std::endl;
	fout << npcinfo[0].yesno << std::endl;
	fout << npcinfo[0].yestext << std::endl;
	fout << npcinfo[0].notext << std::endl;
	fout << npcinfo[0].quest_idnum << std::endl;
	fout << npcinfo[0].set_quest_idnum << std::endl;
	fout << npcinfo[0].quest_text;
	
	int counter = 1;
	while(counter < npcinfo.size()-1)
	{
		fout << std::endl;
		fout << npcinfo[counter].id << std::endl;
		fout << npcinfo[counter].name << std::endl;
		fout << npcinfo[counter].text << std::endl;
		fout << npcinfo[counter].yesno << std::endl;
		fout << npcinfo[counter].yestext << std::endl;
		fout << npcinfo[counter].notext << std::endl;
		fout << npcinfo[counter].quest_idnum << std::endl;
		fout << npcinfo[counter].set_quest_idnum << std::endl;
		fout << npcinfo[counter].quest_text;
		counter++;
	}
}

void LoadNpcs()
{
    std::ifstream fin;
    fin.open("./data/npcs.txt");
    char temp[1024];
    
    npcinfo.resize(1);

    int counter = 0;
    while(!fin.eof())
    {
     for(int i = 0; i < 1024; ++i)
               temp[i] = ' ';
     fin.getline( temp, 255);
     npcinfo[counter].id = atol(temp);

     for(int i = 0; i < 1024; ++i)
             temp[i] = ' ';
     fin.getline( temp, 255);
       npcinfo[counter].name = temp;

     for(int i = 0; i < 1024; ++i)
             temp[i] = ' ';
     fin.getline( temp, 1024);
        npcinfo[counter].text = temp;

     for(int i = 0; i < 1024; ++i)
             temp[i] = ' ';
     fin.getline( temp, 1024);
        npcinfo[counter].yesno = temp;

     for(int i = 0; i < 1024; ++i)
             temp[i] = ' ';
     fin.getline( temp, 1024);
        npcinfo[counter].yestext = temp;
        
     for(int i = 0; i < 1024; ++i)
             temp[i] = ' ';
     fin.getline( temp, 1024);
        npcinfo[counter].notext = temp;

     for(int i = 0; i < 1024; ++i)
             temp[i] = ' ';
     fin.getline( temp, 1024);
        npcinfo[counter].quest_idnum = atol(temp);
        
     for(int i = 0; i < 1024; ++i)
             temp[i] = ' ';
     fin.getline( temp, 1024);
        npcinfo[counter].set_quest_idnum = atol(temp);

     for(int i = 0; i < 1024; ++i)
             temp[i] = ' ';
     fin.getline( temp, 1024);
        npcinfo[counter].quest_text = temp;
        
        
     
     npcinfo.resize(npcinfo.size()+1);
     ++counter;
    }
    fin.close();
}










class CAppEditor
{
public:
	CAppEditor();
	~CAppEditor();
	int Init();
	int done;
	int SetDepth(int);
	void Shutdown();
	void SetTimers();
private:
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//             Timing Routines
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
volatile int speed_counter = 0;

void increment_speed_counter()
{
    speed_counter++;
}
END_OF_FUNCTION(increment_speed_counter);

volatile int frame_count, fps  = 0;

void fps_proc(void)
{
   fps = frame_count;
   frame_count = 0;
}
END_OF_FUNCTION(fps_proc);

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                          CAppEditor
////////////////////////////////////////////////////////////////////////////////////////////////////////////
CAppEditor AppEditor;

//Callback to exit
void exit_callback()
{
AppEditor.done = true;
}

///////////////////////////////////////
//              Constructor
///////////////////////////////////////
CAppEditor::CAppEditor(void)
{
	done = false;
}


///////////////////////////////////////
//              Destructor
///////////////////////////////////////
CAppEditor::~CAppEditor()
{

}


///////////////////////////////////////
//              CAppEditor::Init
///////////////////////////////////////
int CAppEditor::Init(void)
{
	allegro_init();
	install_keyboard();
	install_timer();
	install_mouse();
	set_color_depth(16);


	if (1)
	{
		set_gfx_mode(GFX_DIRECTX_WIN, 1024, 768, 0, 0);
		set_close_button_callback(exit_callback);
	}
	
	else 
	{
		set_gfx_mode(GFX_AUTODETECT, 1024, 768, 0, 0);
	}
	
	
	SetTimers();

	set_window_title("Item editor nir.gwgaming.net"); 


	vscreen = create_bitmap(1024, 768);
return 0;
}



///////////////////////////////////////
//              CAppEditor::SetDepth
// doesn't work
///////////////////////////////////////
int CAppEditor::SetDepth(int bpp)
{
	set_color_depth(bpp);
	return 1;
}

///////////////////////////////////////
//              Shutdown - Important
///////////////////////////////////////
void CAppEditor::Shutdown(void)
{
//Destroy anything... bitmaps etc..
allegro_exit();
}


void CAppEditor::SetTimers(void)
{
	LOCK_VARIABLE(speed_counter);
	LOCK_FUNCTION(increment_speed_counter);
	install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));

	LOCK_VARIABLE (frame_count);
	LOCK_VARIABLE (fps);       // Lock FPS
	LOCK_FUNCTION (fps_proc);  // Lock FPScounter
	install_int(fps_proc, 1000);
	frame_count = fps = 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                          MAIN
////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
	AppEditor.Init();
    BITMAP *vscreen = create_bitmap(SCREEN_W, SCREEN_H);
	
	int cur_id = 0;
	char temp[1024];
	
	LoadNpcs();

do
{
while (speed_counter > 0) 
 {
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                          Logic
////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if(key[KEY_ESC]) AppEditor.done = true;
  
  if(key[KEY_UP]) 
	if(cur_id < npcinfo.size()-2) // account that the npcinfo id starts at 1
	{
		cur_id ++;
		while(key[KEY_UP]) {}
	}
  
  if(key[KEY_DOWN])
	if(cur_id > 0)
	{
		cur_id --;
		while(key[KEY_DOWN]) {}
	}
	
	
if(key[KEY_HOME]) 
	cur_id = npcinfo.size()-2;

	if(key[KEY_N])
	{
		 clear_keybuf();
		 clear(screen);
         gstream gs(screen);
         gs.save_input_approver();
    	 gs.set_input_approver(ia_allow_everything);
         gs.set_insert_mode(true);
         gs.set_max_input_length(1024);
         gs.goto_xy(0,0);
         gs << "Name?: ";
		 
		 gs.getline(temp, sizeof(temp) );
		 npcinfo[cur_id].name = temp;
	}
	
	if(key[KEY_T])
	{
        clear_keybuf();		
		clear(screen);
        gstream gs(screen);
        gs.save_input_approver();
    	gs.set_input_approver(ia_allow_everything);
        gs.set_insert_mode(true);
        gs.set_max_input_length(1024);
        gs.goto_xy(0,0);
        gs << "Text?: ";
		
		gs.getline(temp, sizeof(temp) );
		npcinfo[cur_id].text = temp;		
	}
	
		
	if(key[KEY_LCONTROL] && key[KEY_S])
	{
		SaveNpcs();
		allegro_message("Saved.. you better hope you didn't fuck up");
	}
	
	else if(key[KEY_S])
	{
        clear_keybuf();				
		clear(screen);
        gstream gs(screen);
        gs.save_input_approver();
    	gs.set_input_approver(ia_allow_everything);
        gs.set_insert_mode(true);
        gs.set_max_input_length(1024);
        gs.goto_xy(0,0);
        gs << "Type?(yesno, none, setcheckquest): ";
		
		gs.getline(temp, sizeof(temp) );
		npcinfo[cur_id].yesno = temp;	
	}
	
	if(key[KEY_Y])
	{
        clear_keybuf();				
		clear(screen);
        gstream gs(screen);
        gs.save_input_approver();
    	gs.set_input_approver(ia_allow_everything);
        gs.set_insert_mode(true);
        gs.set_max_input_length(1024);
        gs.goto_xy(0,0);
        gs << "Text?: ";
		
		gs.getline(temp, sizeof(temp) );
		npcinfo[cur_id].yestext = temp;	
	}
	
	if(key[KEY_O])
	{
        clear_keybuf();				
		clear(screen);
        gstream gs(screen);
        gs.save_input_approver();
    	gs.set_input_approver(ia_allow_everything);
        gs.set_insert_mode(true);
        gs.set_max_input_length(1024);
        gs.goto_xy(0,0);
        gs << "No Text?: ";
		
		gs.getline(temp, sizeof(temp) );
		npcinfo[cur_id].notext = temp;	
	}
	
	if(key[KEY_I])
	{
        clear_keybuf();				
		clear(screen);
        gstream gs(screen);
        gs.save_input_approver();
    	gs.set_input_approver(ia_allow_integer);
        gs.set_insert_mode(true);
        gs.set_max_input_length(1024);
        gs.goto_xy(0,0);
        gs << "Quest idnum(check)?: ";
        gs >> npcinfo[cur_id].quest_idnum;		
	}
	
	if(key[KEY_M])
	{
        clear_keybuf();				
		clear(screen);
        gstream gs(screen);
        gs.save_input_approver();
    	gs.set_input_approver(ia_allow_integer);
        gs.set_insert_mode(true);
        gs.set_max_input_length(1024);
        gs.goto_xy(0,0);
        gs << "Quest idnum(set!)?: ";
        gs >> npcinfo[cur_id].set_quest_idnum;	
	}
	
	if(key[KEY_Q])
	{		
        clear_keybuf();				
		clear(screen);
        gstream gs(screen);
        gs.save_input_approver();
    	gs.set_input_approver(ia_allow_everything);
        gs.set_insert_mode(true);
        gs.set_max_input_length(1024);
        gs.goto_xy(0,0);
        gs << "Quest Text?: ";
		
		gs.getline(temp, sizeof(temp) );
		npcinfo[cur_id].quest_text = temp;	
	}
	
	if(key[KEY_C])
	{
		npcinfo.resize(npcinfo.size()+1);
		npcinfo[npcinfo.size()-2].id = npcinfo.size()-1;
		while(key[KEY_C]) {}
	}

  
  speed_counter--;
 }
textprintf_ex(vscreen, font, 0, 0,  makecol(255,255,255), 0,"Total Entries: %d curid %d", npcinfo.size(), cur_id);

textprintf_ex(vscreen, font, 0, 20,  makecol(255,255,255), 0,"Npc id: %d", npcinfo[cur_id].id);
textprintf_ex(vscreen, font, 0, 40,  makecol(255,255,255), 0,"(N)ame: %s", npcinfo[cur_id].name.c_str());
textprintf_ex(vscreen, font, 0, 50,  makecol(255,255,255), 0,"(T)ext: %s", npcinfo[cur_id].text.c_str());
textprintf_ex(vscreen, font, 0, 60,  makecol(255,255,255), 0,"(S)et Type(yesno,none,set?): %s", npcinfo[cur_id].yesno.c_str());
textprintf_ex(vscreen, font, 15, 70, makecol(255,255,255), 0,"(Y)es Text: %s", npcinfo[cur_id].yestext.c_str());
textprintf_ex(vscreen, font, 15, 80, makecol(255,255,255), 0,"N(o)  Text: %s", npcinfo[cur_id].notext.c_str());
textprintf_ex(vscreen, font, 0, 90,  makecol(255,255,255), 0,"(i) Quest Idnum(check): %d", npcinfo[cur_id].quest_idnum);
textprintf_ex(vscreen, font, 0, 100, makecol(255,255,255), 0,"(m) Quest Idnum(set!!): %d", npcinfo[cur_id].set_quest_idnum);
textprintf_ex(vscreen, font, 0, 110, makecol(255,255,255), 0,"(q) Quest Text: %s", npcinfo[cur_id].quest_text.c_str());


textprintf_ex(vscreen, font, 0, 150, makecol(255,255,255), 0,"Create New Entry: (C)reate");
textprintf_ex(vscreen, font, 0, 150, makecol(255,255,255), 0,"Save Entries to File (CTRL+S)");


////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                          Graphics
////////////////////////////////////////////////////////////////////////////////////////////////////////////


frame_count++;
show_mouse(mouse_sprite);
unscare_mouse();

blit(vscreen, screen, 0, 0, 0, 0, 1024, 768);
clear_bitmap(vscreen);
}while(!AppEditor.done);

return 0;
}
END_OF_MAIN();











