/* MainPage.xaml.cpp - Implementation of the MainPage class.
* Copyright (C) 2014 Paulo Pinto
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/

#include "pch.h"
#include <sstream>

#include "Map.h"
#include "MapWidget.h"
#include "WindowsRender.h"
#include "AStarViewModel.h"

#include "MainPage.xaml.h"

using namespace AStarDemoWin;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace concurrency;


// no need to expose it in the header
static ViewModel::AStarViewModel^ viewModel;

MainPage::MainPage()
{
	InitializeComponent();

	viewModel = ref new ViewModel::AStarViewModel();
	DataContext = viewModel;

	// Now bind the events

	Window::Current->CoreWindow->GetForCurrentThread()->Activated += ref new TypedEventHandler<CoreWindow ^, WindowActivatedEventArgs ^>(viewModel, &ViewModel::AStarViewModel::OnActivated);
	GoBtn->Click += ref new RoutedEventHandler(viewModel, &ViewModel::AStarViewModel::Search_Click);
	ClearBtn->Click += ref new RoutedEventHandler(viewModel, &ViewModel::AStarViewModel::ClearMap_Click);
	SizeChanged += ref new SizeChangedEventHandler(viewModel, &ViewModel::AStarViewModel::Page_SizeChanged);
	MapImage->DoubleTapped += ref new DoubleTappedEventHandler(viewModel, &ViewModel::AStarViewModel::MapImage_DoubleTapped);
}


/**
 * Presents a file chooser whent he user clicks the respecite application button.
 */
void AStarDemoWin::MainPage::OpenMap_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	// to reduce typing
	using namespace Windows::Storage;
	using namespace Windows::Storage::Pickers;

	auto openPicker = ref new FileOpenPicker();
	openPicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;

	openPicker->FileTypeFilter->Clear();
	openPicker->FileTypeFilter->Append(".txt");

	create_task(openPicker->PickSingleFileAsync())
		.then([this](StorageFile^ file)
	{
		if (file == nullptr)
		{
			cancel_current_task();
		}

		viewModel->LoadFile(file);
	});
 }