// RailroadDlg.cpp : implementation file
//

#include "stdafx.h"
#include <Commctrl.h>
#include "Railroad.h"
#include "RailroadDlg.h"
#include "c.cpp"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


RailroadStation ScheduleMain;
RailroadStation ScheduleView;



// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CRailroadDlg dialog



CRailroadDlg::CRailroadDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRailroadDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRailroadDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Control(pDX, IDC_EDIT2, m_TrainNumRest);
	DDX_Control(pDX, IDC_EDIT5, m_WayNumRest);
	DDX_Control(pDX, IDC_DATETIMEPICKER2, m_Arr1);
	DDX_Control(pDX, IDC_DATETIMEPICKER3, m_Arr2);
	DDX_Control(pDX, IDC_DATETIMEPICKER4, m_Dep1);
	DDX_Control(pDX, IDC_DATETIMEPICKER5, m_Dep2);
	DDX_Control(pDX, IDC_EDIT6, m_SourceRest);
	DDX_Control(pDX, IDC_EDIT7, m_DestRest);
	DDX_Control(pDX, IDC_BUTTON4, m_WayRestrict);
	DDX_Control(pDX, IDC_BUTTON9, m_ShowAll);
}

BEGIN_MESSAGE_MAP(CRailroadDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON9, OnBnClickedButton9)
	ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnBnClickedButton8)
	ON_BN_CLICKED(IDC_CLOSE, OnBnClickedClose)
END_MESSAGE_MAP()


// CRailroadDlg message handlers

BOOL CRailroadDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	
	LVCOLUMN column;
	column.mask = LVCF_TEXT | LVCF_WIDTH;
	column.cx = 50;
	column.pszText = "#";
	m_List.InsertColumn(0, &column);

	column.mask = LVCF_TEXT | LVCF_WIDTH;
	column.cx = 50;
	column.pszText = "Way";
	m_List.InsertColumn(1, &column);

	column.mask = LVCF_TEXT | LVCF_WIDTH;
	column.cx = 80;
	column.pszText = "Arrives";
	m_List.InsertColumn(2, &column);

	column.mask = LVCF_TEXT | LVCF_WIDTH;
	column.cx = 80;
	column.pszText = "Departs";
	m_List.InsertColumn(3, &column);

	column.mask = LVCF_TEXT | LVCF_WIDTH;
	column.cx = 120;
	column.pszText = "Goes From";
	m_List.InsertColumn(4, &column);

	column.mask = LVCF_TEXT | LVCF_WIDTH;
	column.cx = 120;
	column.pszText = "Goes To";
	m_List.InsertColumn(5, &column);

	m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	m_ShowAll.EnableWindow(false);
	m_ShowAll.SetWindowText("All is shown");

	m_TrainNumRest.SetLimitText(3);
	m_WayNumRest.SetLimitText(3);
	m_Arr1.SetFormat("H:mm");
	m_Arr2.SetFormat("H:mm");
	m_Dep1.SetFormat("H:mm");
	m_Dep2.SetFormat("H:mm");
	m_SourceRest.SetLimitText(30);
	m_DestRest.SetLimitText(30);

	m_TrainNumRest.SetWindowText("0");
	m_WayNumRest.SetWindowText("0");
	SYSTEMTIME t;
	GetSystemTime(&t);
	t.wHour = 0;
	t.wMinute = 0;
	m_Arr1.SetTime(&t);
	m_Arr2.SetTime(&t);
	m_Dep1.SetTime(&t);
	m_Dep2.SetTime(&t);
	m_SourceRest.SetWindowText("");
	m_DestRest.SetWindowText("");

	TrainInfo ti;
	ti.TrainNum = 65;
	ti.Way = 5;
	ti.TimeArr = 125;
	ti.TimeDep = 199;
	ti.StationSour = "Москва";
	ti.StationDest = "Питер";

	ScheduleMain.AddTrain(ti);
	//InsertTrainInfo(ti);

	ti.TrainNum = 33;
	ti.Way = 1;
	ti.TimeArr = 453;
	ti.TimeDep = 1098;
	ti.StationSour = "Томск";
	ti.StationDest = "Новороссийск";

	ScheduleMain.AddTrain(ti);

	ti.TrainNum = 23;
	ti.Way = 1;
	ti.TimeArr = 233;
	ti.TimeDep = 345;
	ti.StationSour = "Питер";
	ti.StationDest = "Париж";

	//InsertTrainInfo(ti);
	ScheduleMain.AddTrain(ti);

	ti.TrainNum = 587;
	ti.Way = 3;
	ti.TimeArr = 564;
	ti.TimeDep = 763;
	ti.StationSour = "Адлер";
	ti.StationDest = "Мурманск";

	//InsertTrainInfo(ti);
	ScheduleMain.AddTrain(ti);

	ti.TrainNum = 1;
	ti.Way = 5;
	ti.TimeArr = 1400;
	ti.TimeDep = 29;
	ti.StationSour = "Лондон";
	ti.StationDest = "Магадан";

	//InsertTrainInfo(ti);
	ScheduleMain.AddTrain(ti);

	ScheduleView = ScheduleMain;

	RefreshView(ScheduleView);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CRailroadDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRailroadDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRailroadDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

bool CRailroadDlg::InsertTrainInfo(TrainInfo ti)
{
	LVITEM item;
	char str[50];

	item.iItem = 0;

	item.mask = LVIF_TEXT;

	item.iSubItem = 0;
	item.pszText = itoa(ti.TrainNum, str, 10);
	m_List.InsertItem(&item);

	item.iSubItem = 1;
	item.pszText = itoa(ti.Way, str, 10);
	m_List.SetItem(&item);

	item.iSubItem = 2;
	item.pszText = GetStrFromTime(str, ti.TimeArr);
	m_List.SetItem(&item);

	item.iSubItem = 3;
	item.pszText = GetStrFromTime(str, ti.TimeDep);
	m_List.SetItem(&item);

	item.iSubItem = 4;
	item.pszText = ti.StationSour;
	m_List.SetItem(&item);

	item.iSubItem = 5;
	item.pszText = ti.StationDest;
	m_List.SetItem(&item);

	return false;
}

char* CRailroadDlg::GetStrFromTime(char* buf, UINT time)
{
	int h = time / 60;
	int m = time % 60;
	sprintf(buf, "%d:%02d", h, m);
	return buf;
}

void CRailroadDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	m_AddTrainDlg = new CAddTrain();
	if(m_AddTrainDlg->DoModal() == 0)
	{
		ScheduleView = ScheduleMain;
		RefreshView(ScheduleView);
		m_ShowAll.EnableWindow(false);
		m_ShowAll.SetWindowText("All is shown");
	}
	else
	{
		;
	}
}

void CRailroadDlg::RefreshView(RailroadStation view)
{
	m_List.DeleteAllItems();
	TrainInfoList *til = view.Schedule;
	TrainInfo ti;
	while(til != NULL)
	{
		ti.TrainNum = til->TrainNum;
		ti.Way = til->Way;
		ti.TimeArr = til->TimeArr;
		ti.TimeDep = til->TimeDep;
		ti.StationSour = til->StationSour;
		ti.StationDest = til->StationDest;
		InsertTrainInfo(ti);
		til = til->next;
	}
}


void CRailroadDlg::DelTrains(void)
{
	POSITION pos;
	LVITEM it;
	it.mask = LVIF_TEXT;
	it.iSubItem = 0;
	it.cchTextMax = 29;
	it.pszText = new char[30];
	try
	{
		pos = m_List.GetFirstSelectedItemPosition();
		if(pos == NULL)
			return;
		while(pos)
		{
			it.iItem = m_List.GetNextSelectedItem(pos);
			m_List.GetItem(&it);
			ScheduleMain.DelTrain(atoi(it.pszText));
		}
		ScheduleView = ScheduleMain;
		RefreshView(ScheduleView);
		m_ShowAll.EnableWindow(false);
		m_ShowAll.SetWindowText("All is shown");
	}
	catch(char *msg)
	{
		MessageBox(msg);
	}
}

void CRailroadDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	DelTrains();
}

void CRailroadDlg::OnBnClickedButton9()
{
	// TODO: Add your control notification handler code here
	ScheduleView = ScheduleMain;
	RefreshView(ScheduleView);
	m_ShowAll.EnableWindow(false);
	m_ShowAll.SetWindowText("All is shown");
}

void CRailroadDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
	char str[5];
	m_TrainNumRest.GetWindowText(str, 4);
	ScheduleView.GetTrainByNum(atoi(str));
	RefreshView(ScheduleView);
	m_ShowAll.EnableWindow(true);
	m_ShowAll.SetWindowText("Show All");
}

void CRailroadDlg::OnBnClickedButton4()
{
	// TODO: Add your control notification handler code here
	char str[5];
	m_WayNumRest.GetWindowText(str, 4);
	ScheduleView.GetTrainByWay(atoi(str));
	RefreshView(ScheduleView);
	m_ShowAll.EnableWindow(true);
	m_ShowAll.SetWindowText("Show All");
}

void CRailroadDlg::OnBnClickedButton5()
{
	// TODO: Add your control notification handler code here
	CTime time1, time2;
	m_Arr1.GetTime(time1);
	m_Arr2.GetTime(time2);
	ScheduleView.FilterArr(time1.GetHour()*60 + time1.GetMinute(), time2.GetHour()*60 + time2.GetMinute());
	RefreshView(ScheduleView);
	m_ShowAll.EnableWindow(true);
	m_ShowAll.SetWindowText("Show All");
}

void CRailroadDlg::OnBnClickedButton6()
{
	// TODO: Add your control notification handler code here
	CTime time1, time2;
	m_Dep1.GetTime(time1);
	m_Dep2.GetTime(time2);
	ScheduleView.FilterDep(time1.GetHour()*60 + time1.GetMinute(), time2.GetHour()*60 + time2.GetMinute());
	RefreshView(ScheduleView);
	m_ShowAll.EnableWindow(true);
	m_ShowAll.SetWindowText("Show All");
}

void CRailroadDlg::OnBnClickedButton7()
{
	// TODO: Add your control notification handler code here
	char str[31];
	m_SourceRest.GetWindowText(str, 30);
	ScheduleView.FilterSour(str);
	RefreshView(ScheduleView);
	m_ShowAll.EnableWindow(true);
	m_ShowAll.SetWindowText("Show All");
}

void CRailroadDlg::OnBnClickedButton8()
{
	// TODO: Add your control notification handler code here
	char str[31];
	m_DestRest.GetWindowText(str, 30);
	ScheduleView.FilterDest(str);
	RefreshView(ScheduleView);
	m_ShowAll.EnableWindow(true);
	m_ShowAll.SetWindowText("Show All");
}


void CRailroadDlg::OnBnClickedClose()
{
	// TODO: Add your control notification handler code here
	EndDialog(0);
}
