/*
 *
 *  $Id: previewpanel.cpp $
 *  Ginkgo CADx Project
 *
 *  Copyright 2008-12 MetaEmotion S.L. All rights reserved.
 *  http://ginkgo-cadx.com
 *
 *  This file is licensed under LGPL v3 license.
 *  See License.txt for details
 *
 *
 */
#include <wx/dc.h>
#include <wx/sizer.h>
#include <wx/event.h>
#include <wx/bitmap.h>
#include <wx/menu.h>
#include <wx/ginkgostyle/ginkgostyle.h>
#include <wx/aui/auibar.h>
#include <api/controllers/icontroladorlog.h>
#include <api/controllers/ieventscontroller.h>
#include <api/globals.h>
#include <resources/ginkgoresourcesmanager.h>

#include "previewpanel.h"
#include "leafpanel.h"
#include "firstlevelpanel.h"



#define ID_MENUS 1401
#define ID_EJECUTAR 1
#define ID_TITULO 0

#define MIN_WIDTH 100


namespace GNC {
	namespace GUI {
		PreviewPanel::PreviewPanel(GNC::GCS::IVista* pView, wxWindow* parent) : PreviewPanelBase(parent),
			INodoHistorial(NULL),
			m_pView(pView),
			m_dirty(true)
		{
			//titulo
			m_pPreviewTitle->GetButtonBar()->AddTool(ID_EJECUTAR,_("Hide"),GinkgoResourcesManager::History::GetIcoPlegar(),_("Hide"), wxITEM_NORMAL);
			m_pPreviewTitle->GetButtonBar()->Connect(ID_EJECUTAR,wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( PreviewPanel::OnHidePreviewsClick),NULL,this);
			m_pPreviewTitle->Realize();

			//se conecta el evento Ginkgo
			GNC::GCS::IEventsController::Instance()->Registrar(this, GNC::GUI::Events::EventoSetFocusHistorial(m_pView));
			GNC::GCS::IEventsController::Instance()->Registrar(this, GNC::GUI::Events::EventoSeleccionarHistorial(m_pView));
			GNC::GCS::IEventsController::Instance()->Registrar(this, GNC::GUI::Events::EventoLayoutHistorial(m_pView));

			this->Connect(wxEVT_IDLE, wxIdleEventHandler(PreviewPanel::OnIdle));

			m_pSelected = NULL;

			SetSize(MIN_WIDTH, -1);
			SetMinSize(wxSize(MIN_WIDTH, -1));
			SetMaxSize(wxSize(MIN_WIDTH, -1));
			Layout();
		}

		PreviewPanel::~PreviewPanel() 
		{			
		}

		//interfaz modelo dicom
		void PreviewPanel::LoadModel(IPreviewNode* pNode )
		{
			m_pPreviewTitle->SetToolTip(wxString::FromUTF8(pNode->GetLabel().c_str()));
			m_pPanelVertical->SetToolTip(wxString::FromUTF8(pNode->GetLabel().c_str()));
			m_pPanelPreviews->GetSizer()->Clear(true);
		
			int leafIndex = 0;
			for (IPreviewNode::TListNodes::iterator it = pNode->children.begin(); it != pNode->children.end(); ++it, ++leafIndex)
			{
				IPreviewLeaf* pLeaf = dynamic_cast<IPreviewLeaf*>((*it));
				if (pLeaf != NULL) {
					LeafPanel* p = new LeafPanel(m_pView, this, pLeaf, leafIndex%2); 
					m_pPanelPreviews->GetSizer()->Add(p,0,wxBOTTOM|wxEXPAND,0);
				} else {
					FirstLevelPanel* pNewLevel = new FirstLevelPanel(m_pView, this, (*it), true);
					m_pPanelPreviews->GetSizer()->Add(pNewLevel,0,wxBOTTOM|wxEXPAND,0);
				}				
			}
			GNC::GCS::IEventsController::Instance()->ProcesarEvento(new GNC::GUI::Events::EventoLayoutHistorial(m_pView));
		}

		wxWindow* PreviewPanel::GetWxWindow()
		{
			return m_pPanelPreviews;
		}

		void PreviewPanel::OnSize(wxSizeEvent &event)
		{
			m_dirty = true;
			event.Skip();
		}

		void PreviewPanel::OnHidePreviewsClick(wxCommandEvent& )
		{
			FoldUp(true);
		}

		void PreviewPanel::OnShowPreviewsClick(wxMouseEvent& )
		{
			FoldUp(false);
		}

		void PreviewPanel::FoldUp(bool plegar)
		{
			if (!plegar) {
				SetSize(MIN_WIDTH, -1);
				SetMinSize(wxSize(MIN_WIDTH, -1));
				SetMaxSize(wxSize(MIN_WIDTH, -1));
			} else {
				SetSize(20, -1);
				SetMinSize(wxSize(26, -1));
			}
			GetParent()->Layout();
			m_pPanelPreviews->Show(!plegar);
			m_pPreviewTitle->Show(!plegar);
			m_pPanelVertical->Show(plegar);
			Layout();
		}

		void PreviewPanel::PreProcesarEvento(GNC::GCS::Events::IEvent * evt, std::list<GNC::GCS::Events::IEvent*>& lista)
		{
			if (evt == NULL) {
				std::cerr << "Error: null event" << std::endl;
				return;
			}
			switch (evt->GetCodigoEvento()) {
				case ginkgoEVT_Core_HistorialSeleccionar:
				case ginkgoEVT_Core_HistorialLayout:
					{
						lista.push_back(new GNC::GUI::Events::EventoSetFocusHistorial(m_pView));
					}
					break;
				default:
					break;

			}
		}

		void PreviewPanel::ProcesarEvento(GNC::GCS::Events::IEvent *evt) 
		{
			switch(evt->GetCodigoEvento()) {
				case  ginkgoEVT_Core_HistorialSeleccionar:
					{
						GNC::GUI::Events::EventoSeleccionarHistorial* pEvtSelecc = dynamic_cast<GNC::GUI::Events::EventoSeleccionarHistorial*>(evt);
						if(pEvtSelecc != NULL) {
							GNC::GUI::ISeleccionablePreview* pSeleccionable = pEvtSelecc->GetSeleccionable();
							if(pSeleccionable != m_pSelected){
								if(m_pSelected!= NULL) {
									m_pSelected->Seleccionar(false);
								}
								if(pSeleccionable != NULL){
									pSeleccionable->Seleccionar(true);
								}
								m_pSelected = pSeleccionable;
							}
						}
					}
					break;
				case ginkgoEVT_Core_HistorialLayout:
					{
						m_dirty = true;
					}
					break;
				case ginkgoEVT_Core_SetFocus:
					{
					    #if defined(_WIN32) || defined(__WXOSX__)
						m_pPanelPreviews->SetFocus();
						#endif
					}
					break;
			}
		}

		void PreviewPanel::OnIdle (wxIdleEvent &WXUNUSED(event)) 
		{
			 /* after all changes have been done to the control
			  * we actually redraw the tree when everything is over */
			 if (!m_dirty) return;

			 m_dirty = false;
			 Freeze();
			 //adjust scroll bars...
			 for (int i = 0; i < 2; i++) {
				 Refresh();
				 if (!m_pPanelPreviews->GetSizer()->GetChildren().IsEmpty()) {
					  int xUnit, yUnit;
					  m_pPanelPreviews->GetScrollPixelsPerUnit (&xUnit, &yUnit);
					  if (yUnit == 0) yUnit = 20;
					  int y = 0;

					  int xTemp = 0, yTemp = 0;
					  wxSizerItemList listOfChildren = m_pPanelPreviews->GetSizer()->GetChildren();
					  for (wxSizerItemList::iterator it = listOfChildren.begin(); it != listOfChildren.end(); ++it) {
						  GNC::GUI::INodoHistorial* pNode = dynamic_cast<GNC::GUI::INodoHistorial*> ((*it)->GetWindow());
						  if (pNode != NULL) {
							  pNode->GetNodeSize(xTemp, yTemp);
							  y += yTemp;
						  }
					  }

					  ///////////////m_rootItem->GetSize (x, y, this);
					  y += yUnit + 2; // one more scrollbar unit + 2 pixels
					  int y_pos = GetScrollPos (wxVERTICAL);
					  m_pPanelPreviews->SetScrollbars (0, yUnit, 0, y/yUnit, 0, y_pos);
				 }else{
					  m_pPanelPreviews->SetScrollbars (0, 0, 0, 0);
				 }
			 }
			 Thaw();
		}
	};
};
