/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     m2d_pageHeaderPanel.cpp

  Copyright (C)2007-2009 Nintendo  All Rights Reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law. They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Revision: 1758 $
 *-------------------------------------------------------------------------*/

#include <ntmv/m2d/textCanvasContext.h>

#include <ntmv/m2d/pageHeaderPanel.h>
#include <ntmv/m2d/uiResDefines.h>
#include <ntmv/m2d/objG2dInfo.h>
#include <ntmv/m2d/nlv_skin_up_obj_NANR_LBLDEFS.h>
#include <cwchar>

namespace ntmv
{
namespace m2d
{

namespace
{

const int   TEXT_START_CHAR_X   =  0;
const int   TEXT_START_CHAR_Y   =  0;
const int   TEXT_CWIDTH         = 32;
const int   TEXT_CHEIGHT        =  2;
const int   TEXT_HSPACE         =  0;    // Character spacing (in pixels) when rendering strings

// Width of the header number string
const int   HEADER_NUM_TEXT_WIDTH   = 9*2;

// Starting x-coordinate for displaying the header string
const int   HEADER_TEXT_START_X     = HEADER_NUM_TEXT_WIDTH + 4;

}   // namespace

/*---------------------------------------------------------------------------*
  Name:         PageHeaderPanel::PageHeaderPanel

  Description:  Constructor.

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
PageHeaderPanel::PageHeaderPanel()
:   mText(0)
{
}

/*---------------------------------------------------------------------------*
  Name:         PageHeaderPanel::Init

  Description:  Performs initialization.

  Arguments:    pObjManager: A pointer to an ObjManager object.
                fonts:       Font array.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
PageHeaderPanel::Init(
    ObjManager*             pObjManager,
    const NNSG2dFont *const fonts[]
)
{
    Base::Init(
        Point(GX_LCD_SIZE_X / 2, detail::PAGEHEADERVIEW_HEIGHT/2),
        Size(detail::PAGEHEADERVIEW_WIDTH, detail::PAGEHEADERVIEW_HEIGHT),
        pObjManager->CreateCellAnim(NANR_nlv_skin_up_obj_CellAnime0));
    mFonts = fonts;
}

/*---------------------------------------------------------------------------*
  Name:         PageHeaderPanel::SetText

  Description:  Sets text.

  Arguments:    text:  Text string to set.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
PageHeaderPanel::SetText(
    const wchar_t*  text,
    u16             headerIdx
)
{
    mText = text;

    TextCanvasContext textCanvasContext(
        true,
        Point(TEXT_START_CHAR_X, TEXT_START_CHAR_Y),
        Size(TEXT_CWIDTH, TEXT_CHEIGHT),
        mFonts[FONTSIZE_12],
        TEXT_HSPACE);

    s16 headerTextStartX = 0;
    if (headerIdx != u16(-1))   // There is a header index
    {
        // Header index string
        //   create a string
        const int numTextBufSize = 9 + 1;   // 9 digits \0: This will not overflow because it allocates enough memory for the maximum number of digits in a u32 value.
                                            //           
        wchar_t numTextBuf[numTextBufSize]; 
        std::swprintf(numTextBuf, numTextBufSize, L"%u", headerIdx + 1);
        NNS_G2dTextCanvasDrawTextRect(
            textCanvasContext.GetTextCanvas(),
            0,
            0,
            HEADER_NUM_TEXT_WIDTH,
            TEXT_CHEIGHT * 8,
            TEXT_COLOR_FG,
            NNS_G2D_HORIZONTALALIGN_CENTER | NNS_G2D_VERTICALALIGN_MIDDLE,
            numTextBuf
        );

        headerTextStartX = HEADER_TEXT_START_X;
    }

    // Heading string
    NNS_G2dTextCanvasDrawText(
        textCanvasContext.GetTextCanvas(),
        headerTextStartX,
        TEXT_CHEIGHT * 8 / 2,
        TEXT_COLOR_FG,
        NNS_G2D_HORIZONTALORIGIN_LEFT | NNS_G2D_VERTICALORIGIN_MIDDLE | NNS_G2D_HORIZONTALALIGN_LEFT,
        mText
    );
}

}   // namespace m2d
}   // namespace ntmv
