// DlgHelp.cpp : implementation file
//

#include "stdafx.h"
#include <fstream>
#include <string>

#include "CardGame.h"
#include "DlgHelp.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// DlgHelp dialog

static LPPICTURE gpMe = NULL;
static LPPICTURE gpPiazza = NULL;
static LPPICTURE gpSan = NULL;
static LPPICTURE gpPopolo = NULL;

// This method initialize the strings to show help rules and info
// english or italian, construct is a bool to check if the method is 
// called by constructor, and we do not need to invalidate the rect or by change to language
void DlgHelp::init(bool construct)
{

      // File names of the Info and for the Rules
   const char* pFileNameEng[2] = 
      {".\\napoletane\\rulesEng.txt",
         ".\\napoletane\\infoEng.txt"};
   const char* pFileNameIta[2] = 
      {".\\napoletane\\rulesIta.txt",
         ".\\napoletane\\infoIta.txt"};
   
   mMsgRules = "";
   mMsgInfo = "";

   for (int i = 0; i <= 1; i++)
   {
      std::ifstream in((mLanguage == LANENGLISH) ? pFileNameEng[i] : pFileNameIta[i]);
      if(!in) 
      {
         if (0 == i)
            mMsgRules = "Could not open file .\\Napoletane\\RulesEng (Ita).txt";
         else
            mMsgInfo = "Could not open file .\\Napoletane\\InfoEng (Ita).txt";
      }
      else
      {
         // Loading the strings by the files
         std::string line;
         while(std::getline(in, line)) 
            if (0 == i)
               mMsgRules += line + "\r\n";
            else
               mMsgInfo += line + "\r\n";
         in.close();
      }
   }
   if (!construct)
   {
      CRect rect;
      GetClientRect(&rect);
      InvalidateRect(&rect, TRUE);   
   }
}

DlgHelp::DlgHelp(CWnd* pParent /*=NULL*/)
	: CDialog(DlgHelp::IDD, pParent), mLanguage(LANENGLISH)
{
	//{{AFX_DATA_INIT(DlgHelp)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
   
   // Loading picture in the help Dialog
   myUtilities::LoadPictureFile(".\\napoletane\\me.jpg", gpMe);
   myUtilities::LoadPictureFile(".\\napoletane\\piazza.jpg", gpPiazza);
   myUtilities::LoadPictureFile(".\\napoletane\\san.jpg", gpSan);
   myUtilities::LoadPictureFile(".\\napoletane\\Popolo.jpg", gpPopolo);
   init(true);
}


void DlgHelp::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DlgHelp)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DlgHelp, CDialog)
	//{{AFX_MSG_MAP(DlgHelp)
   ON_WM_INITMENUPOPUP()
	ON_WM_PAINT()
	ON_WM_CANCELMODE()
	ON_COMMAND(IDM_LANGUAGE_ENGLISH, OnLanguageEnglish)
	ON_COMMAND(IDM_LANGUAGE_ITALIAN, OnLanguageItalian)
	ON_UPDATE_COMMAND_UI(IDM_LANGUAGE_ITALIAN, OnUpdateLanguageItalian)
	ON_UPDATE_COMMAND_UI(IDM_LANGUAGE_ENGLISH, OnUpdateLanguageEnglish)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DlgHelp message handlers


// This method is important to solve the check "V" symbol problem in the menu
// attached to a dialog box
void DlgHelp::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
{
    ASSERT(pPopupMenu != NULL);
    // Check the enabled state of various menu items.

    CCmdUI state;
    state.m_pMenu = pPopupMenu;
    ASSERT(state.m_pOther == NULL);
    ASSERT(state.m_pParentMenu == NULL);

    // Determine if menu is popup in top-level menu and set m_pOther to
    // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
    HMENU hParentMenu;
    if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
        state.m_pParentMenu = pPopupMenu;    // Parent == child for tracking popup.
    else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
    {
        CWnd* pParent = this;
           // Child windows don't have menus--need to go to the top!
        if (pParent != NULL &&
           (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
        {
           int nIndexMax = ::GetMenuItemCount(hParentMenu);
           for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
           {
            if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
            {
                // When popup is found, m_pParentMenu is containing menu.
                state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
                break;
            }
           }
        }
    }

    state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
    for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
      state.m_nIndex++)
    {
        state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
        if (state.m_nID == 0)
           continue; // Menu separator or invalid cmd - ignore it.

        ASSERT(state.m_pOther == NULL);
        ASSERT(state.m_pMenu != NULL);
        if (state.m_nID == (UINT)-1)
        {
           // Possibly a popup menu, route to first item of that popup.
           state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
           if (state.m_pSubMenu == NULL ||
            (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
            state.m_nID == (UINT)-1)
           {
            continue;       // First item of popup can't be routed to.
           }
           state.DoUpdate(this, TRUE);   // Popups are never auto disabled.
        }
        else
        {
           // Normal menu item.
           // Auto enable/disable if frame window has m_bAutoMenuEnable
           // set and command is _not_ a system command.
           state.m_pSubMenu = NULL;
           state.DoUpdate(this, FALSE);
        }

        // Adjust for menu deletions and additions.
        UINT nCount = pPopupMenu->GetMenuItemCount();
        if (nCount < state.m_nIndexMax)
        {
           state.m_nIndex -= (state.m_nIndexMax - nCount);
           while (state.m_nIndex < nCount &&
            pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
           {
            state.m_nIndex++;
           }
        }
        state.m_nIndexMax = nCount;
    }
}



void DlgHelp::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
   SetDlgItemText(IDC_EDITHELP, mMsgRules.c_str());
   SetDlgItemText(IDC_EDITCV, mMsgInfo.c_str());
   myUtilities::RenderPic(this, dc.m_hDC, gpMe, 30, 10);
   myUtilities::RenderPic(this, dc.m_hDC, gpPiazza, 550, 10);
   myUtilities::RenderPic(this, dc.m_hDC, gpPopolo, 550, 150);
   myUtilities::RenderPic(this, dc.m_hDC, gpSan, 550, 300);
//   SetCheck(mLanguage == LANENGLISH ? 0 : 1);
}

void DlgHelp::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
}

void DlgHelp::OnLanguageEnglish() 
{

   mLanguage = LANENGLISH;
   init(false);
}

void DlgHelp::OnLanguageItalian() 
{
   mLanguage = LANITALIAN;
   init(false);
}

void DlgHelp::OnUpdateLanguageItalian(CCmdUI* pCmdUI) 
{
   pCmdUI->SetCheck(mLanguage == LANITALIAN ? 1 : 0);
	
}

void DlgHelp::OnUpdateLanguageEnglish(CCmdUI* pCmdUI) 
{
   pCmdUI->SetCheck(mLanguage == LANENGLISH ? 1 : 0);
}

