// AddTrain.cpp : implementation file
//

#include "stdafx.h"
#include "Railroad.h"
#include "AddTrain.h"

extern RailroadStation ScheduleMain;
extern RailroadStation ScheduleView;


// CAddTrain dialog

IMPLEMENT_DYNAMIC(CAddTrain, CDialog)
CAddTrain::CAddTrain(CWnd* pParent /*=NULL*/)
	: CDialog(CAddTrain::IDD, pParent)
{
}

CAddTrain::~CAddTrain()
{
}

void CAddTrain::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, m_TrainNum);
	DDX_Control(pDX, IDC_EDIT2, m_WayNum);
	DDX_Control(pDX, IDC_DATETIMEPICKER1, m_Arrival);
	DDX_Control(pDX, IDC_DATETIMEPICKER2, m_Depart);
	DDX_Control(pDX, IDC_EDIT3, m_Source);
	DDX_Control(pDX, IDC_EDIT4, m_Dest);
}


BEGIN_MESSAGE_MAP(CAddTrain, CDialog)
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
END_MESSAGE_MAP()


// CAddTrain message handlers

void CAddTrain::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	char buf[31];
	TrainInfo ti;
	try
	{
		m_TrainNum.GetWindowText(buf, 20);
		UINT TrainNum = atoi(buf);
		if(TrainNum >= 1 && TrainNum <= 999)
		{
			ti.TrainNum = TrainNum;
		}
		else
		{
			throw "Train # must be from 1 to 999";
		}
		m_WayNum.GetWindowText(buf, 20);
		UINT WayNum = atoi(buf);
		if(WayNum >= 1 && WayNum <= 30)
		{
			ti.Way = WayNum;
		}
		else
		{
			throw "Way # must be from 1 to 30";
		}
		CTime time;
		m_Arrival.GetTime(time);
		ti.TimeArr = time.GetHour()*60 + time.GetMinute();
		m_Depart.GetTime(time);
		ti.TimeDep = time.GetHour()*60 + time.GetMinute();
		if(m_Source.GetLine(0, buf, 30) > 0)
		{
			ti.StationSour = new char[30];
			strcpy(ti.StationSour, buf);
		}
		else
		{
			throw "Specify the source station, please";
		}
		if(m_Dest.GetLine(0, buf, 30) > 0)
		{
			ti.StationDest = new char[30];
			strcpy(ti.StationDest, buf);
		}
		else
		{
			throw "Specify the destination station, please";
		}
		ScheduleMain.AddTrain(ti);
		EndDialog(0);
	}
	catch(char *msg)
	{
		MessageBox(msg);
	}
}

BOOL CAddTrain::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	m_TrainNum.SetLimitText(3);
	m_WayNum.SetLimitText(3);
	m_Arrival.SetFormat("H:mm");
	m_Depart.SetFormat("H:mm");
	m_Source.SetLimitText(30);
	m_Dest.SetLimitText(30);

	m_TrainNum.SetWindowText("0");
	m_WayNum.SetWindowText("0");
	SYSTEMTIME t = {1800,1,0,1,0,0,0};
	m_Source.SetWindowText("");
	m_Dest.SetWindowText("");

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CAddTrain::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	EndDialog(-1);
}
