#include "G4HumanPhantomColour.h"
#include "G4HumanPhantomMaterial.h"
#include "G4MIRDUpperSpine.h"

#include "G4Box.hh"
#include "G4EllipticalTube.hh"
#include "G4PVPlacement.hh"
#include "G4RotationMatrix.hh"
#include "G4SDManager.hh"
#include "G4SubtractionSolid.hh"
#include "G4SystemOfUnits.hh"
#include "G4ThreeVector.hh"
#include "G4VPhysicalVolume.hh"
#include "G4VisAttributes.hh"
#include "globals.hh"

G4MIRDUpperSpine::G4MIRDUpperSpine() {}

G4MIRDUpperSpine::~G4MIRDUpperSpine() {}

G4VPhysicalVolume* G4MIRDUpperSpine::Construct(const G4String& volumeName, G4VPhysicalVolume* mother,
                                               const G4String& colourName, G4bool wireFrame, G4bool) {
    G4HumanPhantomMaterial* material = new G4HumanPhantomMaterial();

    G4cout << "Construct " << volumeName << " with mother volume " << mother->GetName() << G4endl;

    G4Material* skeleton = material->GetMaterial("skeleton");

    delete material;

    G4double dx = 2. * cm;
    G4double dy = 2.5 * cm;
    G4double dz = 4.25 * cm;

    G4EllipticalTube* upperSpine = new G4EllipticalTube("UpperSpine", dx, dy, dz);

    G4double xx = 20. * cm;
    G4double yy = 10. * cm;
    G4double zz = 5. * cm;

    G4Box* subtraction = new G4Box("box", xx / 2., yy / 2., zz / 2.);

    G4RotationMatrix* matrix = new G4RotationMatrix();
    matrix->rotateX(-25. * deg);

    G4SubtractionSolid* upper_spine =
        new G4SubtractionSolid("upperspine", upperSpine, subtraction, matrix, G4ThreeVector(0., -2.5 * cm, 5.5 * cm));

    G4LogicalVolume* logicUpperSpine = new G4LogicalVolume(upper_spine, skeleton, "logical" + volumeName, 0, 0, 0);
    // Define rotation and position here!
    G4VPhysicalVolume* physUpperSpine = new G4PVPlacement(
        0, G4ThreeVector(0.0, 5.5 * cm, -3.5 * cm), "physicalUpperSpine", logicUpperSpine, mother, false, 0, true);

    // Visualization Attributes
    // G4VisAttributes* UpperSpineVisAtt = new G4VisAttributes(G4Colour(0.46,0.53,0.6));
    G4HumanPhantomColour* colourPointer = new G4HumanPhantomColour();
    G4Colour colour = colourPointer->GetColour(colourName);
    G4VisAttributes* UpperSpineVisAtt = new G4VisAttributes(colour);

    UpperSpineVisAtt->SetForceSolid(wireFrame);
    logicUpperSpine->SetVisAttributes(UpperSpineVisAtt);

    G4cout << "UpperSpine created !!!!!!" << G4endl;

    // Testing UpperSpine Volume
    G4double UpperSpineVol = logicUpperSpine->GetSolid()->GetCubicVolume();
    G4cout << "Volume of UpperSpine = " << UpperSpineVol / cm3 << " cm^3" << G4endl;

    // Testing UpperSpine Material
    G4String UpperSpineMat = logicUpperSpine->GetMaterial()->GetName();
    G4cout << "Material of UpperSpine = " << UpperSpineMat << G4endl;

    // Testing Density
    G4double UpperSpineDensity = logicUpperSpine->GetMaterial()->GetDensity();
    G4cout << "Density of Material = " << UpperSpineDensity * cm3 / g << " g/cm^3" << G4endl;

    // Testing Mass
    G4double UpperSpineMass = (UpperSpineVol)*UpperSpineDensity;
    G4cout << "Mass of UpperSpine = " << UpperSpineMass / gram << " g" << G4endl;

    return physUpperSpine;
}
