// win32mineDoc.cpp : Cwin32mineDoc 類別的實作
//

#include "stdafx.h"
#include "win32mine.h"

#include "win32mineDoc.h"
#include "../facade_events.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Cwin32mineDoc

IMPLEMENT_DYNCREATE(Cwin32mineDoc, CDocument)

BEGIN_MESSAGE_MAP(Cwin32mineDoc, CDocument)
    ON_COMMAND(IDM_INTERMEDIATE, &Cwin32mineDoc::OnIntermediate)
    ON_COMMAND(IDM_NEWGAME, &Cwin32mineDoc::OnNewgame)
    ON_UPDATE_COMMAND_UI(IDM_INTERMEDIATE, &Cwin32mineDoc::OnUpdateIntermediate)
    ON_COMMAND(IDM_BEGINNER, &Cwin32mineDoc::OnBeginner)
    ON_UPDATE_COMMAND_UI(IDM_BEGINNER, &Cwin32mineDoc::OnUpdateBeginner)
    ON_COMMAND(IDM_ADVANCED, &Cwin32mineDoc::OnAdvanced)
    ON_UPDATE_COMMAND_UI(IDM_ADVANCED, &Cwin32mineDoc::OnUpdateAdvanced)
END_MESSAGE_MAP()


// Cwin32mineDoc 建構/解構

Cwin32mineDoc::Cwin32mineDoc():
    m_width(BEGINNERWIDTH), 
    m_height(BEGINNERHEIGHT), 
    m_mineCount(0),
    m_resetStatus(IDB_SMILE1),
    m_clockTicks(0),
    m_level(IDM_BEGINNER),
    m_mineCountOriginal(10)
    {
	// TODO: 在此加入一次建構程式碼
    int i, j;
    for (i=0; i<m_height; i++)
        for (j=0; j<m_width; j++)
            m_cells[i][j] = IDB_CELL;
}

Cwin32mineDoc::~Cwin32mineDoc()
{
}

BOOL Cwin32mineDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: 在此加入重新初始化程式碼
	// (SDI 文件會重用此文件)
    SetTitle("踩地雷");

    e_initializeProgram();

    OnBeginner();
    m_resetStatus = IDB_SMILE1;
    m_clockTicks = 0;
    m_level = IDM_BEGINNER;

	return TRUE;
}




// Cwin32mineDoc 序列化

void Cwin32mineDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: 在此加入儲存程式碼
	}
	else
	{
		// TODO: 在此加入載入程式碼
	}
}


// Cwin32mineDoc 診斷

#ifdef _DEBUG
void Cwin32mineDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void Cwin32mineDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// width of the game board

int Cwin32mineDoc::GetBoardWidth(void)
{
    return m_width;
}

// height of the game board
int Cwin32mineDoc::GetBoardHeight(void)
{
    return m_height;
}

CELLROWPTR Cwin32mineDoc::GetCells(void)
{
    return m_cells;
}

int Cwin32mineDoc::GetMineCount(void)
{
    return m_mineCount;
}

int Cwin32mineDoc::GetResetStatus(void)
{
    return m_resetStatus;
}

int Cwin32mineDoc::GetClockTicks(void)
{
    return m_clockTicks;
}

void Cwin32mineDoc::resetBtnPressed(void)
{
    switch (m_resetStatus)
    {
    case IDB_FRAWN1:
        m_resetStatus = IDB_FRAWN0;
        break;
    case IDB_SMILE1:
        m_resetStatus = IDB_SMILE0;
        break;
    }
    UpdateAllViews(0);
}

void Cwin32mineDoc::resetBtnReleased(void)
{
    switch (m_resetStatus)
    {
    case IDB_FRAWN0:
        m_resetStatus = IDB_FRAWN1;
        break;
    case IDB_SMILE0:
        m_resetStatus = IDB_SMILE1;
        break;
    }
    UpdateAllViews(0);
}

void Cwin32mineDoc::SetBoardSize(int width, int height)
{
    m_width = width;
    m_height = height;
    int i, j;
    for (i=0; i<m_height; i++)
        for (j=0; j<m_width; j++)
            m_cells[i][j] = IDB_CELL;

    UpdateAllViews(0, 1); // recalculate the window width and height
}

void Cwin32mineDoc::OnNewgame()
{
    // TODO: 在此加入您的命令處理常式程式碼
    m_mineCount = m_mineCountOriginal;

    e_newGame(m_width, m_height, m_mineCount);
}

void Cwin32mineDoc::OnIntermediate()
{
    // TODO: 在此加入您的命令處理常式程式碼
    m_level = IDM_INTERMEDIATE;
    SetBoardSize(16, 16);
    m_mineCountOriginal = 40;
    m_mineCount = m_mineCountOriginal;

    e_newGame(m_width, m_height, m_mineCount);
}

void Cwin32mineDoc::OnUpdateIntermediate(CCmdUI *pCmdUI)
{
    // TODO: 在此加入您的命令更新 UI 處理常式程式碼
    if (m_level == IDM_INTERMEDIATE)
        pCmdUI->SetCheck();
    else
        pCmdUI->SetCheck(FALSE);
}

void Cwin32mineDoc::OnBeginner()
{
    // TODO: 在此加入您的命令處理常式程式碼
    m_level = IDM_BEGINNER;
    SetBoardSize(9, 9);
    m_mineCountOriginal = 10;
    m_mineCount = m_mineCountOriginal;

    e_newGame(m_width, m_height, m_mineCount);
}

void Cwin32mineDoc::OnUpdateBeginner(CCmdUI *pCmdUI)
{
    // TODO: 在此加入您的命令更新 UI 處理常式程式碼
    if (m_level == IDM_BEGINNER)
        pCmdUI->SetCheck();
    else
        pCmdUI->SetCheck(FALSE);
}

void Cwin32mineDoc::OnAdvanced()
{
    // TODO: 在此加入您的命令處理常式程式碼
    m_level = IDM_ADVANCED;
    SetBoardSize(30, 16);
    m_mineCountOriginal = 99;
    m_mineCount = m_mineCountOriginal;

    e_newGame(m_width, m_height, m_mineCount);
}

void Cwin32mineDoc::OnUpdateAdvanced(CCmdUI *pCmdUI)
{
    // TODO: 在此加入您的命令更新 UI 處理常式程式碼
    if (m_level == IDM_ADVANCED)
        pCmdUI->SetCheck();
    else
        pCmdUI->SetCheck(FALSE);
}

void Cwin32mineDoc::SetClock(int clockValue)
{
    m_clockTicks = clockValue;
    UpdateAllViews(0);
}

void Cwin32mineDoc::SetMineCount(int count)
{
    m_mineCount = count;
    UpdateAllViews(0);
}

void Cwin32mineDoc::SetCell(int value, int irow, int icol)
{
//    TRACE("m_cell[%d][%d]=%d\n", irow, icol, value);
    switch (value)
    {
    case -5:
        m_cells[irow][icol] = IDB_CELL;
        break;
    case -4:
        m_cells[irow][icol] = IDB_QMARK;
        break;
    case -3:
        m_cells[irow][icol] = IDB_FLAG;
        break;
    case -2:
        m_cells[irow][icol] = IDB_BLASTEDMINE;
        break;
    case -1:
        m_cells[irow][icol] = IDB_MINE;
        break;
    case 0:
        m_cells[irow][icol] = IDB_OC0;
        break;
    case 1:
        m_cells[irow][icol] = IDB_OC1;
        break;
    case 2:
        m_cells[irow][icol] = IDB_OC2;
        break;
    case 3:
        m_cells[irow][icol] = IDB_OC3;
        break;
    case 4:
        m_cells[irow][icol] = IDB_OC4;
        break;
    case 5:
        m_cells[irow][icol] = IDB_OC5;
        break;
    case 6:
        m_cells[irow][icol] = IDB_OC6;
        break;
    case 7:
        m_cells[irow][icol] = IDB_OC7;
        break;
    case 8:
        m_cells[irow][icol] = IDB_OC8;
        break;
    }
    UpdateAllViews(0);
}

void Cwin32mineDoc::SetFrawn(void)
{
    m_resetStatus = IDB_FRAWN1;
    UpdateAllViews(0);
}

void Cwin32mineDoc::SetSmile(void)
{
    m_resetStatus = IDB_SMILE1;
    UpdateAllViews(0);
}