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

#include <vcl.h>
#include <jpeg.hpp>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "WindowMsgF.h"
#include "ITSLangTransF.h"
#include "ITS_OPLibF.h"
#pragma hdrstop

#include "PRK0011MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtonEdit"
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinsCore"
#pragma resource "*.dfm"

TPRK0011M *PRK0011M = NULL;
//---------------------------------------------------------------------------

__fastcall TPRK0011M::TPRK0011M(TComponent* Owner, TImage *APRK_PLCE_IMAGE_DATA, String APRK_PLCE_IMAGE_TYPE)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
    Position = poOwnerFormCenter;

    FTitle = Caption;//"VMS 소통정보 배경이미지관리";

    ScrollBox1->Color = Color;
    FPRK_PLCE_IMAGE_DATA = APRK_PLCE_IMAGE_DATA;
    FPRK_PLCE_IMAGE_TYPE = APRK_PLCE_IMAGE_TYPE;

    FSelected = false;
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::CommClose()
{
    try
    {
        //CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::FormShow(TObject *Sender)
{
    Refresh();

    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::FormInit()
{
    EdImageFileName->Text = "";

    try {
        PRK_PLCE_IMAGE_DATA->Picture->Bitmap->FreeImage();
        PRK_PLCE_IMAGE_DATA->Picture->Bitmap = NULL;

        PRK_PLCE_IMAGE_DATA->Picture->Assign(FPRK_PLCE_IMAGE_DATA->Picture);
    } catch(Exception &e) {
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    PRK0011M = NULL;
    Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::EdImageFileNameClick(TObject *Sender)
{
    OpenBmp->Title    = FTitle;
    OpenBmp->FileName = "";
    OpenBmp->Execute();
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::OpenBmpCanClose(TObject *Sender, bool &CanClose)
{
    String sFName = OpenBmp->FileName;

    try
    {
        ClearImage();

        EdImageFileName->Text = sFName;

        TMemoryStream *pImgStream = new TMemoryStream();
        pImgStream->LoadFromFile(sFName);

        if (AssignPrkFlrImage(sFName.SubString(sFName.Length()-2, 3), pImgStream)) {
            //FImageUpdate = true;
        }
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::ClearImage() {
    try {
        PRK_PLCE_IMAGE_DATA->Picture->Bitmap->FreeImage();
        PRK_PLCE_IMAGE_DATA->Picture->Bitmap = NULL;
    } catch(Exception &e) {}
}
//---------------------------------------------------------------------------
bool __fastcall TPRK0011M::AssignPrkFlrImage(String AExt, TMemoryStream *AImgStream)
{
    AExt = AExt.UpperCase();
    //FImageExt = AExt;
    try {
        try {
            if (AExt == "PNG") {
                TdxPNGImage *img = new TdxPNGImage;
                img->LoadFromStream(AImgStream);

                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->Assign(img);
                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->PixelFormat = pf24bit;

                SAFE_DELETE(img);
                FPRK_PLCE_IMAGE_TYPE = "3";
            }
            else
            if (AExt == "JPG" || AExt == "JPEG") {
                TJPEGImage *img = new TJPEGImage();
                img->LoadFromStream(AImgStream);

                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->Assign(img);
                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->PixelFormat = pf24bit;

                SAFE_DELETE(img);
                FPRK_PLCE_IMAGE_TYPE = "2";
            }
            else
            if (AExt == "BMP") {
                Graphics::TBitmap *img = new Graphics::TBitmap;
                img->LoadFromStream(AImgStream);

                PRK_PLCE_IMAGE_DATA->Picture->Bitmap->Assign(img);

                SAFE_DELETE(img);
                FPRK_PLCE_IMAGE_TYPE = "0";
            }
            else {
                return false;
            }
        } catch(Exception &e) {
            return false;
        }
    } __finally {
        SAFE_DELETE(AImgStream);
    }
    return true;
}
//---------------------------------------------------------------------------
void __fastcall TPRK0011M::BtnSaveImgFileClick(TObject *Sender)
{
    SaveDialog->Title      = FTitle;
    SaveDialog->InitialDir = ExtractFilePath(Application->ExeName);
    SaveDialog->Filter     = "Bitmap image Files (*.bmp)|*.bmp";

    if (SaveDialog->Execute())
    {
        try {
            String sFileName = SaveDialog->FileName;

            int iRet = sFileName.Pos(".bmp");
            if (iRet == 0)
            {
                sFileName = SaveDialog->FileName + ".bmp";
            }

            PRK_PLCE_IMAGE_DATA->Picture->SaveToFile(sFileName);
        }
        catch (Exception &exception)
        {
            Application->ShowException(&exception);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0011M::BtnSelectClick(TObject *Sender)
{
    FSelected = true;
    Close();
}
//---------------------------------------------------------------------------

