#include "menu.h"
#include "display.h"
#include "delay.h"
#include "draw.h"
#include <stdio.h>
#include <stdlib.h>
#include "block.h"

//便于调试的预编译命令
//#define DEBUG_MODE

#ifdef DEBUG_MODE
#define M_DEBUG M_BLOCK
#endif

Menu M_MAINMENU;//主菜单
Menu M_BLOCK; 	//俄罗斯方块
Menu M_BALL;		//弹球

Menu M_ABOUT;

Menu* NOW; //当前菜单指针
Menu* LAST = NULL; //上一个状态的菜单

enum OPR opr = idle;
static unsigned char local[10] = {0};
unsigned char score = 0;
extern enum GS blockGameStatus;


//主菜单
void m_mainmenu(struct _menu* this)
{
  if(LAST != &M_MAINMENU)
    {
      LAST = &M_MAINMENU;
      drawMAINMENU(local[0]);
    }

  if(opr == right)
    {
      local[0]++;
      if(local[0] > 3)
        local[0] = 3;

      opr = idle;
      drawMAINMENU(local[0]);
    }
  if(opr == left)
    {
      if(local[0] != 0)
        local[0]--;

      opr = idle;
      drawMAINMENU(local[0]);
    }
  if(opr == confirm)
    {

      switch(local[0])
        {
        case 0:
          return;
        case 1:
          NOW = &M_BLOCK;				//转向下一菜单
          break;
        case 2:
          NOW = &M_BALL;				//转向下一菜单
          break;
        case 3:
          NOW = &M_ABOUT;			//转向下一菜单
          break;
        }

      local[0] = 0;
      clear();
      opr = idle;
    }
}


//方块游戏
void m_block(struct _menu* this)
{
  if(LAST != &M_BLOCK)
    {
      LAST = &M_BLOCK;
      drawVerticalDottedLine(64);
      drawBLOCKSCORE();
      blockInit();
    }
  //重置
  if(opr == confirm)
    {
      opr = idle;
      blockDestroy();
			NOW=&M_MAINMENU;
			clear();
      return;
    }
  if(opr == left)
    {
      opr = idle;
      moveLeftPiece();
			drawBlock();
			return;
    }
  if(opr == right)
    {
      opr = idle;
      moveRightPiece();
			drawBlock();
			return;
    }
  switch(blockGameStatus)
    {
    case good:
      drawGOOD(70, 2);
			blockGameStatus=start;
      break;
    case over:
      drawGAME(70, 2);
      drawOVER(70, 4);
      break;
    default:
      drawVOID8(70, 2, 40);
      drawVOID8(70, 4, 32);
    }
  drawSUPNUM(110, 0, score);		//绘制分数
	blockGameOver();							//检查是否游戏结束
  genPiece();  									//若有需要，生成新块
  dropPiece();									//若有需要，块下落
  judgeBlock();									//若有需要，清除一行
  drawBlock();									//绘制界面
  delayms(400);

}

//关于页
void m_about(struct _menu* this)
{


  if(LAST != &M_ABOUT)
    {
      LAST = &M_ABOUT;

      drawNAME(); 		 		//绘制名字
      delayms(2000);
      drawNUMBER(); 			//绘制学号
    }

  if(opr == confirm)
    {
      opr = idle;
      NOW = &M_MAINMENU;				//转向下一菜单
      clear();
    }
}

void menuInit()
{

  M_MAINMENU.f = m_mainmenu;
  M_BLOCK.f = m_block;
	M_ABOUT.f = m_about;

#ifdef DEBUG_MODE
  NOW = &M_DEBUG;
#else
  NOW = &M_MAINMENU;
#endif
}
