#include "broken_edge.h"
#include <math.h>

BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false)
{
  my_points=new Gnome::Art::Point[3];

  arrow=new Gnome::Canvas::Polygon(g);
  *arrow << Gnome::Canvas::Properties::fill_color("red");
  arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler));
  setPoints(p);
}

BrokenEdge::~BrokenEdge()
{
  if(arrow)delete(arrow);
}

void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
{
  bool set_arrow=false;
  //red arrow losts its position-right button
  if(!move)
    {
      if(p.size()==2)
	{
	  set_arrow=true;
	  Gnome::Canvas::Points points_with_center;
	  points_with_center.push_back(my_points[0]=p[0]);
	  points_with_center.push_back(my_points[1]=Gnome::Art::Point( (p[0].get_x()+p[1].get_x())/2+0 , (p[0].get_y()+p[1].get_y())/2 )+0 );
	  points_with_center.push_back(my_points[2]=p[1]);
	  property_points().set_value(points_with_center);
	}  
      if(p.size()==3)
	{
	  set_arrow=true;
	  property_points().set_value(p);
	  for(int i=0;i<3;i++)
	    {
	      my_points[i]=p[i];
	    }
	}
    }
  else
    {
      //arrow keeps its position-left button

      if(p.size()==2)
      	{
      	  Gnome::Canvas::Points points;
      	  my_points[0]=p[0];
      	  my_points[2]=p[1];
      	  for(int i=0;i<3;i++)
      	    {
      	      points.push_back(my_points[i]);
      	    }
      	  property_points().set_value(points);
      	}
      set_arrow=true;

      //////////////////////////////////////////////////////////////////////////////////////////////////////
      /////////// kepps shape-with angles
      //////////////////////////////////////////////////////////////////////////////////////////////////////


//       //old vector from one to the other node
//       xy<double> o_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
//       //projection of the old vector to positive x axis
//       xy<double> o_x_p2p(fabs(o_p2p.x),0);
//       //length of p2p vector
//       double o_l_p2p=sqrt( o_p2p.normSquare() );
//       if(o_p2p.x<0)
// 	{
// 	  o_l_p2p*=-1;
// 	}
//       //length of projection of p2p vector
//       double o_l_x_p2p=sqrt( o_x_p2p.normSquare() );
//       //old angle of p2p vector to the x axis
//       double o_a_p2p=acos(o_l_x_p2p/o_l_p2p);
//       if(o_p2p.y>0)
// 	{
// 	  o_a_p2p=2*M_PI-o_a_p2p;
// 	}

//       //old vector from first node to the breakpoint
//       xy<double> o_1b((my_points[1].get_x()-my_points[0].get_x()),(my_points[1].get_y()-my_points[0].get_y()));
//       //projection of the old node-breakpoint vector to positive x axis
//       xy<double> o_x_1b(fabs(o_1b.x),0);
//       //length of 1b vector
//       double o_l_1b=sqrt( o_1b.normSquare() );
//       if(o_1b.x<0)
// 	{
// 	  o_l_1b*=-1;
// 	}
//       //length of projection of 1b vector
//       double o_l_x_1b=sqrt( o_x_1b.normSquare() );
//       //old angle of 1b vector to the x axis
//       double o_a_1b=acos(o_l_x_1b/o_l_1b);
//       if(o_1b.y>0)
// 	{
// 	  o_a_1b=2*M_PI-o_a_1b;
// 	}

//       if(p.size()==2)
//       	{
// 	  set_arrow=true;

//       	  my_points[0]=p[0];
//       	  my_points[2]=p[1];

// 	  //new vector from one to the other node
// 	  xy<double> n_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
// 	  //projection of the new vector to positive x axis
// 	  xy<double> n_x_p2p(fabs(n_p2p.x),0);
// 	  //length of p2p vector
// 	  double n_l_p2p=sqrt( n_p2p.normSquare() );
// 	  if(n_p2p.x<0)
// 	    {
// 	      n_l_p2p*=-1;
// 	    }
// 	  //length of projection of p2p vector
// 	  double n_l_x_p2p=sqrt( n_x_p2p.normSquare() );
// 	  //new angle of p2p vector to the x axis
// 	  double n_a_p2p=acos(n_l_x_p2p/n_l_p2p);
// 	  if(n_p2p.y>0)
// 	    {
// 	      n_a_p2p=2*M_PI-n_a_p2p;
// 	    }

// 	  //new angle of 1b vector to the x axis
// 	  double n_a_1b=o_a_1b+n_a_p2p-o_a_p2p;

// 	  std::cout << " p2p regi: " << o_a_p2p/M_PI*180 << " uj: " << n_a_p2p/M_PI*180-(int)n_a_p2p/M_PI*180 << std::endl;
// 	  std::cout << " 1b regi: " << o_a_1b/M_PI*180 << " uj: " << n_a_1b/M_PI*180-(int)n_a_1b/M_PI*180 << std::endl;

// // 	  std::cout << o_p2p << " " << n_p2p << std::endl;

// 	  if((n_a_1b>M_PI*3/2)||(n_a_1b<M_PI/2))
// 	    {
// 	      std::cout << "jobb terfel" << std::endl;
// 	      my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
// 	    }
// 	  else if((n_a_1b<M_PI*3/2)&&(n_a_1b>M_PI/2))
// 	    {
// 	      std::cout << "bal terfel" << std::endl;
// 	      my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
// 	    }
// 	  else
// 	    {
// 	      std::cout << "y tengely" << std::endl;
// 	      double new_y=my_points[1].get_y();
// 	      my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,new_y);
// 	    }

// 	  std::cout << "alap: " << p[0] << " eredeti hossz: " << o_l_1b << " nagy uj: " << n_l_p2p << " nagy regi: " << o_l_p2p << " a*b/c " << o_l_1b*n_l_p2p/o_l_p2p << " eredmeny: " << my_points[1] << std::endl;


// // 	  if(o_1b.x*o_1b.y>0)
// // 	    {
// // 	      if(n_p2p.x>0)
// // 		{
// // 		  my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
// // 		}
// // 	      else
// // 		{
// // 		  my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
// // 		}
// // 	    }
// // 	  else if(o_1b.x*o_1b.y<0)
// // 	    {
// // 	      if(n_p2p.x>0)
// // 		{
// // 		  my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
// // 		}
// // 	      else
// // 		{
// // 		  my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
// // 		}
// // 	    }
// // 	  else
// // 	    {
// // 	    }

      //////////////////////////////////////////////////////////////////////////////////////////////////////
      /////////// kepps shape-with scalar multiplication
      //////////////////////////////////////////////////////////////////////////////////////////////////////

//       if(p.size()==2)
//       	{
// 	  //old vector from one to the other node - a
// 	  xy<double> a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
// 	  //new vector from one to the other node - b
// 	  xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());

// 	  //old vector from one node to the breakpoint - c
// 	  xy<double> c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y());

// 	  //new vector from one node to the breakpoint - d - we have to calculate this one
// 	  xy<double> d_v;

// 	  //scalar product of a and b (old and new vector from first point to the other)
// 	  double sab=a_v*b_v;
// 	  //scalar product of c and d (old and new vector from first point to breakpoint)
// 	  double scd=sab*c_v.normSquare()/a_v.normSquare();

// 	  std::cout<<" a " << a_v<<" b " <<b_v<<" c " <<c_v<<" sab " <<sab<<" scd "<<scd<<std::endl;

// 	  double a=c_v.normSquare();
// 	  double b=2*scd*c_v.y;
// 	  double c=scd*scd-b_v.normSquare()/a_v.normSquare()*c_v.normSquare()*c_v.x*c_v.x;

// 	  std::cout<<" a " << a<<" b " <<b<<" c " <<c<<std::endl;

// 	  d_v.y=(-b-sqrt(b*b-4*a*c))/2/a;

// 	  if(c_v.x!=0)
// 	    {
// 	      d_v.x=(scd-c_v.y*d_v.y)/c_v.x;
// 	    }
// 	  else
// 	    {
// 	      d_v.x=my_points[1].get_x();
// 	    }

// 	  std::cout<<" d " << d_v<<std::endl;

// 	  my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());

// 	  Gnome::Canvas::Points points;
// 	  for(int i=0;i<3;i++)
// 	    {
// 	      points.push_back(my_points[i]);
// 	    }
// 	  property_points().set_value(points);
// 	}
    }
  if(set_arrow)
    {
      //calculating coordinates of the direction indicator arrow

      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );

      xy<gdouble> unit_vector_in_dir(target-center);
      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
      unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
      //       std::cout << " = " << unit_vector_in_dir << std::endl;

      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
      //       std::cout << unit_norm_vector << std::endl;

      {      
	//       /\       // top
	//      /  \      //
	//      -  -      // c(enter)l(eft), ccl, ccr, cr
	//       ||       //
	//       ||       // b(ottom)l, br
      }

      double size=3;

      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
	 
      //       std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;

      Gnome::Canvas::Points arrow_points;
      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );

      arrow->property_points().set_value(arrow_points);
    }
}

bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
{
  switch(e->type)
    {
    case GDK_BUTTON_PRESS:
      //we mark the location of the event to be able to calculate parameters of dragging
      if(gdc.getActualTool()!=CREATE_NODE)
	{
	  gdc.toggleEdgeActivity(this, true);
	  clicked_x=e->button.x;
	  clicked_y=e->button.y;
	  isbutton=true;
	}
      break;
    case GDK_BUTTON_RELEASE:
      if(gdc.getActualTool()!=CREATE_NODE)
	{
	  gdc.toggleEdgeActivity(this, false);
	  isbutton=false;
	}
      break;
    case GDK_MOTION_NOTIFY:
      //we only have to do sg. if the mouse button is pressed
      if(isbutton)
	{
	  //new coordinates will be the old values,
	  //because the item will be moved to the
	  //new coordinate therefore the new movement
	  //has to be calculated from here

	  double dx=e->motion.x-clicked_x;
	  double dy=e->motion.y-clicked_y;

	  Gnome::Canvas::Points points_new;

	  points_new.push_back(my_points[0]);
	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
	  points_new.push_back(my_points[2]);

	  setPoints(points_new);
	  gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));

	  clicked_x=e->motion.x;
	  clicked_y=e->motion.y;

	}
    default: break;
    }

  return true;
}

xy<double> BrokenEdge::getArrowPos()
{
  xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
  return ret_val;
}
