#include "window.h"
#include <curl/curl.h>
#include "page.h"
#include "config.h"
#include "anime.h"
#include "statistics.h"
#include "now_playing.h"
#include "16x16/document-list.png.h"
#include "16x16/film.png.h"
#include "16x16/chart.png.h"
#include "16x16/clock-history-frame.png.h"
#include "16x16/magnifier.png.h"
#include "16x16/calendar.png.h"
#include "16x16/feed.png.h"
#include "24x24/arrow-circle-double-135.png.h"
#include "24x24/folder-open.png.h"
#include "24x24/gear.png.h"

Config Weeaboo::config = Config();

WeeabooFrame::WeeabooFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
					: wxFrame(NULL, wxID_ANY, title, pos, size) {
	/* ---- Menu Bar ---- */
	wxMenu* library_folders_submenu = new wxMenu;
	library_folders_submenu->Append(ID_AddLibraryFolder, "&Add library folder");
	library_folders_submenu->Append(ID_ScanLibraryFolders, "&Rescan library folders");

	wxMenu* file_menu = new wxMenu;
	file_menu->AppendSubMenu(library_folders_submenu, "&Library folders");
	file_menu->Append(ID_SyncAnimeList, "&Sync anime list\tCtrl+S");
	file_menu->AppendSeparator();
	file_menu->Append(ID_PlayNextEpisode, "Play &next episode\tCtrl+N");
	file_menu->Append(ID_PlayRandomEpisode, "Play &random episode\tCtrl+R");
	file_menu->AppendSeparator();
	file_menu->Append(wxID_EXIT);

	wxMenu* help_menu = new wxMenu;
	help_menu->Append(wxID_ABOUT);

	wxMenuBar* menu_bar = new wxMenuBar;
	menu_bar->Append(file_menu, "&File");
	menu_bar->Append(help_menu, "&Help");

	SetMenuBar(menu_bar);

	/* Toolbar */
	wxToolBar* top_toolbar = CreateToolBar();
	top_toolbar->SetToolBitmapSize(wxSize(24,24));
	top_toolbar->AddTool(ID_ToolbarSync, wxT("Sync"), wxBITMAP_PNG_FROM_DATA(arrow_circle_double_135));
	top_toolbar->Realize();

	/* ---- Sidebar ---- */
	/* This first panel is only for the sizer... */
	wxPanel* left_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(140, 600), wxTAB_TRAVERSAL, wxPanelNameStr);
	wxPanel* left_panel_inside = new wxPanel(left_panel, wxID_ANY, wxPoint(6, 6), wxSize(128, 588), wxTAB_TRAVERSAL);
	wxToolBar* left_toolbar = new wxToolBar(left_panel_inside, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_FLAT | wxTB_VERTICAL | wxTB_HORZ_TEXT | wxTB_NODIVIDER);
	left_toolbar->SetMargins(6, 6);
	left_toolbar->SetToolBitmapSize(wxSize(16,16));
	left_toolbar->AddRadioTool(ID_NowPlaying, "Now Playing", wxBITMAP_PNG_FROM_DATA(film));
	left_toolbar->AddRadioTool(ID_AnimeList,  "Anime List",  wxBITMAP_PNG_FROM_DATA(document_list));
	left_toolbar->AddRadioTool(ID_History,    "History",     wxBITMAP_PNG_FROM_DATA(clock_history_frame));
	left_toolbar->AddRadioTool(ID_Statistics, "Statistics",  wxBITMAP_PNG_FROM_DATA(chart));
	left_toolbar->AddRadioTool(ID_Search,     "Search",      wxBITMAP_PNG_FROM_DATA(magnifier));
	left_toolbar->AddRadioTool(ID_Seasons,    "Seasons",     wxBITMAP_PNG_FROM_DATA(calendar));
	left_toolbar->AddRadioTool(ID_Torrents,   "Torrents",    wxBITMAP_PNG_FROM_DATA(feed));

	/* ---- Initialize our pages ---- */
	wxPanel* right_panel = new wxPanel(this, wxID_ANY, wxPoint(140, 0), wxSize(460, 600), wxTAB_TRAVERSAL, wxPanelNameStr);
	now_playing = new NowPlaying(&pages[PAGE_NOW_PLAYING], right_panel);
	anime_list = new AnimeListPage(&pages[PAGE_ANIME_LIST], right_panel);
	anime_list->SyncAnimeList();
	anime_list->LoadAnimeList(this);
	statistics = new Statistics(&pages[PAGE_STATISTICS], right_panel);
	
	status.current_page = PAGE_ANIME_LIST; // The below function depends on this value being set?
	set_page(PAGE_ANIME_LIST);
	left_toolbar->ToggleTool(ID_AnimeList, true);
	left_toolbar->Realize();

	wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
	sizer->Add(left_panel, 0, wxEXPAND, 10);
	sizer->Add(right_panel, 1, wxEXPAND, 10);
	sizer->SetMinSize(600, 600);
	this->SetSizer(sizer);
	sizer->SetSizeHints(this);

}

bool Weeaboo::OnInit() {
	config.Load();
	if (curl_global_init(CURL_GLOBAL_DEFAULT) != 0) {
		wxMessageBox("libcurl failed to initialize!",
				 "Error", wxOK | wxICON_ERROR);
	}
	wxSystemOptions::SetOption("msw.remap",
		wxSystemOptions::HasOption("msw.remap")
						 ? wxSystemOptions::GetOptionInt("msw.remap")
						 : wxDisplayDepth() <= 8 ? 1 : 2
	);
	wxImage::AddHandler(new wxPNGHandler);
	frame = new WeeabooFrame("Weeaboo", wxPoint(50, 50), wxSize(450, 340));
	frame->Show(true);
	return true;
}

#define TOOLBAR_HANDLER(name, page) \
void WeeabooFrame::name(wxCommandEvent& event) { \
	set_page(page); \
}
TOOLBAR_HANDLER(OnNowPlaying, PAGE_NOW_PLAYING)
TOOLBAR_HANDLER(OnAnimeList,  PAGE_ANIME_LIST)
TOOLBAR_HANDLER(OnHistory,    PAGE_HISTORY)
TOOLBAR_HANDLER(OnStatistics, PAGE_STATISTICS)
TOOLBAR_HANDLER(OnSearch,     PAGE_SEARCH)
TOOLBAR_HANDLER(OnSeasons,    PAGE_SEASONS)
TOOLBAR_HANDLER(OnTorrents,   PAGE_TORRENTS)
#undef TOOLBAR_HANDLER

void WeeabooFrame::OnClose(wxCloseEvent& event) {
	Weeaboo::config.Save();
	curl_global_cleanup();
	delete anime_list;
	event.Skip();
}

void WeeabooFrame::OnExit(wxCommandEvent& event) {
	Close(true);
}

void WeeabooFrame::OnAbout(wxCommandEvent& event) {
	wxMessageBox("To be written",
				 "About Weeaboo", wxOK | wxICON_INFORMATION);
}

void WeeabooFrame::OnAddFolder(wxCommandEvent& event) {
	wxLogMessage("OnAddFolder");
}

void WeeabooFrame::OnScanFolders(wxCommandEvent& event) {
	wxLogMessage("OnScanFolders");
}

void WeeabooFrame::OnNextEpisode(wxCommandEvent& event) {
	wxLogMessage("OnNextEpisode");
}

void WeeabooFrame::OnSyncList(wxCommandEvent& event) {
	anime_list->SyncAnimeList();
	anime_list->LoadAnimeList(this);
}

void WeeabooFrame::OnRandomEpisode(wxCommandEvent& event) {
	wxLogMessage("OnRandomEpisode");
}

AnimeListPage* WeeabooFrame::GetAnimeList() {
	return anime_list;
}
