/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     pageView.h

  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: 1906 $
 *-------------------------------------------------------------------------*/

#ifndef NTMV_M2D_MANUALVIEW_H_
#define NTMV_M2D_MANUALVIEW_H_

#if defined(__CWCC__)
#pragma once
#endif

// #define DOC_SCROLL_DEBUG

#include <nnsys/g2d/g2d_Font.h>
#include <ntmv/m2d/types.h>
#include <ntmv/m2d/arcResourceAccessor.h>
#include <ntmv/m2d/touchPanel.h>
#include <ntmv/m2d/pageView.h>
#include <ntmv/m2d/soundManager.h>
#include <ntmv/m2d/objManager.h>
#include <ntmv/m2d/gamePad.h>


// Archive name for identifying manuals
#define NTMV_M2D_MANARCID       "M2M"

// Path to the manual's root directory
#define NTMV_M2D_MANROOTDIR     NTMV_M2D_MANARCID ":/arc"


namespace ntmv
{
namespace m2d
{

const int   VRAM_WIDTH  = 256;
const int   VRAM_HEIGHT = 256;

const int   FRAMEBUF_WIDTH  = VRAM_WIDTH;
const int   FRAMEBUF_HEIGHT = VRAM_HEIGHT * 2;


enum
{
    INPUTKIND_NONE,
    INPUTKIND_TPSCROLL,
    INPUTKIND_TPPOINTING,
    INPUTKIND_DOC,
    INPUTKIND_KEYSCROLL,

    INPUTKIND_MAX
};

class Pane;
class Picture;
class TouchPanel;
class TouchRect;
class ManualTableOfContents;
class PageHeaderPanel;
class ManualTocPanel;
class ScrollButton;
class ScrollIndicator;
class UIPanel;

class ManualView
{
public:
                        ManualView();

                        ~ManualView();

    void                Init(
                            const NNSFndAllocator*  pAllocator,
                            const char*             manualFilePath,
                            const char*             uiResFilePath,
                            const NNSG2dFont *const fonts[],
                            u16                     lang);

    void                Init(
                            const NNSFndAllocator*  pAllocator,
                            void*                   manualArcData,
                            void *const             uiResource,
                            const NNSG2dFont *const fonts[],
                            u16                     lang);

    void                Run();

private:
    void                InitLib(const NNSFndAllocator* pAllocator);

    void                InitCommon(
                            void*                   manualArcData,
                            void *const             uiResource,
                            const NNSG2dFont *const fonts[],
                            u16                     lang);

    void                InitFont(const NNSG2dFont *const fonts[]);
    void                InitVRAM();
    void                InitDisplay();
    void                LoadNtpg(
                            const char*         lytFileName,
                            ResourceAccessor*   pResAccessor,
                            bool                bPrev);

    void                ClearNtpg();

    void                SetLanguage(u16 lang);

    void                LoadStartPages();

    bool                OnTouchManualToc(const TouchPanelData& tpData);
    bool                OnTouchScrollUI(const TouchPanelData& tpData);
    bool                OnTouchDocument(const TouchPanelData& tpData);
    bool                OnKeyScroll();
    bool                OnKeyEvent();

    void                ProcessHelpButton(bool bSelected);

    void                ProcessExitButton();

    void                Draw();

    void                TransferDrawData(
                            s16         docPosX     = 0
                        );

    void                LoadSelectedPage();

    void                LoadHelpPage();

    void                ResetHardware();

    bool                IsScrollBusy() const    { return mTouchMoveY != 0; }

    void                ResetScrollInertia();

    void                SetDocBrightness(int brightness);

    void                SetDocFade(int posX);

    void                SetUpDispScrollIndicatorState(bool bOpened);

    #if defined(NTMV_M2D_DEVELOP)
        void                DumpHeap();
    #else   // #if defined(NTMV_M2D_DEVELOP)
        inline void         DumpHeap() {}
    #endif  // #if defined(NTMV_M2D_DEVELOP)

    NNSFndAllocator     mAllocator;
    void*               mManualArcData;
    void*               mUiResArcData;
    NNSFndArchive       mManualArc;
    bool                mbMountManual;
    NNSFndArchive       mUiResArc;
    bool                mbMountUIRes;
    const NNSG2dFont*   mFonts[FONTSIZE_MAX];
    SoundManager        mSndMgr;

    ObjManager*         mpObjManagerMain;
    ObjManager*         mpObjManagerSub;

    ManualTableOfContents*
                        mpManualToc;
    UIPanel*            mpPanels[DISPTYPE_MAX];
    PageHeaderPanel*    mpPageHeaderPanel;
    ManualTocPanel*     mpManualTocPanel;
    Page*               mpUITextPage;
    ScrollButton*       mpScrollButton;
    ScrollIndicator*    mpScrollIndicators[DISPTYPE_MAX];
    u16                 mFrameBuf[FRAMEBUF_WIDTH * FRAMEBUF_HEIGHT] ATTRIBUTE_ALIGN(16);
    PageView            mPageView;
    Page*               mpPage;

    GamePad             mGamePad;
    TouchPanel          mTouchPanel;

    ArcResourceAccessor mUIResAccessor;
    ArcResourceAccessor mManArcAccessor;

    s32                 mPageOrder;         // Value for determining the order of a page (that is currently displayed)
    s16                 mTouchOnDispY;      // Y-coordinate on the Touch Panel when the Manual was tapped
    s16                 mPreDragDocY;       // Previous y-coordinate on the Touch Panel while the bar is being dragged
    s16                 mDocDragDir;        // Direction in which the Manual is being dragged
    s16                 mTouchOnDocY;       // Y-coordinate of the Manual when it was tapped
    s16                 mMaxMovePosY;
    s16                 mTouchMoveYs[3];
    s16                 mTouchMoveY;
    s16                 mInputKind;

    bool                mbTouchDoc;
    bool                mbExit;
    bool                mbSetDMA;
    bool                mbEnabledVBlankIntr;
    bool                mbPMAutoExit;

#if defined(DOC_SCROLL_DEBUG)
    s16                 mDocScrollMax;      // Maximum distance to scroll the manual
    s16                 mDocScrollLoss;     // Amount to dampen manual scrolling
#else
    static const s16    mDocScrollMax;
    static const s16    mDocScrollLoss;
#endif
};

}   // namespace m2d
}   // namespace ntmv

/* NTMV_M2D_MANUALVIEW_H_ */
#endif
