// nlib_driver.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"

#include <qwerty\windebuger.h>

#include <shareCookie\shareCookie.h>
#include <shareCookie\browserType.h>
#include <nlib\nlib.h>
#include <nlib\nlib_define.h>



//ライブラリリンク
#pragma comment(lib,"Wininet.lib")
#pragma comment(lib,"WinHttp.lib")
#pragma comment(lib,"Iepmapi.lib")
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib, "shell32.lib" )
#pragma comment(lib,"expat.lib")
#pragma comment(lib,"sqlite3.lib")
#pragma comment(lib,"shareCookie.lib")
#pragma comment(lib,"nlib.lib")
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"JSONParser.lib")


NicoLiveStream_P nl;
NicoVideoAuth_P nva;
WSAData wsaData;
HINTERNET hInternet = WinHttpOpen(L"nicoliveTest",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,(LPCWSTR)WINHTTP_NO_PROXY_NAME,(LPCWSTR)WINHTTP_NO_PROXY_BYPASS,0);

int nicoLive_setup(void) {

	INT_PTR err;
	INT_PTR count = 0;


	//WSADataを初期化
	while((err = WSAStartup(MAKEWORD(2,0),&wsaData)) == WSASYSNOTREADY){

		count++;
		if(count >= 5)break;
		Sleep(500);



	}

	if(err != ERROR_SUCCESS){

		return NLIB_INITALIZE_FAILED_NETWORK;
	}
	InitializeNLIB(hInternet);
	nl = NicoLiveStream_new();
	nva = NicoVideoAuth_new();
	NicoVideoAuth_getNicoVideoAuth(nva,BT_GOOGLE_CHROME);
	return nl != NULL ? 0 : 1;
}

int nicoLive_teardown(void) {
  NicoLiveStream_delete(&nl);
  NicoVideoAuth_delete(&nva);
  FinalizeNLIB();
  WinHttpCloseHandle(hInternet);
  WSACleanup();
  return 0;
}




NLIB_RESULT callback(NICOLIVE_EVENT eventType,NicoLiveStream_P pNicoLiveStream,LPVOID option,NICOLIVE_PARAM param1,NICOLIVE_PARAM param2){


	switch(eventType){

	case NICOLIVE_EVENT_ERR_CONNECT:

		errprint(TEXT("通信エラー発生"));

		break;


	case NICOLIVE_EVENT_RECEVE_CONNECTRESULT:

		println(TEXT("receveStart::last_res[%s],thread[%s],ticket[%s],server_time[%s]"),((NicoLiveThreadComment_P)param1)->last_res,((NicoLiveThreadComment_P)param1)->thread,((NicoLiveThreadComment_P)param1)->ticket,((NicoLiveThreadComment_P)param1)->server_time);
		
		break;

	case NICOLIVE_EVENT_RECEVE_CHAT:

		{
			int i = 0;
			NicoLiveChat_P chatP = (NicoLiveChat_P)param1;
			int index = chatP->mailCount;
			println(TEXT("recevechat::no[%s],locale[%s],vpos[%s],date[%s],premium[%s],anonymity[%s],thread[%s],user_id[%s],name[%s]"),chatP->no,chatP->locale,chatP->vpos,chatP->date,chatP->premium,chatP->anonymity,chatP->thread,chatP->user_id,chatP->name);



			for(;i < index;i++){

				println(TEXT("mail(%d)[%s]"),i,chatP->mail[i]);

			}

			println(TEXT("chatBody::%s"),chatP->chatBuf);

		}
		break;

	case NICOLIVE_EVENT_RECEVE_CHATSETTLE:


		
		println(TEXT("recevechatCount::no[%d],count[%d]"),param1,param2);
		param1;
		
		break;

	case NICOLIVE_EVENT_RECEVE_CHATSENDRESULT:

		{
			NicoLiveSendResultComment_P nls = (NicoLiveSendResultComment_P)param1;
			
			println(TEXT("sendresult::no[%s],status[%s],thread[%s]"),nls->no,nls->status,nls->thread);

		}

		break;

	case NICOLIVE_EVENT_SEND_HEARTBEAT:

		{

			NicoLiveHeartBeat_P hbp = (NicoLiveHeartBeat_P)param1;

			println(TEXT("heartbeat::time[%llu],watchCount[%d],commentCount[%d],is_restrict[%s],ticket[%s]"),hbp->time,hbp->watchCount,hbp->commentCount,hbp->is_restrict,hbp->ticket);
			//println(TEXT("heartbeat::watchCount[%d],commentCount[%d],is_restrict[%s],ticket[%s]"),hbp->watchCount,hbp->commentCount,hbp->is_restrict,hbp->ticket);
		

		}


		break;


	}

	return 0;
}

NLIB_RESULT myListCallBack(LPVOID option,NicoMyListItem_P pVideo){

//	dumpln(TEXT("video_id:%s"),pVideo->itemData.video_id);



	return NLIB_ERR_OK;
}

static NLIB_RESULT myListGroupCallBack(LPVOID option,NicoMyList_P pMyList){
	WCHAR buf[256];
	NLIB_RESULT rslt;
//	wcscpy(buf,pMyList->id);
	dumpln(TEXT("id:%s"),buf);
	rslt = NicoMyList_getMyList(nva,buf,myListCallBack,NULL);
	
	

	return NLIB_ERR_OK;
}

void nicoLive_connect(void) {
	
	dumpline();
	NicoLiveStrream_setResFrom(nl,1000);
	//NicoLiveStream_setHeartBeatMsec(nl,5000);
	//NicoLiveStream_setKeapALiveMsec(nl,5000);
	//NicoLiveStream_setKeapALiveFlag(nl,FALSE);
	
	
	WCHAR housoubuf[LENGTH_1024];
	std::wcin.getline(housoubuf,sizeof(housoubuf) / sizeof(housoubuf[0]));
	
	WCHAR buf[1024];
	
	

	for(int index = 0;index < 5;index++){
		check(NicoLiveStream_connect(nl,nva,housoubuf,callback,NULL) ==  NLIB_ERR_OK,TEXT("接続に失敗しました"));
//		NicoLiveStream_queryStreamInfo(nl);
		std::wcin.getline(buf,sizeof(buf) / sizeof(buf[0]));

		LPWSTR mail = L"184";
		LPWSTR exname = L"name";
		LPWSTR exvalue = L"qwerty";

		LPCWSTR mails[2] = {mail,NULL};
		LPCWSTR extends[3] = {exname,exvalue,NULL};
		

		while(wcslen(buf) > 0){

			NicoLiveStream_sendChat(nl,buf,mails,NULL);
			std::wcin.getline(buf,sizeof(buf) / sizeof(buf[0]));
		}
		NicoLiveStream_disConnect(nl);
	}
	
	check(_heapchk() == _HEAPOK,TEXT("ヒープエラーです"));
	
	//Sleep(3000);


	


	return;
}


void nicoLive_mylist(void) {

	

	//check(NicoMyList_getDefList(nva,myListCallBack,NULL) ==  NLIB_ERR_OK,TEXT("とりあえずマイリスト取得に失敗しました"));
	//check(NicoMyList_getMyListGroup(nva,myListGroupCallBack,NULL) == NLIB_ERR_OK,TEXT("マイリストグループ登録に失敗しました"));
	//check(NicoMyList_addItem(L"sm16046365") == NLIB_ERR_OK,TEXT("マイリスト登録に失敗しました!"));
	//check(_heapchk() == _HEAPOK,TEXT("heapエラーです"));
	dumpln(TEXT("heapchk:%d"),_heapchk());


}




int _tmain(int argc, _TCHAR* argv[])
{

	
	nicoLive_setup();
	


	nicoLive_connect();
	//nicoLive_mylist();
	
	nicoLive_teardown();


	_getch();
	return 0;
}