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

#pragma hdrstop

#include "VmsUtilsF.h"
#include "CDSFormF.h"
#include "CDSFontF.h"
#include "VMSCommLibF.h"

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

#pragma package(smart_init)

bool AssignBmp(TImage *AImage, TStream *AStream)
{
    bool bResult = true;
	try
	{
		try
		{
            AImage->Picture->Bitmap->PixelFormat = g_AppCfg.PixelFormat;//pf24bit;
            AImage->Picture->Bitmap->LoadFromStream(AStream);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN AssignBmp Failed: %s, %s", AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
	}
    return bResult;
}
//---------------------------------------------------------------------------

bool AssignGif(TImage *AImage, TStream *AStream)
{
    bool bResult = true;
	TGIFImage *pGif = new TGIFImage();
	try
	{
		try
		{
			pGif->LoadFromStream(AStream);
			AImage->Picture->Assign(pGif);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN AssignGif Failed: %s, %s", AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
		if (pGif) {
			delete pGif;
		}
	}
    return bResult;
}
//---------------------------------------------------------------------------

bool AssignJpg(TImage *AImage, TStream *AStream)
{
    bool bResult = true;
	TJPEGImage *pJpg = new TJPEGImage();
	try
	{
		try
		{
			pJpg->LoadFromStream(AStream);
			AImage->Picture->Assign(pJpg);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN AssignJpg Failed: %s, %s", AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
		if (pJpg) {
			delete pJpg;
		}
    }
    return bResult;
}
//---------------------------------------------------------------------------

bool SaveToFileBmp(TImage *AImage, AnsiString AFileName)
{
    bool bResult = true;
	try
	{
		try
		{
			AImage->Picture->SaveToFile(AFileName);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN SaveToFileBmp Failed: %s, %s, %s", AFileName.c_str(), AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
    }
    return bResult;
}
//---------------------------------------------------------------------------

bool SaveToFileGif(TImage *AImage, AnsiString AFileName)
{
    bool bResult = true;
	TGIFImage *pGif = new TGIFImage();
	try
	{
		try
		{
			pGif->Assign(AImage->Picture);
			pGif->SaveToFile(AFileName);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN SaveToFileGif Failed: %s, %s, %s", AFileName.c_str(), AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
		if (pGif) {
			delete pGif;
		}
    }
    return bResult;
}
//---------------------------------------------------------------------------

bool SaveToFileJpg(TImage *AImage, AnsiString AFileName)
{
    bool bResult = true;
	TJPEGImage *pJpg = new TJPEGImage();
	try
	{
		try
		{
			pJpg->Assign(AImage->Picture);
			pJpg->SaveToFile(AFileName);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN SaveToFileJpg Failed: %s, %s, %s", AFileName.c_str(), AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
		if (pJpg) {
			delete pJpg;
		}
    }
    return bResult;
}
//---------------------------------------------------------------------------

bool SaveToStreamGif(TImage *AImage, TStream *AStream)
{
    bool bResult = true;
	TGIFImage *pGif = new TGIFImage();
	try
	{
		try
		{
			pGif->Assign(AImage->Picture);
			pGif->SaveToStream(AStream);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN SaveToStreamGif Failed: %s, %s", AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
		if (pGif) {
			delete pGif;
		}
    }
    return bResult;
}
//---------------------------------------------------------------------------

bool SaveToStreamJpg(TImage *AImage, TStream *AStream)
{
    bool bResult = true;
	TJPEGImage *pJpg = new TJPEGImage();
	try
	{
		try
		{
			pJpg->Assign(AImage->Picture->Bitmap);
			pJpg->SaveToStream(AStream);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN SaveToStreamJpg Failed: %s, %s", AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
		if (pJpg) {
			delete pJpg;
		}
    }
    return bResult;
}
//---------------------------------------------------------------------------
bool SaveToStreamJpg(Graphics::TBitmap *ABitmap, TStream *AStream)
{
    bool bResult = true;
	TJPEGImage *pJpg = new TJPEGImage();
	try
	{
		try
		{
			pJpg->Assign(ABitmap);
			pJpg->SaveToStream(AStream);
		}
		catch(Exception &e)
		{
            bResult = false;
            MERROR("MAIN SaveToStreamJpg Failed: %s, %s", AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str());
		}
	}
	__finally
	{
		if (pJpg) {
			delete pJpg;
		}
    }
    return bResult;
}
//---------------------------------------------------------------------------

int GradeToColorCode(int AGrade)
{
	int nColor = color_green;
	switch(AGrade)
	{
	//case grade_none:       nColor = color_black; break;
	case grade_smoothness: nColor = color_green; break;
	case grade_delay:      nColor = color_amber; break;
	case grade_congestion: nColor = color_red;   break;
	}
	return nColor;
}
//---------------------------------------------------------------------------

TColor GradeToFillColor(int AGrade)
{
	TColor clColor;
#if 0
    clColor = (TColor)g_AppCfg.nDefaultFillColor;
#else
    clColor = clGray;//clBlack;
#endif
	switch(AGrade)
	{
	case grade_smoothness: clColor = clLime;   break;
	case grade_delay:      clColor = clYellow; break;
	case grade_congestion: clColor = clRed;    break;
	}
	return clColor;
}
//---------------------------------------------------------------------------

AnsiString GradeToString(int AGrade, int AType)
{
	AnsiString sGrade = " ";
    if (AType == eFormtp_figure)
    {
        switch(AGrade)
        {
        case grade_smoothness: sGrade = FIGURE.GRADE1; break;
        case grade_delay:      sGrade = FIGURE.GRADE2; break;
        case grade_congestion: sGrade = FIGURE.GRADE3; break;
        }
	}
    else
    {
        switch(AGrade)
        {
        case grade_smoothness: sGrade = TEXT.GRADE1; break;
        case grade_delay:      sGrade = TEXT.GRADE2; break;
        case grade_congestion: sGrade = TEXT.GRADE3; break;
        }
    }
    return sGrade;
}
//---------------------------------------------------------------------------

TColor CodeToFormColor(int ACode)
{
	TColor clColor = clBlack;
	switch(ACode)
	{
	case color_black: clColor = clBlack;  break;
	case color_red:   clColor = clRed;    break;
	case color_green: clColor = clLime;   break;
	case color_amber: clColor = clYellow; break;
	}
	return clColor;
}
//---------------------------------------------------------------------------

TColor CodeToBackColor(int ACode)
{
	TColor clColor = clBlack;
	switch(ACode)
	{
	case color_black: clColor = clBlack;  break;
	case color_red:   clColor = clRed;    break;
	case color_green: clColor = clLime;   break;
	case color_amber: clColor = clYellow; break;
	}
	return clColor;
}
//---------------------------------------------------------------------------

TColor CodeToFontColor(int ACode)
{
    TCDSFontColor *pObj = CDSFontManager->FColors.Find(ACode);
    if (pObj)
    {
        return (TColor)pObj->VMS_FONT_COLR_VAL;
    }

	TColor clColor = clBlack;
	switch(ACode)
	{
	case color_black: clColor = clBlack;  break;
	case color_red:   clColor = clRed;    break;
	case color_green: clColor = clLime;   break;
	case color_amber: clColor = clYellow; break;
	}
	return clColor;
}
//---------------------------------------------------------------------------

AnsiString CodeToFontName(int ACode)
{
    TCDSFontName *pObj = CDSFontManager->FNames.Find(ACode);
    if (pObj)
    {
        return pObj->VMS_FONT_NAME_NM;
    }

	AnsiString sFont = "¸¼Àº °íµñ";
	switch(ACode)
	{
	case font_malgun:  sFont = "¸¼Àº °íµñ"; break;
	case font_hygmj:   sFont = "HY°ß¸íÁ¶";  break;
	case font_gulim:   sFont = "±¼¸²Ã¼";    break;
	case font_gungsuh: sFont = "±Ã¼­Ã¼";    break;
	case font_dotum:   sFont = "µ¸¿òÃ¼";    break;
	case font_batang:  sFont = "¹ÙÅÁÃ¼";    break;
	}
	return sFont;
}
//---------------------------------------------------------------------------

