//---------------------------------------------------------------------------
#pragma hdrstop

#include "Unit3.h"
#include <objbase.h>
#include <mshtml.h>
#include <urlmon.h>
#include <utilcls.h>
#include "Unit2.h"
#include <ExtCtrls.hpp>
#include "SHDocVw_OCX.h"
#include <OleCtrls.hpp>

//---------------------------------------------------------------------------

void __fastcall THotelListParser::HotelListParaser (AnsiString SeedUrl)
{
  hr = S_OK;
  pDoc = NULL;
  pAnchorCollection=NULL;
  Document=NULL;
  hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER,
                        IID_IHTMLDocument2, (void **)&pDoc);
  if(FAILED(hr))
    {
      ShowMessage("MSHTML 생성 실패");
      return ;
    }
  pMoniker = NULL;
  pBCtx= NULL;
  pPMoniker = NULL;

  WideString wUrl = SeedUrl;

  // URL 로 부터 파일을 다운받아 MSHTML 과 연결할 URL 모니커를 생성

  hr = CreateURLMoniker(NULL, wUrl, &pMoniker);
  if(FAILED(hr))
  {
    ShowMessage("URL Moniker 생성 실패");
    return ;
  }

  // 모니커에서 사용할 바인드 컨텍스트를 생성

  hr = CreateBindCtx(0, &pBCtx);
  if(FAILED(hr))
  {
    ShowMessage("IBindCtx 생성 실패");
    return ;
  }


  hr = pDoc->QueryInterface(IID_IPersistMoniker, (void **)&pPMoniker);
  if(SUCCEEDED(hr))
  {
    pPMoniker->Load(FALSE, pMoniker, pBCtx, STGM_READ);
  }

  AnsiString strHTML;
  WideString wRdy;
  WideString w_url;
  WideString w_Key;
  AnsiString Key;
  AnsiString url;

  while(true)
  {
    pDoc->get_readyState(&wRdy);
    if(wRdy == AnsiString("complete"))  break;
    Application->ProcessMessages();
  }
  pDoc->get_all(&pAnchorCollection);
  pDoc->Release();

  long Anchor_Cnt;
  pAnchorCollection->get_length(&Anchor_Cnt);
  for (int nIndex = 0; nIndex < Anchor_Cnt; nIndex++ )
    {
      Variant vIndex((long)nIndex);
      Variant vNull;
      hr = pAnchorCollection->item(vIndex, vNull, &Document);
      if  ( hr  == S_OK )
        {
          if (Document != NULL)
            {
              IHTMLAnchorElement *pAnchor;
              IHTMLElement *pElement;
              hr = Document->QueryInterface(IID_IHTMLAnchorElement,(void**)&pAnchor);
              if ( hr == S_OK )
                {
                  pAnchor->get_href(&w_url);

                  url = w_url.c_bstr();
                  if ( url != "" )
                    {
                      urlMap[url]++;
                      if ( url.Pos("hotsavail.ctl?") != 0  )
                        {
                          hr = pAnchor->QueryInterface(IID_IHTMLElement,(void**)&pElement);
                          if ( hr == S_OK )
                            {
                              pElement->get_innerText(&w_Key);
                              Key = w_Key.c_bstr();

                              if ( Key == "Next" )
                                nextUrlMap[url]++;
                              pElement->Release();

                            }
                        }
                    }
                  pAnchor->Release();
                }
              Document->Release();
            }
        }
      pAnchorCollection->Release();
    }

  pAnchorCollection->Release();
  pDoc->Release();
  pMoniker->Release();
  pBCtx->Release();

  map<AnsiString , int > :: iterator itr;
  AnsiString temp;
  for ( itr = urlMap.begin() ; itr != urlMap.end() ; itr ++ )
    {
      temp = itr->first;
      if ( temp.Pos("hotsdetail.ctl?") != 0 )
        UrlList->Add(temp);
    }

  for ( itr = nextUrlMap.begin() ; itr != nextUrlMap.end() ; itr++ )
    {
      temp = itr->first;
      if ( itr->second <= 2)
        {
          GoneQue.push_back(temp);
          itr->second++;
        }
    }
  ::CoUninitialize();
}
//---------------------------------------------------------------------------
void __fastcall THotelListParser::GetNextPage ( void )
{
  AnsiString url;
  if ( !GoneQue.empty() )
    {
      url = GoneQue[GoneQue.size()-1];
      GoneQue.pop_back();
      UrlList->Clear();
      HotelListParaser ( url );
    }
}
//---------------------------------------------------------------------------
#pragma package(smart_init)
