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

#include <vcl.h>
#include "FrmMainF.h"
#pragma hdrstop

#include "ThrDownloaderF.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(&UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall TThrDownloader::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------

long g_CompletedJobCnt = 0;

__fastcall TThrDownloader::TThrDownloader(bool CreateSuspended, bool delOldMap)
    : TThread(CreateSuspended)
{
    ::CoInitialize(NULL);
    FDelOldMap = delOldMap;
}
//---------------------------------------------------------------------------
void __fastcall TThrDownloader::Execute()
{
    String sUrl;
    String sFile;
    TIdHTTP *IdHTTP1 = new TIdHTTP(NULL);
    TIdSSLIOHandlerSocketOpenSSL *IdSSLIOHandler = NULL;

    String tmpUrl = FURL.UpperCase();
    bool isDaum = tmpUrl.Pos("DAUM") ? true : false;

    int nSuccess = 0;
    if (tmpUrl.Pos("HTTPS://")) {
        IdSSLIOHandler = new TIdSSLIOHandlerSocketOpenSSL(NULL);
        IdSSLIOHandler->SSLOptions->Method = sslvSSLv23;//sslvSSLv3;//sslvSSLv2;//sslvTLSv1;//sslvSSLv23;
        IdSSLIOHandler->SSLOptions->Mode = sslmClient;//sslmUnassigned;//sslmClient;
        IdHTTP1->HandleRedirects = false;
        IdHTTP1->IOHandler = IdSSLIOHandler;
    }

    POST_MSG(Application->MainForm->Handle, WM_DOWNLOAD_START, FXMin, 0);
    for (int ii = FXMin; ii <= FXMax; ii++)
    {
        for(int jj = FYMin; jj <= FYMax; jj++)
        {
            //if (isDaum) {
            //    sFile.printf(L"%s%d\\%d.%s", FPath.c_str(), jj, ii, FFileExt.c_str());
            //}
            //else {
                sFile.printf(L"%s%d\\%d.%s", FPath.c_str(), ii, jj, FFileExt.c_str());
            //}
            if (FDelOldMap && FileExists(sFile))
            {
                try {
                    DeleteFileA(sFile);
                } catch(Exception &e) {
                }
            }

            if (!FileExists(sFile))
            {
                if (isDaum) {
                    sUrl.printf(FURL.c_str(), FZoom, jj, ii);
                }
                else {
                    sUrl.printf(FURL.c_str(), FZoom, ii, jj);
                }

                TFileStream *fs = new TFileStream(sFile, fmCreate);
                try
                {
                    IdHTTP1->Get(sUrl, fs);
                    delete fs;
                    nSuccess++;
                }
                catch(Exception &e)
                {
                    if (fs != NULL) delete fs;
                }
            }
            else nSuccess++;

            InterlockedIncrement(&g_CompletedJobCnt);
            Sleep(10);
    //        POST_MSG(Application->MainForm->Handle, WM_DOWNLOAD_JOB, FX, nSuccess);
    #if 0
            int i=0;
            for(i=0; i<100; i++)
            {
                _asm
                {
                    NOP;
                };
            }
    #endif
        }
    }
    if (IdSSLIOHandler != NULL) delete IdSSLIOHandler;
    delete IdHTTP1;

    POST_MSG(Application->MainForm->Handle, WM_DOWNLOAD_COMPLETE, FXMin, nSuccess);
}
//---------------------------------------------------------------------------
