#include "maps\export.lgc"
#include "maps\common.lgc"
//#include "maps\common_compaign.lgc"
//#include "maps\common_menu.lgc"
//#include "maps\common_fame.lgc"

#define TIME_FOR_ONE_SLIDE 25000


//for MenuState
#define MENU_PLAY     0
#define MENU_OPTIONS  1
#define MENU_FAME     2
#define MENU_CREDITS  3
#define MENU_EXIT     4
#define MENU_CONTINUE 5
#define MENU_MAIN     6
#define MENU_COMPAIGN 7
#define MENU_SURVIVE  8
#define MENU_LOAD     9
#define MENU_ADDON    10
#define MENU_ADDON2   11
#define MENU_GUN_STAND 12
#define MENU_BUY      13

int MenuState = MENU_MAIN;//тип меню 0-Play,1-Options,2-FameHall,3-Credits,4-Quit,5-BaseMenu
int StartEffectTime = 0;//время начала видео эффекта
static int    EnterNameOn=0;
static string OldName;
static int    AddOnSlide = 0;
static int    DifficultyLevel = DEFAULT_DIFFICULTY_LEVEL;
static string DifficultyName[3];
static int    DifficultyText = 0;

ChangeMenuState(int new_menu_type)
{
  MenuState = new_menu_type;
  Effect(EFF_FADE,0,0,FADE_GAME_TIME);
  StartEffectTime = GetTime();
}


main()
{
  int menu,i;
  iff( 1 )
  {
    SetCursor(CURSOR_NORMAL);
//    PlayMusicFile("music\\menu_mus.ogg", 1);
    if( GetGlobValI("NewRecord",-999 ) != -999)
    {
      if(!DEMO_VERSION)
        ChangeMenuState(MENU_FAME);
      else
      {
        SetGlobValI("NewRecord",-999);
        Load("maps\\slide.map");
      }
    }
    else
      ChangeMenuState(MENU_MAIN);
    SetVidData(705,VID_GAMMA+1,0x7f7f7f);
  }
  if( MenuState==MENU_MAIN )//basemenu
  {
    if( StartEffectTime && (GetTime()-StartEffectTime) > FADE_GAME_TIME/2 )
    {
      StartEffectTime = 0;
      MenuRelease();
      MenuLoad("maps\\mainmenu.men");
      Call_i(EXT_SetCurUserG, -1);
      if(Call_v(EXT_GetUsersCnt) == 0)
      {
//        MenuAction(705,5,ANI_MENUDISABLE);
        Action(MenuFindNamed("continue"), ANI_MENUDISABLE);
      }
    }
    if ( MenuLClick() && GetUnitVid(MenuLClick()) == 933 )
        ChangeMenuState(MENU_BUY);
    else
    if ( MenuLClick() && MenuNVidUnderCursor()==932 )
    {
        if ( Action(MenuLClick(), ACT_GET_NAME) == "continue" && Call_v(EXT_GetUsersCnt) > 0 && !DEMO_VERSION)
          ChangeMenuState(MENU_CONTINUE);// не переходим к CONTINUE если save нет
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "new" )
          ChangeMenuState(MENU_PLAY);
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "options" )
          ChangeMenuState(MENU_OPTIONS);
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "credits" )
          ChangeMenuState(MENU_CREDITS);
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "highscores" && !DEMO_VERSION)
          ChangeMenuState(MENU_FAME);
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "exit" )
          ChangeMenuState(MENU_EXIT);
//      if (MenuNDirUnderCursor()!=MENU_CONTINUE || Call_v(EXT_GetUsersCnt) > 0)
//        ChangeMenuState(MenuNDirUnderCursor());
    }
    if( GetKey()==' ' || GetKey()==13 )
      ChangeMenuState(MENU_PLAY);
    if( GetKey()==27 )
      ChangeMenuState(MENU_EXIT);
  }
  else if( MenuState==MENU_CONTINUE )//Continue
  {
		int iUsersCnt = Call_v(EXT_GetUsersCnt);
		if( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
		{
			StartEffectTime = 0;
			MenuRelease();
			MenuLoad("maps\\load.men");
			//загружаем списочек профилей
			int num2 = iUsersCnt;
			if(num2 > MAX_SAVE)
				num2 = MAX_SAVE;
			for(i = 0; i < num2; i++)
			{
				string str;
				str = Call_i(EXT_GetUsrNameG, iUsersCnt-i-1);
				int spr = MenuFind(4, 999000+100+i);
				if(spr)
					Action(spr, ACT_SET_TEXT, &str);
			}
			for(i = num2; i < MAX_SAVE; i++)
				Action(MenuFindNamed(itoa(i)),ANI_MENUDISABLE);
		}
		if ( GetKey()==VK_ESCAPE )
			ChangeMenuState(MENU_MAIN);
		else if ( MenuLClick() && MenuNVidUnderCursor()==705 )
		{
            if ( Action(MenuLClick(), ACT_GET_NAME) == "mainmenu" )
        		ChangeMenuState(MENU_MAIN);
            else
            {
    			int n = Action(MenuLClick(), ACT_GET_NAME);//MenuNDirUnderCursor();
    			n = iUsersCnt-n-1;
    			if ( n >= 0 && n < iUsersCnt )
    			{
    				Call_i(EXT_SetCurUserG, n);
    				OnContinueGame();
    				ChangeMenuState(MENU_LOAD);
    			}
            }
		}
  }
  else if( MenuState==MENU_PLAY )//Play
  {
    string text;
    if( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
      StartEffectTime = 0;
      //
      PlayerName = GetGlobValS("PlayerNameLast",GetString("menu","DefaultName"));
      gsPlayer_Sex = GetGlobValS("PlayerSexLast","male");
      DifficultyLevel = GetGlobValI("DifficultyLevelLast",DEFAULT_DIFFICULTY_LEVEL);
      //
      MenuRelease();
      int addon1;
      addon1 = FOpen("maps\\addon\\Level_01.lgc");
      if(1)//!DEMO_VERSION)
      {
	      if ( addon1 )
    	  {  
        	  FClose(addon1);
	          MenuLoad("maps\\PlayWithAddon1.men");
	      }
    	  else
        	  MenuLoad("maps\\play.men");//PlayWithoutAddon1.men");
	  }
      if( gsPlayer_Sex=="male" )
        MenuAction(706,0,ANI_MENUSTANDUP);
      else
        MenuAction(707,0,ANI_MENUSTANDUP);
      DifficultyName[0] =  "Easy";
      DifficultyName[1] =  "Normal";
      DifficultyName[2] =  "Hard";
      int butt;
      butt = MenuFind(717);
      //if(!DEMO_VERSION)
      	DifficultyText = CreateText(3, ToScreenX(GetX(butt)), ToScreenY(GetY(butt)) - 1, GetZ(butt) + 1, GetString("menu", DifficultyName[DifficultyLevel]), BEH_CENTER);
    }
    //
    /*if(DEMO_VERSION)
    {
      ChangeMenuState(MENU_COMPAIGN);
    }
    else*/
      if( MenuNVidUnderCursor()==716 && MenuLClick() )
        if( MenuNDirUnderCursor()==0 )
        {// стрелка влево для уровня сложности
          if ( DifficultyLevel ) DifficultyLevel--;
          text = GetString("menu", DifficultyName[DifficultyLevel]);
          Action(DifficultyText, ACT_SET_TEXT, &text);
        }
        else
        {// стрелка вправо для уровня сложности
          if ( DifficultyLevel < 2 ) DifficultyLevel++;
          text = GetString("menu", DifficultyName[DifficultyLevel]);
          Action(DifficultyText, ACT_SET_TEXT, &text);
        }
      else if( MenuNVidUnderCursor()==717 && MenuLClick() )
      {// смена уровня сложности
          if ( DifficultyLevel < 2 ) DifficultyLevel++;
          else                       DifficultyLevel = 0;
          text = GetString("menu", DifficultyName[DifficultyLevel]);
          Action(DifficultyText, ACT_SET_TEXT, &text);
      }
      else if( MenuNVidUnderCursor()==706 && MenuLClick() )
      {
        MenuAction(706,0,ANI_MENUSTANDUP);
        MenuAction(707,0,ANI_MENUSTANDDOWN);
        gsPlayer_Sex = "male";
      }
      else if( MenuNVidUnderCursor()==707 && MenuLClick() && !DEMO_VERSION )
      {
        MenuAction(707,0,ANI_MENUSTANDUP);
        MenuAction(706,0,ANI_MENUSTANDDOWN);
        gsPlayer_Sex = "female";
      }
      else if( MenuNVidUnderCursor()==708 && !EnterNameOn && MenuLClick() )//PlayerName
      {
        EnterNameOn = 1;
        OldName = PlayerName;
        if( PlayerName==GetString("menu","DefaultName") )
          PlayerName = "";
        PlayerName += "_";
      }
      else if ( MenuLClick() && MenuNVidUnderCursor()==705 )
      {
        if ( Action(MenuLClick(), ACT_GET_NAME) == "compaign" )
          ChangeMenuState(MENU_COMPAIGN);
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "addon2" )
          ChangeMenuState(MENU_ADDON2);
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "survive" )
          ChangeMenuState(MENU_SURVIVE);
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "gunstand" )
          ChangeMenuState(MENU_GUN_STAND);
        else if ( Action(MenuLClick(), ACT_GET_NAME) == "mainmenu" )
          ChangeMenuState(MENU_MAIN);
      }
    if( EnterNameOn )
    {// Ввод имени игрока в процессе
      if( GetKey()==VK_BACK && strlen(PlayerName) > 1 )
      {
        PlayerName[strlen(PlayerName)-2] = '_';
        PlayerName[strlen(PlayerName)-1] = 0;
      }
      else if( GetKey()==VK_RETURN || (MenuLClick() && MenuNVidUnderCursor()!=708) )
      {
        EnterNameOn = 0;
        PlayerName[strlen(PlayerName)-1] = 0;//убираем '_'
      }
      else if( GetKey()==VK_ESCAPE )
      {
        EnterNameOn = 0;
        PlayerName = OldName;
      }
      else if( strlen(PlayerName) < 11 && GetKey() >= 'A' && GetKey() <= 'Z' )
      {
        PlayerName[strlen(PlayerName)-1] = 0;//убираем '_'
        PlayerName += Printf("%c_",GetKey());
      }
      else if( strlen(PlayerName) < 11 && GetKey() >= 'a' && GetKey() <= 'z' )
      {
        PlayerName[strlen(PlayerName)-1] = 0;//убираем '_'
        PlayerName += Printf("%c_",GetKey()-('a'-'A'));
      }
      else if( strlen(PlayerName) < 11 && GetKey() >= '0' && GetKey() <= '9' )
      {
        PlayerName[strlen(PlayerName)-1] = 0;//убираем '_'
        PlayerName += Printf("%c_",GetKey());
      }
      else if( strlen(PlayerName) < 11 && GetKey()=='_' )
      {
        PlayerName[strlen(PlayerName)-1] = 0;//убираем '_'
        PlayerName += Printf("%c_",GetKey());
      }
      else if( GetKey() && GetKey() < 255 )
        PlaySFX(46);//нажата неверная клавиша
    }
    else if( GetKey()==VK_ESCAPE )
      ChangeMenuState(MENU_MAIN);
    else if( GetKey()==' ' || GetKey()==VK_RETURN )
      ChangeMenuState(MENU_COMPAIGN);
  }
  else if ( MenuState==MENU_OPTIONS )//Options
  {
    if ( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
      StartEffectTime = 0;
      CreateOptionsMenu("maps\\options.men");
    }
    if ( MenuNVidUnderCursor()==705 && MenuLClick() )
    {
      if ( Action(MenuLClick(), ACT_GET_NAME) == "back" )
        ChangeMenuState(MENU_MAIN);
    }
    if ( GetKey()==27 && !gbWasEnterBloodPass)
      ChangeMenuState(MENU_MAIN);
    TactOptionsMenu();
  }
  else if( MenuState==MENU_FAME )//Hall of Fame
  {
    static int siShowFameTime = 0;
    if(!siShowFameTime)
    	siShowFameTime = GetTime();
    if( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
      StartEffectTime = 0;
      MenuRelease();
      DrawFame();
    }
    if(GetTime()-siShowFameTime > 3000)
    {
      if( GetKey() || (GetInputState()&(INPUT_LCLICK|INPUT_RCLICK)) )
        ChangeMenuState(MENU_MAIN);
    }
  }
  else if( MenuState==MENU_CREDITS )//Credits
  {
    if( StartEffectTime && GetEffectState(EFF_FADE) > 44 )
    {
      StartEffectTime = 0;
      MenuRelease();
      MenuLoad("maps\\credits.men");
    }
    if( GetKey() || (GetInputState()&(INPUT_LCLICK|INPUT_RCLICK)) )
      ChangeMenuState(MENU_MAIN);
  }
  else if( MenuState==MENU_EXIT )//Quit
  {
    if( StartEffectTime && GetEffectState(EFF_FADE) > 44 )
    {
      StartEffectTime = 0;
//      Load( "maps\\slide.map" );// for demo
      MenuRelease();
      if(DEMO_VERSION)
      {
      	//CreateSprite(951, ScreenX()/2, ScreenY()/2, 0);
      	Load("maps\\slide.map");
      }
    }
    if( GetKey() || (GetInputState()&(INPUT_LCLICK|INPUT_RCLICK)) || !DEMO_VERSION)
      Exit();
  }
  else if( MenuState==MENU_BUY )
  {
    if( StartEffectTime && GetEffectState(EFF_FADE) > 44 )
    {
      StartEffectTime = 0;
      Exec(GetString("strings","BuyCommand"));
      Exit();
    }
  }
  else if( MenuState==MENU_COMPAIGN )//Compaign
  {
    if( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
      StartEffectTime = 0;
      BeginNewGame(GAME_TYPE_COMPAIGN, PlayerName, DifficultyLevel);
    }
  }
  else if( MenuState==MENU_ADDON )//addon
  {
    int text = 0, text_count, old_time = 0;
    if ( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
        StartEffectTime = 0;
        if ( AddOnSlide > 4 )
        {
        	BeginNewGame(GAME_TYPE_ADDON, PlayerName, DifficultyLevel);
            return;
        }
        else
        {
            MenuRelease();
            text_count = 0;
            old_time = GetTime();
            MenuCreate(954, AddOnSlide, ScreenX()/2, ScreenY()/2, 0 );
//            text = CreateTextFromFile(3, ScreenX()/2 - 300, ScreenY()/2 + 150, 1, "Text\\addon_story_0" + itoa(++AddOnSlide) + ".txt");
            text = CreateTextFromFile(3, 10, 10, 1, "Text\\addon_story_0" + itoa(++AddOnSlide) + ".txt");
            Action(text, ACT_SET_TEXT_COUNT, text_count);
        }
    }
    if ( !StartEffectTime )
        if ( GetKey() || (GetInputState() & (INPUT_LCLICK | INPUT_RCLICK)) || (GetTime() - old_time) > TIME_FOR_ONE_SLIDE )
            if ( text_count )
            {
                Effect(EFF_FADE,0,0,FADE_GAME_TIME);
                StartEffectTime = GetTime();
            }
            else
            {
                text_count = 10000;
                Action(text, ACT_SET_TEXT_COUNT, text_count);
    //            Action(text, ACT_SET_FILE, "Text\\addon_story_0" + itoa(AddOnSlide) + ".txt");
            }
    if ( GetKey() == 27 || (GetInputState() & INPUT_RCLICK) )
    {
        AddOnSlide = 5;
        old_time = GetTime() - TIME_FOR_ONE_SLIDE - 2000;
    }
  }
  else if( MenuState==MENU_ADDON2 )//addon2
  {
    int text = 0, text_count, old_time = 0;
    if ( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
        StartEffectTime = 0;
        if ( AddOnSlide > 4 )
        {
            BeginNewGame(GAME_TYPE_ADDON2, PlayerName, DifficultyLevel);
            return;
        }
        else
        {
            MenuRelease();
            text_count = 0;
            old_time = GetTime();
            MenuCreate(958, AddOnSlide, ScreenX()/2, ScreenY()/2, 0 );
//            text = CreateTextFromFile(3, ScreenX()/2 - 300, ScreenY()/2 + 150, 1, "Text\\addon_story_0" + itoa(++AddOnSlide) + ".txt");
            text = CreateTextFromFile(3, 10, 10, 1, "Text\\addon2story_0" + itoa(++AddOnSlide) + ".txt");
            Action(text, ACT_SET_TEXT_COUNT, text_count);
        }
    }
    if ( !StartEffectTime )
        if ( GetKey() || (GetInputState() & (INPUT_LCLICK | INPUT_RCLICK)) || (GetTime() - old_time) > TIME_FOR_ONE_SLIDE )
            if ( text_count )
            {
                Effect(EFF_FADE,0,0,FADE_GAME_TIME);
                StartEffectTime = GetTime();
            }
            else
            {
                text_count = 10000;
                Action(text, ACT_SET_TEXT_COUNT, text_count);
    //            Action(text, ACT_SET_FILE, "Text\\addon_story_0" + itoa(AddOnSlide) + ".txt");
            }
    if ( GetKey() == 27 || (GetInputState() & INPUT_RCLICK) )
    {
        AddOnSlide = 5;
        old_time = GetTime() - TIME_FOR_ONE_SLIDE - 2000;
    }
  }
  else if( MenuState==MENU_SURVIVE )//Survive
  {
    if( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
      StartEffectTime = 0;
      StopMusic(0);
      BeginNewGame(GAME_TYPE_SURVIVE, PlayerName, DifficultyLevel);
    }
  }
  else if( MenuState==MENU_GUN_STAND )//Survive gun stand
  {
    if( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
      StartEffectTime = 0;
      StopMusic(0);
      BeginNewGame(GAME_TYPE_GUN_STAND, PlayerName, DifficultyLevel);
    }
  }
  else if( MenuState==MENU_LOAD )//Load Compaign
  {
    if( StartEffectTime && GetEffectState(EFF_FADE) > 45 )
    {
      StartEffectTime = 0;
      Load("maps\\shop.map");
    }
  }
}
