/*
_______________________________________________________________________________

                              TableGenerator v1.3

-------------------------------------------------------------------------------
Copyright (c) 2007-2010 Kai Braaten

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-------------------------------------------------------------------------------
*/

#include "tgen_internal.hpp"

namespace TableGen
{

///////////////////////////////////////////////////////////////////////////////
// ShadowTable strategy

ShadowTable::ShadowTable( const std::string &ht,  // Header text
                          const std::string &tc,  // Text colour
                          const std::string &fc,  // Background fill colour
                          const std::string &sc,  // Shadow colour
                          const std::string &st,  // Stop colour
                          size_t fp,              // side padding
                          size_t cp,              // coloumn padding
                          size_t ind,             // indentation
			  const std::string &cc,  // closing colour tag
			  const std::string &nl ) // newline character(s)
  : _header( ht ),
    _textColour( tc ),
    _fillColour( fc ),
    _shadowColour( sc ),
    _stopColour( st ),
    _closeColour( cc ),
    _newline( nl ),
    _padding( fp ),
    _columnPadding( cp ),
    _indentation( ind ),
    _depth( 0 )
{

}

const std::string &ShadowTable::addColour( const std::string &colour )
{
  ++_depth;
  return colour;
}

std::string ShadowTable::balanceColourTags()
{
  std::string out;

  if( !_closeColour.empty() )
    {
      while( _depth-- != 0 )
	{
	  out += _closeColour;
	}
    }
  else
    {
      _depth = 0;
    }

  return out;
}

std::string ShadowTable::render( const std::vector< TableRow > &table,
                                 const std::vector< size_t > &fieldWidth )
{
#ifdef __STORMGCC__
  std::ostrstream buf;
#else
  std::ostringstream buf;
#endif
  size_t tableWidth = 0;
  bool hasHeader = !_header.empty();

  for( size_t rowNum = 0; rowNum < table.size(); ++rowNum )
    {
      const TableRow &row = table[ rowNum ];
      size_t rowWidth = 0;

      if( _padding > 0 )
	{
	  buf << addColour( _stopColour ) << std::string( _indentation, ' ' )
	      << addColour( _fillColour ) << addColour( _textColour )
	      << std::string( _padding, ' ' );
	}

      for( size_t colNum = 0; colNum < row.size(); ++colNum )
        {
          buf << addColour( _fillColour ) << addColour( _textColour );

#ifdef __STORMGCC__
	  if( isNumber( row[ colNum ] ) )
	    {
	      buf.unsetf( std::ios::left );
	    }
          else
	    {
	      buf.setf( std::ios::left );
	    }
#else
	  if( isNumber( row[ colNum ] ) )
	    {
	      buf << std::right;
	    }
          else
	    {
	      buf << std::left;
	    }
#endif
          buf << std::setw( fieldWidth[ colNum ] )
              << row[ colNum ];

          rowWidth += fieldWidth[ colNum ];

          if( colNum != row.size() - 1 )
            {
              buf << std::string( _columnPadding, ' ' );
              rowWidth += _columnPadding;
	    }
        }

      if( _padding > 0 )
	{
	  buf << std::string( _padding, ' ' );
	}

      buf << addColour( _shadowColour ) << ' ' << addColour( _stopColour )
	  << _newline;

      if( rowWidth > tableWidth )
	{
	  tableWidth = rowWidth;
	}
    }

  std::string out = buf.str();

#ifdef __STORMGCC__
  buf.rdbuf()->freeze( false );
#endif

  // Header
  if( hasHeader )
    {
      if( _header.size() > tableWidth )
	{
	  _header.erase( tableWidth - tableWidth % 2 );
	}

      std::string headBuf = addColour( _stopColour )
	+ std::string( _indentation, ' ' )
        + addColour( _fillColour ) + std::string( _padding, ' ' );

      // Calculate center justification
      size_t headPad = ( tableWidth / 2 - _header.size() / 2 );
      size_t compensate = ( _header.size() % 2 ) ? 1 : 0;

      if( headPad )
	{
	  headBuf += std::string( headPad - compensate, ' ' );
	}

      headBuf += addColour( _textColour ) + _header;

      compensate = ( tableWidth % 2 );

      size_t tmpPad = _padding;

      if( !headPad )
	{
	  tmpPad = 0;

	  if( _header.size() % 2 == 0 )
	    {
	      ++compensate;
	    }
	}

      headBuf += std::string( headPad + tmpPad + compensate, ' ' );
      headBuf += addColour( _shadowColour ) + ' '
	+ addColour( _stopColour ) + _newline;

      // At an extra blank line below header
      headBuf +=  addColour( _stopColour )
	+ std::string( _indentation, ' ' ) + addColour( _fillColour )
        + std::string( tableWidth + _padding * 2, ' ' )
        + addColour( _shadowColour ) + ' ' + addColour( _stopColour )
	+ _newline;
      out.insert( 0, headBuf );
    }

  // Insert a blank line at the very top.
  out.insert( 0, addColour( _stopColour ) + std::string( _indentation, ' ' )
	      + addColour( _fillColour )
              + std::string( tableWidth + _padding * 2, ' ' )
              + addColour( _stopColour ) + _newline );

  // Add a blank line at the bottom
  out += addColour( _stopColour ) + std::string( _indentation, ' ' )
    + addColour( _fillColour )
    + std::string( tableWidth + _padding * 2, ' ' )
    + addColour( _shadowColour ) + ' ' + addColour( _stopColour ) + _newline;

  // And finally the bottom shadow
  out += addColour( _stopColour ) + std::string( _indentation + 1, ' ' )
    + addColour( _shadowColour )
    + std::string( tableWidth + _padding * 2, ' ' )
    + addColour( _stopColour ) + _newline;

  out += balanceColourTags();

  return out;
}

}