//----------------------------------------------------------------------------
///
/// \file   button.hpp
///
/// \brief  class for graphical widget
///
/// \date   20220830
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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


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


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

#include <ocelli/button.hpp>


//---Declaration---------------------------------------------------------------

#if defined USE_BIWAK

const OButton::SStyles OButton::styles[]=
{
    [ (int)OButton::EButtonStyle::bar ]=
    {
      .topColor= __lightGrey ,
      .bottomColor= __black ,
      .horizontalSeperator=true,
      .indicatorCircle=false,
      .rectBorder=8
    },
    [ (int)OButton::EButtonStyle::flat ]=
    {
      .topColor=__black,
      .bottomColor=__black,
      .horizontalSeperator=false,
      .indicatorCircle=false,
      .rectBorder=6
    },
    [ (int)OButton::EButtonStyle::flatSmallBorder ]=
    {
      .topColor=__black,
      .bottomColor=__black,
      .horizontalSeperator=false,
      .indicatorCircle=false,
      .rectBorder=1
    },
};


void OButton::draw() /* override virtual */
{
   int border=styles[(int)m_style].rectBorder;

   if ( styles[(int)m_style].topColor == styles[(int)m_style].bottomColor )
   {
      m_display->setBg( styles[(int)m_style].topColor );
      m_display->setDrawBackground(true);
   }
   else
   {
      m_display->setDrawBackground(false);
   }
   m_display->setFg(white);

   m_display->drawBar( p1().x, p1().y, p2().x, p2().y,
                       styles[(int)m_style].topColor,
                       styles[(int)m_style].bottomColor);

   if ( styles[(int)m_style].topColor == styles[(int)m_style].bottomColor )
   {
      m_display->drawBar( p1().x + border + 1, p1().y + border + 1 , p2().x - border - 1, p2().y - border - 1,
                     darkDarkViolet, darkDarkViolet );
      m_display->setBg( darkDarkViolet );
   }

   if( m_icon != nullptr )
   {
      //OIconDrawer icon;
      m_icon->draw( centerX() - ( m_icon->getWidth() / 2 ),
                 centerY() - ( m_icon->getHeight() / 2 ) );
   }
   else
   {
      m_display->setFont(getFont16());
      m_display->drawText( centerX(), centerY(),
                        m_text, eTextFlagsHCenter | eTextFlagsVCenter );
   }

   #if 0
   if( styles[(int)m_style].horizontalSeperator )
   {
      m_display->drawBar( p1().x, p1().y, p1().x, p2().y , green, black);
   }
   #endif

   const Ocelli::SEvent event{.type=Ocelli::EEventType::init};
   //update( CTouch::EEventType::none );
   update( event );
   m_display->setBg( black );
   return;
}

void OButton::update( const Ocelli::SEvent &event ) /* override virtual */
{
   int border=styles[(int)m_style].rectBorder;

   if( event.type == Ocelli::EEventType::moved )
   {
      return;
   }

   if( styles[(int)m_style].indicatorCircle )
   {
      m_display->drawCircle( centerX(), centerY(), (width()/2)- border, false,
                        event.type==Ocelli::EEventType::pressed ? lightGreen : darkDarkGrey );
   }
   else
   {
      m_display->drawRect( p1().x + border, p1().y + border, p2().x - border, p2().y - border,
                        event.type==Ocelli::EEventType::pressed ? lightGreen : darkDarkGrey );
   }
}

#else

void OButton::draw() /* override virtual */
{
}

void OButton::update(CTouch::EEventType eventType) /* override virtual */
{
}

#endif

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