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

#include <vcl.h>
#pragma hdrstop

#include "FrameFlexWatchF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxGraphics"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinsCore"
#pragma link "ImageBtn"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxLabel"
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdHTTP"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma link "cxTextEdit"
#pragma resource "*.dfm"
TFrameFlexWatch *FrameFlexWatch;
//---------------------------------------------------------------------------
__fastcall TFrameFlexWatch::TFrameFlexWatch(TComponent* Owner)
    : TFrame(Owner)
{
    FStrmPlay = false;

    reMsg->Lines->Clear();
    FStrmHandle = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFrameFlexWatch::BtnWebCamControlMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    TImageBtn *pBtn = (TImageBtn*)Sender;

    String sPTZSpeed = String(PTZSpeed->Position);
    FStrmHttp = EdHttpUrl->Text.Trim();
    if (FStrmHttp == "")
    {
        return;
    }

    // Network Camera, goform, goahead
    try
    {
        String sCmdHtml = "";
        TImageBtn *pBtn = (TImageBtn*)Sender;

        switch(pBtn->Tag)
        {
        case  1: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x107&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Tilt Up
        case  2: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x101&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Tilt Down
        case  3: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x103&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Pan left
        case  4: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x105&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Pan right
        case  9: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x10B&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Zoom In
        case 10: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x10C&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Zoom Out
        case 12: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x10E&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Focus In
        case 13: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x10D&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Focus Out
        default: return;
        }

        if (sCmdHtml == "") return;

        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions >> hoInProcessAuth;
        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions << hoInProcessAuth;
        String sResult = IdHTTP1->Get(sCmdHtml);
        reMsg->Lines->Add(sResult);
        IdHTTP1->Disconnect();
    }
    catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TFrameFlexWatch::BtnWebCamControlMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    TImageBtn *pBtn = (TImageBtn*)Sender;

    String sPTZSpeed = String(PTZSpeed->Position);
    FStrmHttp = EdHttpUrl->Text.Trim();
    if (FStrmHttp == "")
    {
        return;
    }

    try
    {
        String sCmdHtml = "";

        switch(pBtn->Tag)
        {
        case  1: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x207&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Tilt Up
        case  2: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x201&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Tilt Down
        case  3: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x203&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Pan left
        case  4: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x205&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Pan right
        case  9: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x20B&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Zoom In
        case 10: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x20C&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Zoom Out
        case 12: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x20E&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Focus In
        case 13: sCmdHtml.printf(L"%s/goform/app/FwPtzCtr?FwModId=0&PortId=0&PtzCode=0x20D&PtzParm=%s&FwCgiVer=0x0001", FStrmHttp.c_str(), sPTZSpeed.c_str()); break;  // Focus Out
        default: return;
        }

        if (sCmdHtml == "") return;

        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions >> hoInProcessAuth;
        IdHTTP1->HTTPOptions = IdHTTP1->HTTPOptions << hoInProcessAuth;
        String sResult = IdHTTP1->Get(sCmdHtml);
        reMsg->Lines->Add(sResult);
        IdHTTP1->Disconnect();
    }
    catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TFrameFlexWatch::OpenStream()
{
    FStrmUrl = EdStreamUrl->Text.Trim();
    FStrmUser = EdStreamId->Text.Trim();
    FStrmPswd = EdStreamPswd->Text.Trim();

    AcesTechXPlayer21->URL = FStrmUrl;
    FStrmHandle = AcesTechXPlayer21->ConnectAsync();
    if (FStrmHandle > 0)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrameFlexWatch::CloseStream()
{
    if (FStrmHandle > 0)
    {
        AcesTechXPlayer21->Close();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrameFlexWatch::BtnCameraConDisconnClick(TObject *Sender)
{
    TcxButton *btn = (TcxButton*)Sender;
    if (btn->Tag == 1) {
        // 연결
        btn->Tag = 0;
        btn->Caption = "영상 연결 해제";
        OpenStream();
    }
    else {
        // 연결 종료
        btn->Tag = 1;
        btn->Caption = "영상 연결";
        CloseStream();
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrameFlexWatch::IdHTTP1Authorization(TObject *Sender, TIdAuthentication *Authentication, bool &Handled)
{
#if 1
    IdHTTP1->Request->Username = FStrmUser;
    IdHTTP1->Request->Password = FStrmPswd;
#endif

    Authentication->Username = FStrmUser;
    Authentication->Password = FStrmPswd;

    TIdDigestAuthentication *pAuth1 = (TIdDigestAuthentication*)IdHTTP1->Request->Authentication;
    TIdDigestAuthentication *pAuth2 = (TIdDigestAuthentication*)Authentication;
    pAuth1->Uri = IdHTTP1->Request->URL;
    pAuth2->Method = "GET";

    Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TFrameFlexWatch::reMsgDblClick(TObject *Sender)
{
    reMsg->Lines->Clear();
}
//---------------------------------------------------------------------------

