//----------------------------------------------------------------------------
///
/// \file   progress.hpp
///
/// \brief  Progress bar widget
///
/// \date   20221224
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------


//--- Own ----------------------------

#include <ocelli/progress.hpp>


//---Implementation-----------------------------------------------------------


OProgress::OProgress(int x1, int y1, int x2, int y2 )
   :OWidget(x1, y1, x2, y2)
   ,m_text("0%")
{
}

OProgress::OProgress( const OPoint p1, const OPoint p2)
   :OWidget(p1, p2)
   ,m_text("0%")
{
}

void OProgress::draw()
{
   m_display->drawRect( p1().x, p1().y , p2().x , p2().y , darkGreen );
   m_display->drawBar( p1().x+2, p1().y+2 , p2().x-2 , p2().y-2 , black );
   m_display->setFont(getFont16());
   m_display->drawText( centerX(), centerY(),
                     m_text.data(), eTextFlagsHCenter | eTextFlagsVCenter );

}

void OProgress::update( const Ocelli::SEvent &event )
{

}

void OProgress::undraw( )
{
   m_display->setFont(getFont16());
   m_display->setDrawBackground(false);
   m_display->setFg(black);
   m_display->drawText( centerX(), centerY(),
                     m_text.data(), eTextFlagsHCenter | eTextFlagsVCenter );
   if(!m_progress)
   {
      m_display->drawRect( p1().x, p1().y , p2().x , p2().y , green );
   }
}

void OProgress::update( )
{
   m_display->setFg(green);
   int xSize= ( ( (p2().x-2)-(p1().x+2) ) * m_progress ) / 100;

   m_display->drawBar( p1().x+2, p1().y+2 , p1().x+2 + xSize , p2().y-2 ,
                  darkGreen );

   m_display->setFont(getFont16());
   m_display->setDrawBackground(false);
   //m_display->setFg( ( m_progress > 50 ) ? black : white );
   m_display->setFg( white );
   m_display->drawText( centerX(), centerY(),
                     m_text.data(), eTextFlagsHCenter | eTextFlagsVCenter );
}

//---fin-----------------------------------------------------------------------
