// Borrowed from cold stowage

#include "rack.h"

double Rack::inclength=180; // Default value for the number of days in an increment.
double Rack::middeckmass=35; // Default (old) value for the mass of a middeck in kg
double Rack::rack_overhead=0.6; // Default value for the packing factor overhead in rack upmass.

Rack::Rack(){
  name="";
  partner="";
  //mod=0;
}

Rack::Rack(MYSQL& conn,string namep){
#ifndef RELEASE
  cout<<"Entered Rack::rack(MYSQL& conn,string namep)\n"<<flush;
  cout<<"\tnamep="<<namep<<'\n'<<flush;
#endif
  name=namep;
  ResultSet res(&conn);
  res.query<<"select racks.*, "
	   <<"locations.RFCA as RFCA, "
	   <<"modules.acronym as modname, "
	   <<"modules.MTL_min, "
	   <<"modules.MTL_max, "
	   <<"modules.MTL_RFCA_min, "
	   <<"modules.MTL_RFCA_max, "
	   <<"modules.LTL_min, "
	   <<"modules.LTL_max, "
	   <<"modules.LTL_RFCA_min, "
	   <<"modules.LTL_RFCA_max "
	   <<"from racks, locations, modules "
	   <<"where racks.acronym=\""<<name<<"\" and "
	   <<"racks.location=locations.idx and "
	   <<"locations.module=modules.idx";
  res.store();res.next();

  modname=(string)res["modname"];
  partner=(string)res["partner"];

  cycles_per_increment=res["cycles_per_increment"];
  hrs_per_cycle=res["hrs_per_cycle"];

  upmass_resupply=res["upmass_resupply"];
  mid_resupply=res["mid_resupply"];
  stowage=res["stowage"];

  power=res["power"];
  keepalive_power=res["keepalive_power"];

  crew_training=res["crew_training"];
  crew_time=res["crew_time"];

  MTL_flow=res["MTL_flow"];
  MTL_flow_keepalive=res["MTL_flow_keepalive"];
  MTL_inlet=res["MTL_inlet"];
  MTL_outlet=res["MTL_outlet"];

  LTL_flow=res["LTL_flow"];
  LTL_flow_keepalive=res["LTL_flow_keepalive"];
  LTL_inlet=res["LTL_inlet"];
  LTL_outlet=res["LTL_outlet"];

  mod_MTL_min=res["MTL_min"];
  mod_MTL_max=res["MTL_max"];
  mod_MTL_RFCA_min=res["MTL_RFCA_min"];
  mod_MTL_RFCA_max=res["MTL_RFCA_max"];
  mod_LTL_min=res["LTL_min"];
  mod_LTL_max=res["LTL_max"];
  mod_LTL_RFCA_min=res["LTL_RFCA_min"];
  mod_LTL_RFCA_max=res["LTL_RFCA_max"];

  video_uplink=res["video_uplink"];
  video_downlink=res["video_downlink"];
  video_rate=res["video_rate"];

  nominal_uplink=res["nominal_uplink"];
  peak_uplink=res["peak_uplink"];
  peak_uplink_duration=res["peak_uplink_duration"];

  nominal_downlink=res["nominal_downlink"];
  peak_downlink=res["peak_downlink"];
  peak_downlink_duration=res["peak_downlink_duration"];

  nominal_1553=res["nominal_1553"];
  peak_1553=res["peak_1553"];
  peak_1553_duration=res["peak_1553_duration"];

  nominal_ethernet=res["nominal_ethernet"];
  peak_ethernet=res["peak_ethernet"];
  peak_ethernet_duration=res["peak_ethernet_duration"];

  nominal_HRL=res["nominal_HRL"];
  peak_HRL=res["peak_HRL"];
  peak_HRL_duration=res["peak_HRL_duration"];
  RFCA=((int)res["RFCA"]==1);
  attached=((int)res["attached"]==1);
#ifndef RELEASE
  cout<<"Returned from Rack::rack(MYSQL& conn,string namep)\n"<<flush;
#endif
}

Rack::~Rack(){
}


double Rack::calc_runtime(){
  return cycles_per_increment*hrs_per_cycle;
}

double Rack::calc_MTL_flow(){
  if(MTL_flow==0)return 0;
  if(RFCA)
    {
      if(MTL_flow < mod_MTL_RFCA_min) return mod_MTL_RFCA_min;
      if(MTL_flow > mod_MTL_RFCA_max) return mod_MTL_RFCA_max;
    }
  else
    {
      if(MTL_flow < mod_MTL_min) return mod_MTL_min;
      if(MTL_flow > mod_MTL_max) return mod_MTL_max;
    }
  return MTL_flow;
}

double Rack::calc_MTL_flow_keepalive(){
  if(MTL_flow_keepalive==0)return 0;
  if(RFCA)
    {
      if(MTL_flow_keepalive < mod_MTL_RFCA_min) return mod_MTL_RFCA_min;
      if(MTL_flow_keepalive > mod_MTL_RFCA_max) return mod_MTL_RFCA_max;
    }
  else
    {
      if(MTL_flow_keepalive < mod_MTL_min) return mod_MTL_min;
      if(MTL_flow_keepalive > mod_MTL_max) return mod_MTL_max;
    }
  return MTL_flow_keepalive;
}

double Rack::calc_LTL_flow(){
  if(LTL_flow==0)return 0;
  if(RFCA)
    {
      if(LTL_flow < mod_LTL_RFCA_min) return mod_LTL_RFCA_min;
      if(LTL_flow > mod_LTL_RFCA_max) return mod_LTL_RFCA_max;
    }
  else
    {
      if(LTL_flow < mod_LTL_min) return mod_LTL_min;
      if(LTL_flow > mod_LTL_max) return mod_LTL_max;
    }
  return LTL_flow;
}

double Rack::calc_LTL_flow_keepalive(){
  if(LTL_flow_keepalive==0)return 0;
  if(RFCA)
    {
      if(LTL_flow_keepalive < mod_LTL_RFCA_min) return mod_LTL_RFCA_min;
      if(LTL_flow_keepalive > mod_LTL_RFCA_max) return mod_LTL_RFCA_max;
    }
  else
    {
      if(LTL_flow_keepalive < mod_LTL_min) return mod_LTL_min;
      if(LTL_flow_keepalive > mod_LTL_max) return mod_LTL_max;
    }
  return LTL_flow_keepalive;
}

double Rack::calc_energy(){
  return power*calc_runtime()+keepalive_power*(inclength*24-calc_runtime());
}

double Rack::calc_upmass(){
  return upmass_resupply*(rack_overhead+1)+mid_resupply*middeckmass;
}
