#include "wsTimerThread.h"
#include <iostream>

namespace YA
{
	wsTimerThread::wsTimerThread()
	{
		__Reset();
	}


	wsTimerThread::~wsTimerThread()
	{
		Stop();
	}

	void wsTimerThread::Start()
	{
		__Running = true;
		__Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) );
		__Thread->detach();
	}

	void wsTimerThread::Stop()
	{
		__Enable = false;

		boost::unique_lock<boost::mutex> lock( __ExitMutex );
		__ExitCond.wait( lock );

		__Running = false;
	}

	void wsTimerThread::__Reset()
	{
		__Enable  = true;
		__Running = false;
	}

	void wsTimerThread::__ChkConfig()
	{
		if ( __Config.SendInterval < MIN_SEND_INTERVAL )
		{
			__Config.SendInterval = MIN_SEND_INTERVAL;
		}
	}
	void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
	{
		while ( pOwner->__Enable )
		{
			//do something...
			printf("doing ....timer thread .......\n");
			boost::this_thread::sleep( boost::posix_time::millisec( 1000 ) );
		}

		pOwner->__ExitCond.notify_one();
	}



	void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
	{
		__Config = Config;
		__ChkConfig();
	}

	void wsTimerThread::add_card_pos( const _CARD_POS_ & pos )
	{
		__CardPosList.insert( pos.ID, pos );
	}

	void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
	{
		__CardPosList.update( pos.ID, pos );
	}

	void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
	{
		__CardPosList.erase( pos.ID );
	}
}
