//---------------------------------------------------------------------------
// For SELMA200, 20180223, moon, 변경사항 없음
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Warning_Form.h"
#include "MultiLanguage.h"
#include "common.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TntExtCtrls"
#pragma link "TntStdCtrls"
#pragma resource "*.dfm"
TWarningForm *WarningForm;
//---------------------------------------------------------------------------
__fastcall TWarningForm::TWarningForm(TComponent* Owner)
	: TTntForm(Owner)
{
  if(DefaultLCID == 1028 || DefaultLCID == 2052 || DefaultLCID == 3076 || DefaultLCID == 4100)
    ComponentSetFontName(this, "SimSun");
  else if(DefaultLCID == 1041)
    ComponentSetFontName(this, "Meiryo UI");
  else if(DefaultLCID == 3082)
  {
    ComponentSetFontName(this, "Segoe UI");
    ComponentSetMultiLine(this);
  }

	CheckTimerInterval = 19;
	memset(&OldMachineWarningState, 0, sizeof(TMachineWarningState));
	memset(&AlarmMachineWarningState, 0, sizeof(TMachineWarningState));

	FormAnimateState = EFAS_HIDE;
}
//---------------------------------------------------------------------------
void __fastcall TWarningForm::CheckTimerTimer(TObject *Sender)
{
	if (CheckTimerInterval == 0)
	{
		//if (FormAnimateState == EFAS_HIDE)
		{
			CheckWarningEvent();
		}

		CheckTimerInterval = 19;
	}
	else
	{
		CheckTimerInterval--;
	}

	if (FormAnimateState == EFAS_ON_SHOW)
	{
		if (this->Top + this->Height <= Screen->Height)
		{
			FormAnimateState = EFAS_SHOW;
		}
		else
		{
			this->Top -= 10;
		}
	}
	else if (FormAnimateState == EFAS_ON_HIDE)
	{
		this->Top += 10;
		if (this->Top >= Screen->Height)
		{
			FormAnimateState = EFAS_HIDE;
			Hide();
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TWarningForm::CheckWarningEvent(void)
{
	bool bWarningEventOccured = false;
	WideString warningMessage;

	WarningMemo->Lines->Clear();

	try
	{
		if (Machine.WarningStatus.VBFeederEmpty != 0)
		{
			if (OldMachineWarningState.VBFeederEmpty == 0)
			{
				OldMachineWarningState.VBFeederEmpty = 1;
				AlarmMachineWarningState.VBFeederEmpty = 1;

				bWarningEventOccured = true;
				//throw Exception("");
			}
			//warningMessage = "Hopper 레벨이 낮습니다. 정제를 투입해 주십시오.";
			//WarningMemo->Lines->Add(warningMessage);
		}
		else
		{
			OldMachineWarningState.VBFeederEmpty = 0;
		}

		if (AlarmMachineWarningState.VBFeederEmpty != 0)
		{
			warningMessage = WARNINGFORM_MSG_01;
			WarningMemo->Lines->Add(warningMessage);
		}

		if (Machine.WarningStatus.RegulatorWarning != 0)
		{
			if (OldMachineWarningState.RegulatorWarning == 0)
			{
				OldMachineWarningState.RegulatorWarning = 1;
				AlarmMachineWarningState.RegulatorWarning = 1;
				bWarningEventOccured = true;
				//throw Exception("");
			}
			//warningMessage = "공압 수준이 낮습니다.";
			//WarningMemo->Lines->Add(warningMessage);
		}
		else
		{
			OldMachineWarningState.RegulatorWarning = 0;
		}

		if (AlarmMachineWarningState.RegulatorWarning != 0)
		{
			warningMessage = WARNINGFORM_MSG_02;
			WarningMemo->Lines->Add(warningMessage);
		}

		if (Machine.WarningStatus.FrontDoorOpen != 0)
		{
			if (OldMachineWarningState.FrontDoorOpen == 0)
			{
				OldMachineWarningState.FrontDoorOpen = 1;
				AlarmMachineWarningState.FrontDoorOpen = 1;
				bWarningEventOccured = true;
				//throw Exception("");
			}
			//warningMessage = "정면 도어가 열렸습니다.";
			//WarningMemo->Lines->Add(warningMessage);
		}
		else
		{
			OldMachineWarningState.FrontDoorOpen = 0;
		}

		if (AlarmMachineWarningState.FrontDoorOpen != 0)
		{
			//warningMessage = WARNINGFORM_MSG_06;
                        warningMessage = WARNINGFORM_MSG_08;
			WarningMemo->Lines->Add(warningMessage);
		}

		if (Machine.WarningStatus.LeftDoorOpen != 0)
		{
			if (OldMachineWarningState.LeftDoorOpen == 0)
			{
				OldMachineWarningState.LeftDoorOpen = 1;
				AlarmMachineWarningState.LeftDoorOpen = 1;
				bWarningEventOccured = true;
				//throw Exception("");
			}
			//warningMessage = "좌측 도어가 열렸습니다.";
			//WarningMemo->Lines->Add(warningMessage);
		}
		else
		{
			OldMachineWarningState.LeftDoorOpen = 0;
		}

		if (AlarmMachineWarningState.LeftDoorOpen != 0)
		{
			//warningMessage = WARNINGFORM_MSG_07;
                        warningMessage = WARNINGFORM_MSG_08;
			WarningMemo->Lines->Add(warningMessage);
		}

		if (Machine.WarningStatus.RightDoorOpen != 0)
		{
			if (OldMachineWarningState.RightDoorOpen == 0)
			{
				OldMachineWarningState.RightDoorOpen = 1;
				AlarmMachineWarningState.RightDoorOpen = 1;
				bWarningEventOccured = true;
				//throw Exception("");
			}
			//warningMessage = "우측 도어가 열렸습니다.";
			//WarningMemo->Lines->Add(warningMessage);
		}
		else
		{
			OldMachineWarningState.RightDoorOpen = 0;
		}

		if (AlarmMachineWarningState.RightDoorOpen != 0)
		{
			//warningMessage = WARNINGFORM_MSG_05;
                        warningMessage = WARNINGFORM_MSG_08;
			WarningMemo->Lines->Add(warningMessage);
		}
	}
	catch (...)
	{
	/*
		if (bWarningEventOccured)
		{
			WarningMemo->Lines->Clear();
			WarningMemo->Lines->Add(warningMessage);
			this->Top = Screen->Height;
			this->Left = Screen->Width - this->Width;
			this->Show();
			FormAnimateState = EFAS_ON_SHOW;
		}
	*/
	}

	if (bWarningEventOccured)
	{
		this->Top = Screen->Height;
		this->Left = Screen->Width - this->Width;
		if (FormAnimateState == EFAS_ON_HIDE || FormAnimateState == EFAS_HIDE)
		{
			this->Show();
			FormAnimateState = EFAS_ON_SHOW;
		}
		else if (FormAnimateState == EFAS_SHOW)
		{
			this->Top = Screen->Height - this->Height;
			this->Show();
		}
		else
		{
			this->Show();
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TWarningForm::WarningMemoClick(TObject *Sender)
{
	FormAnimateState = EFAS_ON_HIDE;
}
//---------------------------------------------------------------------------
void __fastcall TWarningForm::FormDeactivate(TObject *Sender)
{
	FormAnimateState = EFAS_HIDE;
	this->Hide();
	memset(&AlarmMachineWarningState, 0, sizeof(OldMachineWarningState));
}
//---------------------------------------------------------------------------

