// ramzor3View.cpp : implementation of the CRamzor3View class
//

#include "stdafx.h"
#include "ramzor3.h"
#include "general.h"
#include "SimulationParams.h"
#include "policy.h"

#include "Simulation.h"
#include "ramzor3Doc.h"
#include "spreadsheet.h"
#include "ramzor3View.h"
#include "simulationparamsdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CRamzor3App theApp;
/////////////////////////////////////////////////////////////////////////////
// CRamzor3View

IMPLEMENT_DYNCREATE(CRamzor3View, CFormView)

BEGIN_MESSAGE_MAP(CRamzor3View, CFormView)
	//{{AFX_MSG_MAP(CRamzor3View)
	ON_CBN_SELCHANGE(IDC_CBO_LANE1_ARR5, OnSelchangeCboLane1Arr5)
	ON_BN_CLICKED(IDC_BTN_DEFAULTS, OnBtnDefaults)
	ON_BN_CLICKED(IDC_LEARN, OnLearn)
	ON_BN_CLICKED(IDC_SIMULATE, OnSimulate)
	ON_BN_CLICKED(IDC_BTN_VIEW, OnBtnView)
	ON_BN_CLICKED(IDC_BTN_RESET, OnBtnReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRamzor3View construction/destruction

CRamzor3View::CRamzor3View()
	: CFormView(CRamzor3View::IDD),
	m_bFirstTime(true)
{
		
}

CRamzor3View::~CRamzor3View()
{
}

void CRamzor3View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRamzor3View)
	DDX_Control(pDX, IDC_CBO_LANE1_DEP, m_cboLane1Dep);
	DDX_Control(pDX, IDC_CBO_LANE1_ARR, m_cboLane1Arr);
	DDX_Control(pDX, IDC_CBO_LANE8_DEP, m_cboLane8Dep);
	DDX_Control(pDX, IDC_CBO_LANE7_DEP, m_cboLane7Dep);
	DDX_Control(pDX, IDC_CBO_LANE6_DEP, m_cboLane6Dep);
	DDX_Control(pDX, IDC_CBO_LANE5_DEP, m_cboLane5Dep);
	DDX_Control(pDX, IDC_CBO_LANE4_DEP, m_cboLane4Dep);
	DDX_Control(pDX, IDC_CBO_LANE3_DEP, m_cboLane3Dep);
	DDX_Control(pDX, IDC_CBO_LANE2_DEP, m_cboLane2Dep);
	DDX_Control(pDX, IDC_CBO_LANE8_ARR, m_cboLane8Arr);
	DDX_Control(pDX, IDC_CBO_LANE7_ARR, m_cboLane7Arr);
	DDX_Control(pDX, IDC_CBO_LANE6_ARR, m_cboLane6Arr);
	DDX_Control(pDX, IDC_CBO_LANE5_ARR, m_cboLane5Arr);
	DDX_Control(pDX, IDC_CBO_LANE4_ARR, m_cboLane4Arr);
	DDX_Control(pDX, IDC_CBO_LANE3_ARR, m_cboLane3Arr);
	DDX_Control(pDX, IDC_CBO_LANE2_ARR, m_cboLane2Arr);
	DDX_Text(pDX, IDC_EDIT_LEARNING_DAYS, m_nLearningDays);
	DDV_MinMaxInt(pDX, m_nLearningDays, 0, 10000);
	//}}AFX_DATA_MAP
}

BOOL CRamzor3View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}
	
void CRamzor3View::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	
	if (m_bFirstTime)
	{
	    CombosArrivalArray.SetSize(NUM_OF_LANES);
		CombosDepartureArray.SetSize(NUM_OF_LANES);
		CString strLastJuncSettings, strDefaultTrafficParams; 
	
		
		
		LoadDefaults(strLastJuncSettings, strDefaultTrafficParams);
		
		
		CombosArrivalArray[0] = &m_cboLane1Arr;
		CombosArrivalArray[1] = &m_cboLane2Arr;
		CombosArrivalArray[2] = &m_cboLane3Arr;
		CombosArrivalArray[3] = &m_cboLane4Arr;
		CombosArrivalArray[4] = &m_cboLane5Arr;
		CombosArrivalArray[5] = &m_cboLane6Arr;
		CombosArrivalArray[6] = &m_cboLane7Arr;
		CombosArrivalArray[7] = &m_cboLane8Arr;

		CombosDepartureArray[0] = &m_cboLane1Dep;
		CombosDepartureArray[1] = &m_cboLane2Dep;
		CombosDepartureArray[2] = &m_cboLane3Dep;
		CombosDepartureArray[3] = &m_cboLane4Dep;
		CombosDepartureArray[4] = &m_cboLane5Dep;
		CombosDepartureArray[5] = &m_cboLane6Dep;
		CombosDepartureArray[6] = &m_cboLane7Dep;
		CombosDepartureArray[7] = &m_cboLane8Dep;
		
		m_cboLane8Dep.SetCurSel(strLastJuncSettings.GetAt(7)-'0');
		m_cboLane7Dep.SetCurSel(strLastJuncSettings.GetAt(6)-'0');
		m_cboLane6Dep.SetCurSel(strLastJuncSettings.GetAt(5)-'0');
		m_cboLane5Dep.SetCurSel(strLastJuncSettings.GetAt(4)-'0');
		m_cboLane4Dep.SetCurSel(strLastJuncSettings.GetAt(3)-'0');
		m_cboLane3Dep.SetCurSel(strLastJuncSettings.GetAt(2)-'0');
		m_cboLane2Dep.SetCurSel(strLastJuncSettings.GetAt(1)-'0');
		m_cboLane1Dep.SetCurSel(strLastJuncSettings.GetAt(0)-'0');

		m_cboLane8Arr.SetCurSel(strLastJuncSettings.GetAt(15)-'0');
		m_cboLane7Arr.SetCurSel(strLastJuncSettings.GetAt(14)-'0');
		m_cboLane6Arr.SetCurSel(strLastJuncSettings.GetAt(13)-'0');
		m_cboLane5Arr.SetCurSel(strLastJuncSettings.GetAt(12)-'0');
		m_cboLane4Arr.SetCurSel(strLastJuncSettings.GetAt(11)-'0');
		m_cboLane3Arr.SetCurSel(strLastJuncSettings.GetAt(10)-'0');
		m_cboLane2Arr.SetCurSel(strLastJuncSettings.GetAt(9)-'0');
		m_cboLane1Arr.SetCurSel(strLastJuncSettings.GetAt(8)-'0');
		m_bFirstTime = false;
		m_nLearningDays = 0;
		UpdateData(FALSE);
#ifdef SARSA
		GetDocument()->SetTitle("Sarsa");
#endif
#ifdef DEFAULT
		GetDocument()->SetTitle("Default");
#endif
#ifdef PROPORTION
		GetDocument()->SetTitle("Proportional");
#endif

		GetDlgItem(IDC_BTN_RESET)->EnableWindow(FALSE);
	}	
}
/////////////////////////////////////////////////////////////////////////////
// CRamzor3View diagnostics

#ifdef _DEBUG
void CRamzor3View::AssertValid() const
{
	CFormView::AssertValid();
}

void CRamzor3View::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CRamzor3Doc* CRamzor3View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRamzor3Doc)));
	return (CRamzor3Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CRamzor3View message handlers


void CRamzor3View::OnSelchangeCboLane1Arr5() 
{
	
	int i =	m_cboLane1Arr.GetCurSel();
}

void CRamzor3View::OnBtnDefaults() 
{
	
	CSimulationParamsDlg SimParamsDlg(this);
	SimParamsDlg.DoModal();
	CSimulationParams& simParams = GetDocument()->GetSimulationParams();
//	UpdateRegistry();	
}

void CRamzor3View::setArrivalArrAt(int ind, int val)
{
	((CComboBox*)CombosArrivalArray[ind])->SetCurSel(val);
}

void CRamzor3View::setDepartureArrAt(int ind, int val)
{
	((CComboBox*)CombosDepartureArray[ind])->SetCurSel(val);
}


CString CRamzor3View::GetCurrentSetting()
{
	CString currDepSettings("12345678");
	CString currArrivalSettings("12345678");
	
	for(int i = 0;i < NUM_OF_LANES;i++)
		currDepSettings.SetAt(i, (char)((((CComboBox *)CombosDepartureArray[i])->GetCurSel()+'0')));
	for(i = 0;i < NUM_OF_LANES;i++)
		currArrivalSettings.SetAt(i, (char)((((CComboBox *)CombosArrivalArray[i])->GetCurSel()+'0')));
	return currDepSettings + currArrivalSettings;

}


void CRamzor3View::LoadDefaults(CString& strLastJuncSettings, CString& strDefaultTrafficParams)
{
	CString strDefault, strDefault1, strDefault2, strDefault3, strDefault4, strDefault5, strDefault6, strDefault7, strDefault8;
	strDefault.Format("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d",0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2);
	strDefault1.Format("%f", HIGH_ARR);
	strDefault2.Format("%f", MED_ARR);
	strDefault3.Format("%f", LOW_ARR);
	strDefault4.Format("%f", HIGH_DEP);
	strDefault5.Format("%f", MED_DEP);
	strDefault6.Format("%f", LOW_DEP);
	strDefault7.Format("%f", 1.0);
	strDefault8.Format("%f", 1.0);
	
	strLastJuncSettings = theApp.GetProfileString(strSettings, strJunction,strDefault); 
	CString str1 = theApp.GetProfileString(strSettings,  strHighArr, strDefault1);
	CString str2 = theApp.GetProfileString(strSettings,  strMedArr, strDefault2);
	CString str3 = theApp.GetProfileString(strSettings,  strLowArr, strDefault3);
	CString str4 = theApp.GetProfileString(strSettings,  strHighDep, strDefault4);
	CString str5 = theApp.GetProfileString(strSettings,  strMedDep, strDefault5);
	CString str6 = theApp.GetProfileString(strSettings,  strLowDep, strDefault6);
	CString str7 = theApp.GetProfileString(strSettings,  strNumOfCarsPow, strDefault7);
	CString str8 = theApp.GetProfileString(strSettings,  strWaitTimePow, strDefault8);
	
	CSimulationParams& simParams = GetDocument()->GetSimulationParams();
	
	simParams.m_HighArrival		 = atof(str1.GetBuffer(0));
	simParams.m_MediumArrival	 = atof(str2.GetBuffer(0));
	simParams.m_LowArrival		 = atof(str3.GetBuffer(0));
	simParams.m_HighDeparture	 = atof(str4.GetBuffer(0));
	simParams.m_MediumDeparture	 = atof(str5.GetBuffer(0));
	simParams.m_LowDeparture	 = atof(str6.GetBuffer(0));
	simParams.m_NumOfCarsPow     = atof(str7.GetBuffer(0));
	simParams.m_WaitTimePow      = atof(str8.GetBuffer(0));
}




void CRamzor3View::OnLearn() 
{
	//save current simulation values to registry
	CString strCurrentSettings(GetCurrentSetting());
	theApp.WriteProfileString(strSettings,strJunction,	strCurrentSettings);
	GetDlgItem(IDC_BTN_RESET)->EnableWindow(TRUE);
	UpdateData(TRUE);
	time_t beforeSim, afterSim;
	time(&beforeSim);
	RunSimulation(m_nLearningDays*SECONDS_IN_DAY, true);
	time(&afterSim);
	CTimeSpan simTime(afterSim - beforeSim);
	CString strSecs; 
	strSecs.Format("%d",simTime.GetTotalSeconds());
	CString strSimTime = "Simulation was "+strSecs+" seconds";
	AfxMessageBox(strSimTime);

	
}

void CRamzor3View::OnSimulate() 
{
	GetDlgItem(IDC_BTN_RESET)->EnableWindow(TRUE);
	UpdateData(TRUE);
	RunSimulation(SECONDS_IN_DAY ,true);//second parameter is whether to save results
	
}
void  CRamzor3View::RunSimulation(int nNumOfSecs,bool bIsSaving)
{
	CRamzor3Doc* pDoc = GetDocument();
	UpdateSimParams();
	(pDoc->GetSimulationManager()).Simulate(nNumOfSecs,bIsSaving);
	
}

void   CRamzor3View::UpdateSimParams()
{
    CRamzor3Doc* pDoc = GetDocument();
    CSimulationParams& SimParams = pDoc->GetSimulationParams();
	
	for (int i = 0; i < NUM_OF_LANES; i++)
	{
		int ArrivalRate = ((CComboBox*)CombosArrivalArray[i])->GetCurSel();
		int DepartureRate = ((CComboBox*)CombosDepartureArray[i])->GetCurSel();

		SimParams.SetArrivalArray(ArrivalRate, i);
		SimParams.SetDepartureArray(DepartureRate, i);
	}

	pDoc->UpdateRewardParams();
#ifdef PROPORTION
	((CProportionalPolicy*)(pDoc->GetPolicy()))->SetLanesTraffic();
#endif
}

void CRamzor3View::OnBtnView() 
{
	CSpreadSheet  SSheetDlg;
	SSheetDlg.DoModal();
	
}


void CRamzor3View::OnBtnReset() 
{
	GetDocument()->ResetSimulation();
		
}
