#include <TPrsStd_ConstraintDriver.ixx>

#include <TDF_Label.hxx>
#include <TDataStd_Constraint.hxx>
#include <TPrsStd_ConstraintTools.hxx>
#include <Standard_ProgramError.hxx>
#include <Standard_GUID.hxx>
#include <TDataStd_Position.hxx>
#include <AIS_Drawer.hxx>
#include <AIS_InteractiveContext.hxx>
#include <TDataStd_Real.hxx>
#include <AIS_Relation.hxx>

//=======================================================================
//function :
//purpose  : 
//=======================================================================
TPrsStd_ConstraintDriver::TPrsStd_ConstraintDriver()
{
}

//=======================================================================
//function :
//purpose  : 
//=======================================================================
Standard_Boolean TPrsStd_ConstraintDriver::Update (const TDF_Label& aLabel,
						  Handle(AIS_InteractiveObject)& anAISObject) 
{
  Handle(TDataStd_Constraint) apConstraint;
  if( !aLabel.FindAttribute(TDataStd_Constraint::GetID(), apConstraint) ) {
   return Standard_False;
  }
   
  if (!anAISObject.IsNull() && anAISObject->HasInteractiveContext()) {
    if (!apConstraint->Verified()) {
      TPrsStd_ConstraintTools::UpdateOnlyValue(apConstraint,anAISObject);
      if (anAISObject->Color() != Quantity_NOC_RED) anAISObject->SetColor(Quantity_NOC_RED);
      return Standard_True;
    }
  }

  Handle(AIS_InteractiveObject) anAIS = anAISObject;

  // recuperation 
  TDataStd_ConstraintEnum thetype = apConstraint->GetType();
  
  switch (thetype)  {
  case TDataStd_DISTANCE:
    {
      TPrsStd_ConstraintTools::ComputeDistance(apConstraint,anAIS);
      break;
    }
  case TDataStd_PARALLEL:
    {
      TPrsStd_ConstraintTools::ComputeParallel(apConstraint,anAIS);
      break;
    }
  case TDataStd_PERPENDICULAR:
    {
      TPrsStd_ConstraintTools::ComputePerpendicular(apConstraint,anAIS);
      break;
    }
  case TDataStd_CONCENTRIC:
    {
      TPrsStd_ConstraintTools::ComputeConcentric(apConstraint,anAIS);
      break;
    }
  case TDataStd_SYMMETRY:
    {
      TPrsStd_ConstraintTools::ComputeSymmetry(apConstraint,anAIS);
      break;
    }
  case TDataStd_MIDPOINT:
    {
      TPrsStd_ConstraintTools::ComputeMidPoint(apConstraint,anAIS);
      break;
    }
  case TDataStd_TANGENT:
    {
      TPrsStd_ConstraintTools::ComputeTangent(apConstraint,anAIS);
      break;
    }
  case TDataStd_ANGLE:
    {
      TPrsStd_ConstraintTools::ComputeAngle(apConstraint,anAIS);
      break;
    }
  case TDataStd_RADIUS:
    {
      TPrsStd_ConstraintTools::ComputeRadius(apConstraint,anAIS);
      break;
    }
  case TDataStd_MINOR_RADIUS:
    {
      TPrsStd_ConstraintTools::ComputeMinRadius(apConstraint,anAIS);
      break;
    }
  case TDataStd_MAJOR_RADIUS:
    {
      TPrsStd_ConstraintTools::ComputeMaxRadius(apConstraint,anAIS);
      break; 
    }
  case TDataStd_DIAMETER:
    {
      TPrsStd_ConstraintTools::ComputeDiameter(apConstraint,anAIS);
      break;
    }
  case TDataStd_FIX:
    {
      TPrsStd_ConstraintTools::ComputeFix(apConstraint,anAIS);
      break;
    } 
  case TDataStd_OFFSET:
    {
      TPrsStd_ConstraintTools::ComputeOffset(apConstraint,anAIS);
      break;
    }
  case TDataStd_COINCIDENT:
    {
      TPrsStd_ConstraintTools::ComputeCoincident(apConstraint,anAIS); 
      break;
    }
  case TDataStd_ROUND:
    {
      TPrsStd_ConstraintTools::ComputeRound(apConstraint,anAIS); 
      break;
    }

  case TDataStd_MATE:
  case TDataStd_ALIGN_FACES:
  case TDataStd_ALIGN_AXES:
  case TDataStd_AXES_ANGLE:
    {
      TPrsStd_ConstraintTools::ComputePlacement(apConstraint,anAIS);
      break;
    }
  case TDataStd_EQUAL_DISTANCE :
    {
      TPrsStd_ConstraintTools::ComputeEqualDistance(apConstraint,anAIS);
      break;
    }
  case  TDataStd_EQUAL_RADIUS:
    {
      TPrsStd_ConstraintTools::ComputeEqualRadius(apConstraint,anAIS);
      break;
    }
  default:
    {
      TPrsStd_ConstraintTools::ComputeOthers(apConstraint,anAIS);
      break;
    }
  }
  if (anAIS.IsNull()) return Standard_False;
 
  anAIS->ResetLocation();
  anAIS->SetToUpdate();
  anAIS->UpdateSelection();
  
  anAISObject = anAIS;
  
  Handle(TDataStd_Position) Position;
  if (aLabel.FindAttribute(TDataStd_Position::GetID(),Position)) {
    Handle(AIS_Relation)::DownCast(anAISObject)->SetPosition(Position->GetPosition());
  }

  if (anAISObject->HasInteractiveContext()) {
    Quantity_NameOfColor originColor = anAISObject->Color();
    if (!apConstraint->Verified()) {
      if (originColor != Quantity_NOC_RED)
	anAISObject->SetColor(Quantity_NOC_RED);
    }
    else if (apConstraint->IsDimension() && apConstraint->GetValue()->IsCaptured()) {
      if (originColor != Quantity_NOC_PURPLE)
	anAISObject->SetColor(Quantity_NOC_PURPLE);
    }
    else if (!apConstraint->IsPlanar() && (originColor != Quantity_NOC_YELLOW)) 
      anAISObject->SetColor(Quantity_NOC_YELLOW);
  }
  else {
    if (!apConstraint->Verified()) {
      anAISObject->SetColor(Quantity_NOC_RED);
    }
    else if (apConstraint->IsDimension() && apConstraint->GetValue()->IsCaptured()) {
      anAISObject->SetColor(Quantity_NOC_PURPLE);
    }
    else if (!apConstraint->IsPlanar()) anAISObject->SetColor(Quantity_NOC_YELLOW);
  }
  return Standard_True;
}

