#include "wsClientMgr.h"
#include "log.h"
#include<thread>

namespace sys
{
	wsClientMgr::wsClientMgr()
	{

	}

	wsClientMgr::~wsClientMgr()
	{

	}

	void wsClientMgr::Build( const std::vector<std::string> uri_list, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList )
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		std::vector<std::string>::const_iterator vit_uri;

		int ID = 1;
		int Index = 0;
		for ( vit_uri = uri_list.begin(); vit_uri != uri_list.end(); vit_uri++ )
		{
			std::shared_ptr<wsClient> pClient = std::make_shared<wsClient>();
			pClient->init( ID, *vit_uri, MsgFuncList );
			__wsClientList.push_back( pClient );
			__uriIndexList.insert( std::make_pair( *vit_uri, Index ) );
			ID++;
			Index++;
		}
	}

	int wsClientMgr::connect( int time_out )
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		for ( size_t i = 0; i < __wsClientList.size() ; i++ )
		{
			if ( __wsClientList[i]->connect( time_out ) != 0 )
			{
				char szError[1024] = { 0 };
				sprintf( szError, "[%d] websocket client failed to connect: %s.", __wsClientList[i]->GetID(), __wsClientList[i]->get_uri().c_str() );
				__LastError = szError;
                std_error(szError);
				return -1;
			}
		}

		return 0;
	}

	void wsClientMgr::login()
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		for ( size_t i = 0; i < __wsClientList.size() ; i++ )
		{
			__wsClientList[i]->login();
		}
	}

	void wsClientMgr::send( const std::string & Cmd, const std::string & Data )
	{
		log_info("send2web:cmd=%s,data=%s",Cmd.c_str(),Data.c_str());

		std::lock_guard<std::recursive_mutex> lock( __lock );
		for ( size_t i = 0; i < __wsClientList.size() ; i++ )
		{
			__wsClientList[i]->send( Cmd, Data );
		}
	}

	std::shared_ptr<wsClient> wsClientMgr::GetClientByURI( const std::string & URI )
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		std::shared_ptr<wsClient> pClient;

		std::map<std::string, int>::iterator mit_uri;
		mit_uri = __uriIndexList.find( URI );
		if ( mit_uri != __uriIndexList.end() )
		{
			pClient = __wsClientList[mit_uri->second];
		}

		return pClient;
	}

	void wsClientMgr::close()
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		for ( size_t i = 0; i < __wsClientList.size() ; i++ )
		{
			__wsClientList[i]->close();
		}
	}

	bool wsClientMgr::IsConnected()
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		if ( __wsClientList.empty() )
		{
			return false;
		}

		for ( size_t i = 0; i < __wsClientList.size() ; i++ )
		{
			if ( !__wsClientList[i]->IsConnected() )
			{
				return false;
			}
		}

		return true;
	}

	bool wsClientMgr::IsLogined()
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		if ( __wsClientList.empty() )
		{
			return false;
		}

		for ( size_t i = 0; i < __wsClientList.size() ; i++ )
		{
			if ( !__wsClientList[i]->IsLogined() )
			{
				return false;
			}
		}

		return true;
	}

	bool wsClientMgr::IsSktOpened()
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		if ( __wsClientList.empty() )
		{
			return false;
		}

		for ( size_t i = 0; i < __wsClientList.size() ; i++ )
		{
			if ( !__wsClientList[i]->IsSktOpened() )
			{
				return false;
			}
		}

		return true;
	}

	void wsClientMgr::SendSpecialAreaProcess( const _BASE_CARD_ & stCard )
	{
		std::string strjson = __jsBuilder.BuildSpecialAreaProcess( stCard );
		send( JSON_CMD_VALUE_PUSH, strjson );
	}

    void wsClientMgr::Reconnect(std::shared_ptr<wsClient> & ws)
    {
		if (nullptr == ws)
			return;

		ws=ws->clone();
		log_info("wsClinet Reconnect : id[%d],url[%s]", ws->GetID(), ws->get_uri().c_str());

		if (ws->connect() == 0)
		{
			ws->login();
			log_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
//			std_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
		}
    }

    void wsClientMgr::Disconnet(std::shared_ptr<wsClient> & ws)
    {
		if (nullptr != ws)
		{
			ws->close();
		}
    }

    // @brief 检测是否有连接断开,并重连
	void wsClientMgr::CheckClientConnect(unsigned int cur_time)
	{
		std::lock_guard<std::recursive_mutex> lock( __lock );

		for( std::shared_ptr<wsClient>& w : __wsClientList)
		{
			unsigned int t = w->GetPingTime();
			// 间隔小于15秒 认为是正常

//			log_info("now=%ud,ping=%ud,now-ping=%d",cur_time,t,cur_time-t);
			if (w->IsConnected() && (t == 0 || cur_time - t < 15))
			{
				continue;
			}
			//重连
			Disconnet(w);
			std::this_thread::sleep_for(std::chrono::milliseconds(10));
			Reconnect(w);
			std::this_thread::sleep_for(std::chrono::seconds(3));
		}
	}
}