/******************************** LICENSE ********************************

 Copyright 2007 European Centre for Medium-Range Weather Forecasts (ECMWF)

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 ******************************** LICENSE ********************************/

/*! \file DateAxis.cc
    \brief Implementation of the Template class DateAxis.
    
    Magics Team - ECMWF 2005
    
    Started: Fri 7-Oct-2005
    
    Changes:
    
*/

#include "DateAxis.h"

using namespace magics;


DateAxis::DateAxis()
{
	update();
}


DateAxis::~DateAxis() 
{
}

/*!
 Class information are given to the output-stream.
*/		
void DateAxis::print(ostream& out)  const
{
	out << "DateAxis[";
	out << "]";
}

double DateAxis::getMin() const
{
	update();
	return from_ - base_;
}


double DateAxis::getMax() const
{
	update();
	return to_  - base_;
}

void DateAxis::update() const
{ 
	from_ = (date_min_ != "undef" ) ? DateTime(date_min_) : DateTime();
	to_   = (date_max_ != "undef" ) ? DateTime(date_max_) : DateTime();

	base_ = (DateAxisAttributes::reference_ != "undef" ) ? DateTime(DateAxisAttributes::reference_) : from_;
}


void DateAxis::prepare(double min, double max, AxisItems& items)
{
	update();
	//MagLog::dev()<< "min..." << min << endl;
	//MagLog::dev()<< "max..." << max << endl; 
	
	Second smin(double(min));
	Second smax(double(max));
	DateTime left = base_ + Second(min);
	DateTime right = base_ + Second(max);
	(*method_).prepare(*this, base_, left, right, items);
	
	vector<AxisItem*> dates;
	for ( AxisItems::const_iterator item = items.begin(); item != items.end(); ++item) {
		if ( (**item).date() )
			dates.push_back(*item);
	}
	int position = 0;
	(*hours_).position(position);
	(*days_).position(position);
	(*months_).position(position);
	(*years_).position(position);
	
	(*hours_).labels(dates);	
	(*days_).labels(dates);	
	(*months_).labels(dates);
	(*years_).labels(dates);
	std::copy((*hours_).begin(), (*hours_).end(), back_inserter(items));
	std::copy((*days_).begin(), (*days_).end(), back_inserter(items));
	std::copy((*months_).begin(), (*months_).end(), back_inserter(items));
	std::copy((*years_).begin(), (*years_).end(), back_inserter(items));
	for ( AxisItems::const_iterator item = items.begin(); item != items.end(); ++item)
	{
		if  (!(**item).label().empty() )
		MagLog::debug() << " found at " << (**item).position() << " --->" << (**item).label() << "\n";
	}
}



void DateAxis::updateX(const Transformation& transformation)
{
	const double min = transformation.getMinX();
	const double max = transformation.getMaxX();

	if ( DateAxisAttributes::reference_ != "undef" )
	{
		base_ = DateTime(reference_);
	}
	else
	{
		base_ = DateTime(transformation.getReferenceX());
	}
	DateTime from = base_ + Second(min*3600);
	MagLog::dev() << "from-> " << from << endl;
	MagLog::dev() << "base-> " << base_ << endl;
	Second add = int(max*3600);
	
	DateTime to = base_;
	MagLog::dev() << "to " << to <<" + "<<add  << endl;
	to = to + add;

	DateAxisAttributes::setDate_min(from);
	DateAxisAttributes::setDate_max(to);
	DateAxisAttributes::setReference(base_);
}

void DateAxis::updateY(const Transformation& transformation)
{
	double min = transformation.getMinY();
	double max = transformation.getMaxY();
	if ( DateAxisAttributes::reference_ != "undef" )
	{
		 base_ = DateTime(reference_);
	}
	else 
	{
		base_ = DateTime(transformation.getReferenceY());
	}
	DateTime from = base_ + Second(min*3600);
	MagLog::dev() << "from-> " << from << endl;
	MagLog::dev() << "base-> " << base_ << endl;
	Second add = int(max*3600);
	
	DateTime to   = base_;
	MagLog::dev() << "to " << add << to << endl;
	to = to + add;
	
	DateAxisAttributes::setDate_min(from);
	DateAxisAttributes::setDate_max(to);
	DateAxisAttributes::setReference(base_);
}




HyperAxis::HyperAxis()
{
}

HyperAxis::~HyperAxis()
{
}

	
void HyperAxis::prepare(double, double, AxisItems&) const
{
}

void HyperAxis::updateX(const Transformation&)
{
}

void HyperAxis::updateY(const Transformation&) 
{
}

void HyperAxis::print(ostream&) const
{
}

void HyperAxis::update() const
{
}
/*
if (ndates/(24/delt) .gt. 5000 ) then
  call psetc('axis_date_type','years')
  call psetc('axis_hours_label','off')
  call psetc('axis_days_label','off')
  call psetc('axis_months_label','off')
  call psetc('axis_years_label','on')
  call psetr('axis_tick_interval',1.)
  call pseti('axis_tick_label_frequency',3)
elseif (ndates/(24/delt) .gt. 1000 ) then
  call psetc('axis_date_type','months')
  call psetc('axis_hours_label','off')
  call psetc('axis_days_label','off')
  call psetc('axis_months_label','on')
  call psetc('axis_years_label','on')
  call psetr('axis_tick_interval',1.)
  call pseti('axis_tick_label_frequency',2)
elseif (ndates/(24/delt) .gt. 720 ) then
  call psetc('axis_date_type','months')
  call psetc('axis_hours_label','off')
  call psetc('axis_days_label','off')
  call psetc('axis_months_label','on')
  call psetc('axis_years_label','on')
  call psetr('axis_tick_interval',2.)
  call pseti('axis_tick_label_frequency',4)
elseif (ndates/(24/delt) .gt. 45) then
  call psetc('axis_date_type','days')
  call psetc('axis_hours_label','off')
  call psetc('axis_days_label','number')
  call psetc('axis_months_label','on')
  call psetc('axis_years_label','off')
  call psetr('axis_tick_interval',1.)
  call pseti('axis_tick_label_frequency',4)
else
  call psetc('axis_date_type','days')
  call psetc('axis_hours_label','off')
  call psetc('axis_days_label','number')
  call psetc('axis_months_label','on')
  call psetc('axis_years_label','on')
  call psetr('axis_tick_interval',1.)
  call pseti('axis_tick_label_frequency',2)
endif
*/
