//---------------------------------------------------------------------------
#pragma hdrstop
#include "Unit4.h"
#include "Unit2.h"
#include <objbase.h>
#include <mshtml.h>
#include <urlmon.h>
#include <utilcls.h>
//---------------------------------------------------------------------------

void __fastcall THotelDescParser::GetHotelHtml (void)
{
  WideString wUrl = URL;
  HRESULT hr = S_OK;

  // 파싱에 사용할 IHTMLDocument2
  IHTMLDocument2 * pDoc = NULL;
  IHTMLElementCollection *pCollection=NULL;

  LPDISPATCH Document=NULL;
//  TLogFile* LogFile = new TLogFile("Unit4.txt");

  // MSHTML 객체 생성
  hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER,
        IID_IHTMLDocument2, (void **)&pDoc);
  if(FAILED(hr))
  {
    ShowMessage("MSHTML 생성 실패");
    return ;
  }

//  LogFile->WriteListParser(URL);

  // URL 로 부터 파일을 다운받아 MSHTML 과 연결할 URL 모니커를 생성
  IMoniker * pMoniker = NULL;
  hr = CreateURLMoniker(NULL, wUrl, &pMoniker);
  if(FAILED(hr))
  {
    ShowMessage("URL Moniker 생성 실패");
    return ;
  }

  // 모니커에서 사용할 바인드 컨텍스트를 생성
  IBindCtx * pBCtx= NULL;
  hr = CreateBindCtx(0, &pBCtx);
  if(FAILED(hr))
  {
    ShowMessage("IBindCtx 생성 실패");
    return ;
  }

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

  WideString wRdy;
  WideString w_InnerHtml;

  while(true)
    {
      Application->ProcessMessages();
      pDoc->get_readyState(&wRdy);
      if(wRdy == AnsiString("complete"))
        {
          pDoc->get_all(&pCollection);
          long Anchor_Cnt;
          pCollection->get_length(&Anchor_Cnt);
//          LogFile->WriteListParser(IntToStr(Anchor_Cnt));

          for ( int nIndex = 0;nIndex < Anchor_Cnt;nIndex++)
            {
              Variant vIndex((long)nIndex);
              Variant vNull;
              hr = pCollection->item(vIndex, vNull, &Document);
              Application->ProcessMessages();
              if  ( hr  == S_OK )
                {
                  if (Document != NULL)
                    {
                      IHTMLTable   *pTable;
                      IHTMLAnchorElement* pAnchor;
                      hr = Document->QueryInterface(IID_IHTMLAnchorElement,(void**)&pAnchor);
                      if ( hr == S_OK )
                        {
                          WideString w_Href;
                          AnsiString Href;
                          pAnchor->get_href(&w_Href);
                          Href = w_Href.c_bstr();
                          if ( Href.Pos("http://dps1.travelocity.com:80/hotsphoto.ctl?") > 0 )
                            ImgUrl = Href;
                          pAnchor->Release();
                        }

                      hr = Document->QueryInterface(IID_IHTMLTable,(void**)&pTable);
                      if ( hr == S_OK )
                        {
                          IHTMLElement *pElement;
                          hr = pTable->QueryInterface(IID_IHTMLElement,(void**)&pElement);
                          if ( hr == S_OK )
                            {
                              WideString w_outerHtml;
                              WideString w_innerHtml;
                              AnsiString outerHtml;
                              pElement->get_outerHTML(&w_outerHtml);
                              outerHtml = w_outerHtml.c_bstr();

                              if ( outerHtml.Pos("name=\"gray_header_bar\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  HotelName =  w_innerHtml.c_bstr();
                                  if ( HotelName.Pos("Avg Price") )
                                    HotelName = HotelName.SubString(0,HotelName.Pos("Avg Price"));
                                  HotelName = HotelName.TrimLeft();
                                }

                              if ( outerHtml.Pos("name=\"hotel_info\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Hotel_Info =  w_innerHtml.c_bstr();
                                  Hotel_Info = Hotel_Info.TrimLeft();
                                }

                              if ( outerHtml.Pos("name=\"Direction\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Directions = w_innerHtml.c_bstr();
                                }

                              if ( outerHtml.Pos("name=\"Dining\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Dining = w_innerHtml.c_bstr();
                                }

                              if ( outerHtml.Pos("name=\"Transportation\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Transportation = w_innerHtml.c_bstr();
                                }

                              if ( outerHtml.Pos("name=\"Property Information\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Property_Information = w_innerHtml.c_bstr();
                                }

                              if ( outerHtml.Pos("name=\"Services\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Services = w_innerHtml.c_bstr();
                                }

                              if ( outerHtml.Pos("name=\"Ratings_Awards\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Ratings_Awards = w_innerHtml.c_bstr();
                                }

                              if ( outerHtml.Pos("name=\"Ratings_Awards\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Ratings_Awards = w_innerHtml.c_bstr();
                                }

                              if ( outerHtml.Pos("name=\"Attractions\"") )
                                {
                                  pElement->get_innerText(&w_innerHtml);
                                  Attractions = w_innerHtml.c_bstr();
                                }
                            }

                          pTable->Release();
                        }
                    }
                  Document->Release();
                }
            }
          pCollection->Release();
          pDoc->Release();
          pMoniker->Release();
          pBCtx->Release();
          break;
        }
      Application->ProcessMessages();
    }
//  delete LogFile;
}
//---------------------------------------------------------------------------
void __fastcall THotelDescParser::HotelDescParser(void)
{
  GetHotelHtml();
}
//---------------------------------------------------------------------------
#pragma package(smart_init)
