// BOSS.CPP
//
// Copyright (c) 1998 Symbian Ltd.  All rights reserved.
//

//!!!!!!!!compiler warnings - to resolve!!!!!!!!!!!!
//LINK : warning LNK4052: using .EXP file; ignoring .DEF file "\EPOC32\RELEASE\WINS\APP.DEF"
// Creating library \EPOC32\RELEASE\WINS\DEB/BOSS.lib and object \EPOC32\RELEASE\WINS\DEB/BOSS.exp
//EDLL.OBJ : warning LNK4099: PDB "edll.pdb" was not found with "\EPOC32\RELEASE\WINS\DEB\EDLL.OBJ" or at "M:\EPOC32\RELEASE\WINS\DEB\edll.pdb"; linking object as if no debug info


#include <e32keys.h>

#include <coemain.h>

#include <eikenv.h>
#include <eikdef.h>
#include <eikcmds.hrh>
#include <eiklabel.h>

#include <boss.rsg>
#include "boss.hrh"
#include "boss.h"

#include "bossview.h"

void CBossAppView::ConstructL(const TRect& aRect, TBossPuzzle* aModel)
    {
	iModel=aModel;
	iMaxPhases=7;
    CreateWindowL();
    Window().SetShadowDisabled(ETrue);
    iContext=this;
   	iBrushStyle=CGraphicsContext::ESolidBrush;
    iBrushColor=KRgbWhite;
    SetRectL(aRect);
	CreateLabelL();
	ConstructViewL();
    ActivateL();
    }

CBossAppView::~CBossAppView()
    {
	delete iView;
	delete iLabel;
    }
    
TInt CBossAppView::CountComponentControls() const
	{
	return 1 + (iView ? 1 : 0); // always a label, sometimes a view
	}

CCoeControl* CBossAppView::ComponentControl(TInt aIndex) const
	{
	switch (aIndex)
		{
	case 0: return iLabel;
	case 1: return iView;
	default: return 0;
		};
	}

const TInt KLabelHeight=20;

void CBossAppView::CreateLabelL()
	{
	iLabel=new (ELeave) CEikLabel;
	TRect rect=Rect();
	rect.iTl.iY=rect.iBr.iY-KLabelHeight; // make it bottom 20 pixels
	iLabel->SetContainerWindowL(*this);
	iLabel->SetRectL(rect);
	iLabel->SetAlignment(EHCenterVCenter); // center text
	iLabel->SetBufferReserveLengthL(200); // nice long buffer
	iLabel->ActivateL(); // now ready
	}

void CBossAppView::ConstructViewL()
	{
	TRect rect=Rect(); // get our rect
	rect.iBr.iY-=KLabelHeight; // make way for label
	rect.Shrink(2,2); // shrink it a bit
	iView=new (ELeave) CBossView(this, rect, iModel);
	iView->ConstructL();
	UpdateModelL();
	}

void CBossAppView::NotifyStatus(const TDesC& aMessage)
	{
	iLabel->SetTextL(aMessage);
	iLabel->DrawNow();
	}

void CBossAppView::Quit()
	{
	_LIT(KControllerFinish,"controller finished"); //constant declaration
	delete iView;
	iView=0;
	NotifyStatus(KControllerFinish);
	}

void CBossAppView::NextPhaseL()
	{
	if (++iPhase >= iMaxPhases)
		Quit();
	else
		{
		UpdateModelL();
		iView->DrawNow();
		}
	}

void CBossAppView::UpdateModelL() // do this after construction ...
	{
	//constant declarations
	_LIT(KNotifyOrdered,"Ordered deal");
	_LIT(KNotifyMvdDown,"moved down");
	_LIT(KNotifyMvdDwnAgain,"moved down again");
	_LIT(KNotifyMvdRight,"moved right");
	_LIT(KNotifyMvdUp,"moved up");
	_LIT(KNotifyBossOrder,"ordered as Boss");
	_LIT(KNotifyOverRun,"overrun!");


	switch(iPhase)
		{
	case 0:
		iModel->SetFullyOrdered();
		NotifyStatus(KNotifyOrdered);
		break;
	case 1:
		iModel->Move(TBossPuzzle::EDown);
		NotifyStatus(KNotifyMvdDown);
		break;
	case 2:
		iModel->Move(TBossPuzzle::EDown);
		NotifyStatus(KNotifyMvdDwnAgain);
		break;
	case 3:
		iModel->Move(TBossPuzzle::ERight);
		NotifyStatus(KNotifyMvdRight);
		break;
	case 4:
		iModel->Move(TBossPuzzle::EUp);
		NotifyStatus(KNotifyMvdUp);
		break;
	case 5:
		iModel->SetBossOrdered();
		NotifyStatus(KNotifyBossOrder);
		break;
	case 6:
		iModel->Move(TBossPuzzle::ERight);
		NotifyStatus(KNotifyMvdRight);
		break;
	default:
		NotifyStatus(KNotifyOverRun);
		break;
		};
	}

TKeyResponse CBossAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
    {
	if	(iView)
   		return iView->OfferKeyEventL(aKeyEvent,aType);
	else
		return EKeyWasNotConsumed;
    }

void CBossAppView::Draw(const TRect& /*aRect*/) const
	{
	CWindowGc& gc = SystemGc();
	gc.SetPenStyle(CGraphicsContext::ENullPen);
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.DrawRect(Rect());
	}

//
// CBossAppUi
//

void CBossAppUi::ConstructL()
    {
	_LIT(KNotifyInitialised,"initialized"); //constant declaration

    BaseConstructL();
	iModel=((CBossDocument*)iDocument)->Model();
    iAppView=new(ELeave) CBossAppView;
    iAppView->ConstructL(ClientRect(),iModel);
	iAppView->NotifyStatus(KNotifyInitialised);
	// add app view to stack; enables key event handling.
	AddToStackL(iAppView);
    }

void CBossAppUi::HandleCommandL(TInt aCommand)
	{
	switch (aCommand)
		{
	case EBossNextPhase:
		iAppView->NextPhaseL();
		return;
	case EEikCmdExit:
		Exit();
		return;
		}
	}

CBossAppUi::~CBossAppUi()
	{
    delete iAppView;
	}

//
// CBossDocument
//

CEikAppUi* CBossDocument::CreateAppUiL()
	{
    return(new(ELeave) CBossAppUi);
	}

//
// CBossApplication
//

TUid CBossApplication::AppDllUid() const
	{
	return KUidBossApp;
	}

CApaDocument* CBossApplication::CreateDocumentL()
	{
	return new(ELeave) CBossDocument(*this);
	}

//
// EXPORTed functions
//

EXPORT_C CApaApplication* NewApplication()
	{
	return new CBossApplication;
	}

GLDEF_C TInt E32Dll(TDllReason)
	{
	return KErrNone;
	}
