#include "select_vertex_rectangle_tool.h"
#include "../geometry/point_set.h"
#include "paint_canvas.h"
#include "../widget/logger.h"

using namespace qglviewer;



namespace Tl {

	SelectVertexRectangleTool::SelectVertexRectangleTool(PaintCanvas* canvas)
		: SelectTool(canvas)
	{
		initialize_select_buffer();
		set_select_button(SBT_LEFT);
	}


	int SelectVertexRectangleTool::needed_buffer_size() const {
		return (point_set()->nb_vertices() * 4);
	}


 	void SelectVertexRectangleTool::pre_highlight_operation() {
 		if (point_set()) {
 			Vertex** vertices = point_set()->vertices();
 			int num = point_set()->nb_vertices();
 			for (int i=0; i<num; ++i) {
 				Vertex* v = vertices[i];
 				if (v->is_highlighted())
 					v->set_highlighted(false);
 			}	
 		}
 	}


	PointSet* SelectVertexRectangleTool::point_set() const {
		return canvas()->pointSet();
	}

	void SelectVertexRectangleTool::draw_with_names() const {
		Vertex** vertices = point_set()->vertices();
		int num = point_set()->nb_vertices();
		for (int i=0; i<num; ++i) {
			vertices[i]->draw_with_name(i);
		}
	}

	void SelectVertexRectangleTool::post_selection_operation(bool b/* = true*/) {
		Vertex** vertices = point_set()->vertices();

		for (unsigned int i=0; i<selected_object_indices_.size(); ++i) {
			int index = selected_object_indices_[i];
			Vertex* v = vertices[index];
			v->set_selected(b);
		}

		if (b)
			Logger::output(title()) << selected_object_indices_.size() << " points selected." << Logger::endl();
		else
			Logger::output(title()) << selected_object_indices_.size() << " points deselected." << Logger::endl();
	}


}