/*
	Based up Neutrino-GUI - Tuxbox-Project
	Copyright (C) 2001 by Steffen Hehn 'McClean'

	Classes for generic GUI-related components.
	Copyright (C) 2012-2015, Thilo Graf 'dbt'

	License: GPL

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public
	License as published by the Free Software Foundation; either
	version 2 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	General Public License for more details.

	You should have received a copy of the GNU General Public
	License along with this program; if not, write to the
	Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
	Boston, MA  02110-1301, USA.
*/

#ifndef __CC_CLOCK__
#define __CC_CLOCK__


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <driver/neutrinofonts.h>
#include "cc_base.h"
#include "cc_frm.h"
#include "cc_timer.h"
#include "cc_text_screen.h"
//! Sub class of CComponents. Show clock with digits on screen. 
/*!
Usable as simple fixed display or as ticking clock.
*/

class CComponentsFrmClock : public CComponentsForm, public CCTextScreen
{
	private:
		CComponentsTimer *cl_timer;
		void ShowTime();
#if 0
		bool may_blit;
#endif

	protected:
		///slot for timer event, reserved for ShowTime()
		sigc::slot0<void> cl_sl;

		///refresh interval in seconds
		int cl_interval;

		///raw time chars
		char cl_timestr[20];

		///handle paint clock within thread and is not similar to cc_allow_paint
		bool paintClock;

		///object: font render object
		Font *cl_font;
		int cl_font_style;

		///text color
		int cl_col_text;

		///current time format
		std::string cl_format;
		///primary time format
		std::string cl_format_str;
		///secondary time format for blink
		std::string cl_blink_str;

		///initialize clock contents  
		void initCCLockItems();
		///initialize timestring, called in initCCLockItems()
		virtual void initTimeString();

		///start ticking clock, returns true on success, if false causes log output
		bool startClock();
		///stop ticking clock, returns true on success, if false causes log output
		bool stopClock();
		///switch between primary and secondary format
		void toggleFormat();

		///return pointer of font object
		Font* getClockFont();

	public:

		CComponentsFrmClock( 	const int& x_pos = 1, const int& y_pos = 1,
					Font * font = NULL,
					const char* format_str = "%H:%M",
					const char* secformat_str = NULL,
					bool activ=false,
					const int& interval_seconds = 1,
					CComponentsForm *parent = NULL,
					int shadow_mode = CC_SHADOW_OFF,
					fb_pixel_t color_frame = COL_LIGHT_GRAY,
					fb_pixel_t color_body = COL_MENUCONTENT_PLUS_0,
					fb_pixel_t color_shadow = COL_MENUCONTENTDARK_PLUS_0,
					int font_style = CNeutrinoFonts::FONT_STYLE_BOLD
				);
		virtual ~CComponentsFrmClock();

		/*! Sets font type for clock segments.
		 * 1st parameter expects a pointer to font type, usually a type from the global g_Font collection, but also possible
		 * are dynamic font.
		 * The use of NULL pointer enforces dynamic font.
		 * 2nd paramter is relevant for dynamic fonts only, you can use the enum types
		 * - FONT_STYLE_REGULAR
		 * - FONT_STYLE_BOLD
		 * - FONT_STYLE_ITALIC
		 * (see /.src/driver/neutrinofonts.h)
		*/
		void setClockFont(Font * font, const int& style = -1);

		///set text color
		virtual void setTextColor(fb_pixel_t color_text){ cl_col_text = color_text;}

		///set height of clock on screen
		virtual void setHeight(const int& h);
		///set width of clock on screen
		virtual void setWidth(const int& w);

		///use string expession: "%H:%M" = 12:22, "%H:%M:%S" = 12:22:12
		///set current time format string, 1st parameter set the default format, 2nd parameter sets an alternatively format for use as blink effect
		virtual void setClockFormat(const char* prformat_str, const char* secformat_str = NULL);

		///start and paint ticking clock
		virtual bool Start(bool do_save_bg = CC_SAVE_SCREEN_NO);
		///same like Start() but for usage as simple call without return value
		virtual void unblock(/*bool do_save_bg = CC_SAVE_SCREEN_NO*/){Start(cc_save_bg);}
		///stop ticking clock, but don't hide, use kill() or hide() to remove from screen
		virtual bool Stop();
		///same like Stop() but for usage as simple call without return value
		virtual void block(){Stop();}
		///return true on blocked status, blocked means clock can be initalized but would be not paint, to unblock use unblock()
		virtual bool isBlocked(void) {return !paintClock;}

		///returns true, if clock is running
		virtual bool isRun() const {return cl_timer ? true : false;};
		///set refresh interval in seconds, default value=1 (=1 sec)
		virtual void setClockIntervall(const int& seconds){cl_interval = seconds;};

		///show clock on screen
		virtual void paint(bool do_save_bg = CC_SAVE_SCREEN_YES);
		///hide clock on screen
		virtual void hide(){Stop(); CComponentsForm::hide();}
		///does the same like kill() from base class, but stopping clock before kill
		void kill(const fb_pixel_t& bg_color = COL_BACKGROUND_PLUS_0, bool ignore_parent = false);

		///reinitialize clock contents
		virtual void refresh() { initCCLockItems(); }
		///allows to save bg screen behind text within segment objects, see also cl_save_segment_screen
		void enableSegmentSaveScreen(bool mode);

		///set color gradient on/off, returns true if gradient mode was changed
		virtual bool enableColBodyGradient(const int& enable_mode, const fb_pixel_t& sec_color = 255 /*=COL_BACKGROUND*/);
#if 0
		///enable/disable automatic blitting
		void setBlit(bool _may_blit = true) { may_blit = _may_blit; }
#endif
};

#endif
