
#include "Event.h"
#include <iostream.h>
#include <math.h>

Event::Event(int n) {
    numHits = n;
    tubeArray = new PhotoTube*[n];
    minCharge = 0;
    maxCharge = 0;
    minTime = 0;
    maxTime = 0;    
}


Event::Event() {
    numHits = 0;
    tubeArray = 0;
    minCharge = 0;
    maxCharge = 0;
    minTime = 0;
    maxTime = 0;
}


Event::Event(const Event& a) {
    numHits = a.numHits;
    tubeArray = new PhotoTube*[numHits];
    minCharge = a.minCharge;
    maxCharge = a.maxCharge;
    minTime = a.minTime;
    maxTime = a.maxTime;
    copy(a); // Copy a's elements
}


void Event::copy(const Event& a) {
    // Copy a's elements into the elements of *this
    PhotoTube** p = tubeArray + numHits;
    PhotoTube** q = a.tubeArray + numHits;
    while (p > tubeArray) *--p = *--q;
}


Event::~Event() {
    delete [] tubeArray;
}

void Event::init(float* limits)
{
  minCharge = limits[0];
  maxCharge = limits[1];
  minTime = limits[2];
  maxTime = limits[3];
  setTColor();
  setCColor();
}


PhotoTube*& Event::operator[](int i) {
    return tubeArray[i];
}


int Event::numTubes() {
    return numHits;
}


Event& Event::operator=(const Event& rhs) {
    if ( tubeArray != rhs.tubeArray ) {
      minCharge = rhs.minCharge;
      maxCharge = rhs.maxCharge;
      minTime = rhs.minTime;
      maxTime = rhs.maxTime;
      setSize( rhs.numHits );
      copy(rhs);
    }
    return *this;
}


void Event::setSize(int n) {
    if (n != numHits) {

        delete [] tubeArray;    // Delete old elements,
        numHits = n;             // set new count,
        tubeArray = new PhotoTube*[n];   // and allocate new elements

    }
}


Event& Event::operator=(PhotoTube* rhs) {
    PhotoTube** p = tubeArray + numHits;
    while (p > tubeArray) *--p = rhs;
    return *this;
}

void Event::setCColor()
{
  for(int i=0; i<numHits; i++)
    tubeArray[i]->setCColor(maxCharge, minCharge);
}

void Event::setTColor()
{
  for(int i=0; i<numHits; i++)
    tubeArray[i]->setTColor(maxTime, minTime);
}

void Event::fadeCColor()
{
  minCharge = (2*minCharge+maxCharge)/3.;
  setCColor();
}

void Event::fadeTColor()
{
  minTime = (2*minTime+maxTime)/3.;
  setTColor();
}

void Event::increaseCColor()
{
  minCharge = (3*minCharge - maxCharge)/2.;
  setCColor();
}

void Event::increaseTColor()
{
  minTime = (3*minTime - maxTime)/2.;
  setTColor();
}


void Event::rotate()
{
  for(int i=0; i<numHits; i++)
    tubeArray[i]->rotate();
}


PhotoTube::PhotoTube(float* para, int i)
{
  index = i;
  charge = para[0];
  time = para[1];
  x = para[2];
  y = para[3];
  z = para[4];
}

PhotoTube::PhotoTube()
{
}

PhotoTube& PhotoTube::operator = (PhotoTube& pt)
{
  index = pt.index;
  charge = pt.charge;
  time = pt.time;
  screenX = pt.screenX;
  screenY = pt.screenY;
  x = pt.x;
  y = pt.y;
  z = pt.z;

  return *this;
}


float PhotoTube::getC()
{
  return charge;
}

float PhotoTube::getT()
{
  return time;
}

int PhotoTube::getScreenX()
{
  return screenX;
}

int PhotoTube::getScreenY()
{
  return screenY;
}


void PhotoTube::setCColor(float max, float min)
{
  ccolor = (int) ((charge - min)/(max - min)*7);
  ccolor = Colors().decide(ccolor);
}

void PhotoTube::setTColor(float max, float min)
{
  tcolor = (int) ((max - time)/(max - min)*7);
  tcolor = Colors().decide(tcolor);
}

int PhotoTube::getTColor()
{
  return tcolor;
}

int PhotoTube::getCColor()
{
  return ccolor;
}


TopTube::TopTube():PhotoTube()
{
}

TopTube::TopTube(float* para, int i):PhotoTube(para, i)
{
  calcXY();
}

void TopTube::calcXY()
{
  screenX = 400 - (int)(y/20);
  screenY = 125 - (int)(x/20);
} 

void TopTube::rotate()
{
  int  tmp = screenX;
  screenX = 400 + screenY - 125;
  screenY = 125 - (tmp - 400);
} 

BottomTube::BottomTube():PhotoTube()
{
}

BottomTube::BottomTube(float* para, int i):PhotoTube(para, i)
{
  calcXY();
}

void BottomTube::calcXY()
{
  screenX = 400 - (int)(y/20);
  screenY = 475 + (int)(x/20);
}

void BottomTube::rotate()
{
  int tmp = screenX;
  screenX = 400 - (screenY - 475);
  screenY = 475 + (tmp - 400);
}

SideTube::SideTube():PhotoTube()
{
}

SideTube::SideTube(float* para, int i):PhotoTube(para, i)
{
  calcXY();
}

void SideTube::calcXY()
{
  screenY = 300 - (int)(z/20);
  if(y>=0)
    screenX = 100 + (int) ((acos(x/sqrt(x*x+y*y))/3.142)*300);
  else
    screenX = 100 + (int) ((2 - acos(x/sqrt(x*x+y*y))/3.142)*300);
}

void SideTube::rotate()
{
  screenX = screenX + 150;
  if(screenX>700)
    screenX = screenX%700 + 100;
}



int Colors::decide(int c)
{
  switch(c)
    {
    case 0: return BLUE2; break;
    case 1: return BLUE1; break;
    case 2: return GREEN;  break;
    case 3: 
    case 4: return YELLOW; break;
    case 5: return RED2; break;
    case 6:
    case 7: return RED1; break;
    default: return BLACK; 
    }
}

Colors::Colors()
{
}

int Colors::RED1 = 39;
int Colors::RED2 = 35;
int Colors::YELLOW = 37;
int Colors::GREEN = 36;
int Colors::ORANGE = 2;
int Colors::BLUE1 = 38;
int Colors::BLUE2 = 41;
int Colors::BLUE3 = 115;
int Colors::PURPLE = 0;
int Colors::BLACK = 1;



