//
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//

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

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

#include "MainPage.xaml.h"
#include "ContinuationManager.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 Windows::Storage::Pickers;
using namespace Windows::ApplicationModel::Activation;
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);
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.  The Parameter
/// property is typically used to configure the page.</param>
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
}

/**
* 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");
	openPicker->PickSingleFileAndContinue();
}

/// <summary>
/// Handle the returned files from file picker
/// This method is triggered by ContinuationManager based on ActivationKind
/// </summary>
/// <param name="args">File open picker continuation activation argment. It cantains the list of files user selected with file open picker </param>
void AStarDemoWin::MainPage::ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs^ args)
{
	using namespace Windows::Storage;
	StorageFile^ file;

	if (args->Files->Size > 0)
	{
		// Open a stream for the selected file.
		file = args->Files->GetAt(0);

		viewModel->LoadFile(file);
	}
}