// VideoWnd.cpp : implementation file
//

#include "stdafx.h"
#include "taxi4u.h"
#include "Common.h"
#include "VideoWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CVideoWnd

HANDLE  g_hPlayNextHandle    =CreateEvent(NULL,FALSE,FALSE,NULL);
HANDLE  g_hPlayVideoThread    =NULL;  //The process handle for normal playlist
HANDLE  g_hPlayTriggThread    =NULL;  //The process handle for trigger playlist
HANDLE  g_hPlayBtnAdThread    =NULL;  //The process handle for news playlist
BOOL  g_bExitPlayThread    =TRUE;
BOOL  g_bPlayNews        =FALSE;

DWORD WINAPI PlayVideoProc(PVOID pArg);
DWORD WINAPI PlayTriggProc(PVOID pArg);
DWORD WINAPI PlayBtnAdProc(PVOID pArg);

extern CConfig    g_Config;      // The config file
extern CScheduler*  g_pSchedule;
extern CButtonWnd*  g_pButtonWnd;
extern BOOL      g_bCustomerOn;
extern BOOL      g_bResetTriggerPlay;
extern BOOL      g_bUserChangedSetting;

CVideoWnd::CVideoWnd(HWND parent,CAdWnd*pAdWnd,CBottomAdWnd*pAdBottomWnd)
{
  MediaPlayerControl(CMD_LAUNCHPLAYER);
  m_hParentWnd = parent;
  m_pAdWnd=pAdWnd;
  m_pAdBottomWnd=pAdBottomWnd;
  m_pCurrentVideo=NULL;
  m_bTriggeredPlay=FALSE;
}

CVideoWnd::~CVideoWnd()
{
  //Release handle
  MediaPlayerControl(CMD_CLOSEPLAYER);
  ClosePlayer();
}

void CVideoWnd::StartPlayer()
{
  if(g_bExitPlayThread)
  {
    g_bExitPlayThread=FALSE;
    g_hPlayVideoThread = CreateThread(NULL,0,PlayVideoProc,this,0,NULL);
    g_hPlayTriggThread = CreateThread(NULL,0,PlayTriggProc,this,0,NULL);
    g_hPlayBtnAdThread = CreateThread(NULL,0,PlayBtnAdProc,this,0,NULL);
  }  
}

void CVideoWnd::ClosePlayer()
{
  if(!g_bExitPlayThread)
  {
    g_bExitPlayThread=TRUE;
    SetEvent(g_hPlayNextHandle);
    WaitForSingleObject(g_hPlayVideoThread,INFINITE);
    WaitForSingleObject(g_hPlayTriggThread,INFINITE);
    WaitForSingleObject(g_hPlayBtnAdThread,INFINITE);
  }  
}

void CVideoWnd::Stop()
{
  MediaPlayerControl(CMD_STOP);
  m_pAdBottomWnd->Stop();
  m_pAdWnd->Stop();
  m_pAdBottomWnd->SetPlayList(NULL);
  m_pAdWnd->SetPlayList(NULL);
  m_bTriggeredPlay=FALSE;
}

void CVideoWnd::PlayContent(CVideoItem* pVideo)
{
  if (pVideo)
  {
    if (pVideo->m_ResourceType == BINTYPE)  
      this->PlayExecutable(pVideo);
    else
      this->PlayMedia(pVideo);
  }
}

void CVideoWnd::PlayNext()
{
  SetEvent(g_hPlayNextHandle);
}

void CVideoWnd::PlayMedia(CVideoItem* pVideo)
{
  if (pVideo && pVideo->m_ResourceType == VIDEOTYPE)
  {
    if (g_pButtonWnd && !g_bUserChangedSetting)
    {
      //reset sound level if user has no preference
      //g_pButtonWnd->SetSoundLevel(g_Config.m_SoundLevel.DefaultNightLevel);
      
      //set sound level base on xml settings
      if (!pVideo->m_AudioLevel.IsEmpty())
        g_pButtonWnd->SetSoundLevel(_ttoi(pVideo->m_AudioLevel));
    }
    this->PlayVideo(pVideo);
    
    if (!g_bPlayNews)
    this->PlayBottomAd(pVideo);
  }
}

void CVideoWnd::PlayVideo(CVideoItem* pVideo)
{
  m_pAdWnd->Stop();
  m_pAdWnd->SetPlayList(pVideo->m_RightPictureList);  
  MediaPlayerPlayFile(pVideo->m_FilePath,FALSE);
  
  UINT wait=10;
  Sleep(10);
  if(m_pCurrentVideo==NULL)
  {
    wait+=500;
  }
  Sleep(wait);
  
  ::SendMessage(m_hParentWnd,WM_VIDEO_PLAYNEXT,(WPARAM)pVideo,0);
  m_pCurrentVideo=pVideo;    
  m_pAdWnd->Start();  // include play right ad
}

void CVideoWnd::PlayBottomAd(CVideoItem* pVideo)
{
  m_pAdBottomWnd->Stop();  
  m_pAdBottomWnd->SetPlayList(pVideo->m_BottomPictureList);
  m_pAdBottomWnd->Start();
}

void CVideoWnd::PlayExecutable(CVideoItem* pExe)
{
  // stop videos and triggered effects
  this->m_bTriggeredPlay=TRUE;
  this->Stop();

  //MediaPlayerControl(CMD_CLOSEPLAYER);
  KillProcess(pExe->m_GUID);
  Sleep(500);

  if (StartProcess(pExe->m_FilePath))
  {
    do 
    {
      Sleep(500);
    }  while (IsProcessRunning(pExe->m_GUID));
  }
  ::SetForegroundWindow(this->m_hParentWnd);

  //Starts play video
  //if (pExe->m_GUID.Compare(L"") != 0)    // TO DO
  {
    if(g_bCustomerOn)
    {
      ::PostMessage(this->m_hParentWnd,WM_CONTINUE_PLAYING,0,0);
    }
  }  
  //MediaPlayerControl(CMD_LAUNCHPLAYER);
  this->m_bTriggeredPlay=FALSE;
}

DWORD WINAPI PlayVideoProc(PVOID pArg)
{
  MYTRACE(L"Create Play video thread");

  CVideoWnd* pVideoWnd = (CVideoWnd*) pArg;
  CVideoItem* pVideo;

  while(!g_bExitPlayThread)
  {
    if(WaitForSingleObject(g_hPlayNextHandle,INFINITE) == WAIT_OBJECT_0 && !g_bExitPlayThread)
    {
      ResetEvent(g_hPlayNextHandle);

      //Play special videos
      pVideo = g_pSchedule->m_PlayList.m_SpecialVideoList.Next();
      if(pVideo != NULL)
      {
        MYTRACE(L"Playing from %s: %s",g_pSchedule->m_PlayList.m_SpecialVideoList.m_GUID,pVideo->m_FilePath);
        LOGDATA(LOG_TYPE_CUST, L"%s|%d|%d", pVideo->m_ContentId, CButtonWnd::c_iCurrAudiLevel, CButtonWnd::c_iCurrBrtnLevel);
        pVideoWnd->PlayContent(pVideo);
        continue;
      }
    
      //Play Normal Videos
      pVideo = g_pSchedule->m_PlayList.m_NormalVideoList.Next();
      if(pVideo != NULL)
      {
        MYTRACE(L"Playing from %s: %s",g_pSchedule->m_PlayList.m_NormalVideoList.m_GUID,pVideo->m_FilePath);
        LOGDATA(LOG_TYPE_NORM, L"%s|%d|%d", pVideo->m_ContentId, CButtonWnd::c_iCurrAudiLevel, CButtonWnd::c_iCurrBrtnLevel);
        pVideoWnd->PlayContent(pVideo);
        continue;
      }
    }
  }
  MYTRACE(L"Exit Play video thread");

  return 1;
}

DWORD WINAPI PlayTriggProc(PVOID pArg)
{
  MYTRACE(L"Create Play trigger thread");

  CVideoWnd* pVideoWnd = (CVideoWnd*) pArg;
  CVideoItem* pVideo;

  while(!g_bExitPlayThread)
  {
    if (g_bCustomerOn)
    {
      //Play trigger videos
      pVideo = g_pSchedule->m_PlayList.m_TriggerVideoList.Next(pVideoWnd->m_bTriggeredPlay);
      if(pVideo != NULL)
      {
        //MYTRACE(L"Playing from %s: %s",g_pSchedule->m_PlayList.m_TriggerVideoList.m_GUID,pVideo->m_FilePath);
        pVideoWnd->Stop();
        g_bResetTriggerPlay=TRUE;
        pVideoWnd->m_bTriggeredPlay=TRUE;
        pVideoWnd->PlayMedia(pVideo);
      }
    }
    Sleep(5000);

    //LOGDATA(LOG_TYPE_NMEA, CGPRMCData::c_GprmcData);
    LogLocationData();
  }
  MYTRACE(L"Exit Play trigger thread");
  return 1;
}

DWORD WINAPI PlayBtnAdProc(PVOID pArg)
{
  MYTRACE(L"Create Play trigger ad thread");
  
  CVideoWnd* pVideoWnd = (CVideoWnd*) pArg;
  CVideoItem* pVideo;
  
  while(!g_bExitPlayThread)
  {
    if (g_bCustomerOn)
    {
      //Play trigger ad videos
      pVideo = g_pSchedule->m_PlayList.m_TriggerAdVideoList.Next();
      if(pVideo != NULL)
      {        
        g_bPlayNews=TRUE;
        g_bResetTriggerPlay=TRUE;
        pVideoWnd->PlayBottomAd(pVideo);
        LOGDATA(LOG_TYPE_NEWS, L"%s|%d|%d", pVideo->m_ContentId, CButtonWnd::c_iCurrAudiLevel, CButtonWnd::c_iCurrBrtnLevel);
      }
    }
    Sleep(1000);
  }
  MYTRACE(L"Exit Play trigger ad thread");
  return 1;
}