//---------------------------------------------------------------------------
/*
Remote Train Control Program for Windows

© Copyright 2020 by Mark DiVecchio

    This file is part of Remote Train Control.

    Remote Train Control is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Remote Train Control is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

*/
// Home Page : http://www.silogic.com/trains/RTC_Running.html
//---------------------------------------------------------------------------
#ifndef LayoutH
#define LayoutH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "HideButton.h"
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include "_GClass.hpp"
#include "AbLED.hpp"
#include <Dialogs.hpp>
#include <Grids.hpp>
#include <Menus.hpp>

#include "LED3Panel.h"
#include "LED2Panel.h"
#include "LED1Panel.h"
#include "LEDMPanel.h"  // Mike Hewett's 3 light signal
#include "RTC_Help.h"

// maximum number of Flags
#define FLAGLIMIT 100
// special meaning flags
#define UNOCCUPIED 0
//#define OCCUPIED 1	// Occupied is any value > 0
#define MANUALFLAG -1	// this is the flag value when set manually on the Layout Window
                        // This setting can only be removed manually in the layout window
#define SOFTOCCUPY -2   // Anyone can override this setting

// Flaggrid File Rows
#define FLAG_LABEL_ROW 0
#define FLAG_TOPROW 1

// columns in FlagGrid
#define FLAGNUM 0
#define FLAGX 1
#define FLAGY 2
#define FLAGORD 3
#define FLAGNAME 4

// Signal Aspects 1-x
enum SignalAspect {AsNone = 1, AsDark = 2, AsStopSignal = 3, AsClear = 4, AsStopandProceed = 5, AsApproach = 6, AsMediumClear = 7, AsMediumApproach = 8,
	AsSlowClear = 9, AsSlowApproach = 10, AsApproachMedium = 11, AsApproachSlow = 12, AsRestricting = 13, AsApproachLimited = 14,
    AsLimitedClear = 15, AsLimitedApproach = 16, AsMediumApproachSlow = 17, AsTest = 18, AsLast = 19};

// Signal Mode
#define HdMAX 4     // highest signal mode
#define HdMLED 4    // Mike Hewett's 3 light signal
#define Hd3LED 3
#define Hd2LED 2
#define Hd1LED 1
#define HdNONE 0

// maximum number of Signals
#define SIGNALLIMIT 200

// Flaggrid File Rows
#define SIGNAL_LABEL_ROW 0
#define SIGNAL_TOPROW 1

// columns in SignalGrid
#define SIGNALNUM 0
#define SIGNALX 1
#define SIGNALY 2
#define SIGNALASPECT 3
#define SIGNALMODE 4
#define SIGNALNUMBERPLATE 5
#define SIGNALNAME 6
#define SIGNALDIR 7
#define SIGNALTYPE 8
#define SIGNALSP1 9

// East-Westbound and North-Southbound signal identifiers
#define WB 1
#define EB 2
#define NB 3
#define SB 4

// Signal Type
#define UNDEFINED_SIGNAL_TYPE 0
#define PERMISSIVE_IN_OPEN_COUNTRY 1
#define ABSOLUTE_HEAD_BLOCK 2
#define PERMISSIVE_HEAD_BLOCK 3
#define APPROACH_TO_HEAD_BLOCK 4
#define LAST_SIGNAL_TYPE 4

//-----------------------Traffic Stick Directions----------------------------
#define NDT  0		// No direction-of-traffic
#define WEST 1      // Westbound
#define EAST 2      // Eastbound
//---------------------------------------------------------------------------
class TLayoutForm : public TForm
{
__published:	// IDE-managed Components
	TPanel *HidePanel;
	THideButton *HideButton1;
	TImage *Copyright;
	TButton *EditGridButton;
	TLabel *FlagN;
	TLabel *Coords;
    TButton *LayoutCopyButton;
	TButton *WindowCopyButton;
	TOpenDialog *OpenDialog;
	TButton *BrowseforBitmapFileButton;
	TPopupMenu *BitmapPopupMenu;
	TMenuItem *TransparentTextMenuItem;
    TMenuItem *ToggleFlagNameColorMenuItem;
	TPopupMenu *FlagPopupMenu;
	TMenuItem *RedrawLayout;
    TMenuItem *DeleteFlag;
	TMenuItem *RedrawLayout2;
	TMenuItem *CopyWindowtoClipboard;
	TMenuItem *ShowCursorCoords;
    TMenuItem *CreateFlag2;
	TMenuItem *Info;
	TMenuItem *CopytoClipboard;
	TScrollBox *ImageScrollBox;
	TImage *LayoutImage;
	TStringGrid *FlagGrid;
	TStringGrid *SignalGrid;
	TMemo *Information;
    TMenuItem *CopyLayouttoClipboard;
	TMenuItem *MoreInformation1;
    TButton *ViewLayoutDir;
	void __fastcall HideButton1Click(TObject *Sender);
	void __fastcall FormActivate(TObject *Sender);
	void __fastcall FormDeactivate(TObject *Sender);
	void __fastcall FormClick(TObject *Sender);
	void __fastcall LayoutImageClick(TObject *Sender);
	void __fastcall BrowseforBitmapFileButtonClick(TObject *Sender);
	void __fastcall EditGridButtonClick(TObject *Sender);
	void __fastcall TransparentTextMenuItemClick(TObject *Sender);
	void __fastcall ToggleFlagNameColorMenuItemClick(TObject *Sender);
	void __fastcall RedrawLayoutClick(TObject *Sender);
	void __fastcall DeleteFlagClick(TObject *Sender);
	void __fastcall FlagGridKeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
	void __fastcall FlagGridMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y);
	void __fastcall FormCanResize(TObject *Sender, int &NewWidth,
          int &NewHeight, bool &Resize);
	void __fastcall CopyrightClick(TObject *Sender);
	void __fastcall CopyWindowtoClipboardClick(TObject *Sender);
	void __fastcall ShowCursorCoordsClick(TObject *Sender);
	void __fastcall LayoutMouseMove(TObject *Sender, TShiftState Shift,
          int X, int Y);
	void __fastcall CreateFlag2Click(TObject *Sender);
	void __fastcall InfoClick(TObject *Sender);
	void __fastcall CopytoClipboardClick(TObject *Sender);
	void __fastcall SignalGridKeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
	void __fastcall SignalGridMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y);
	void __fastcall CopyLayouttoClipboardClick(TObject *Sender);
	void __fastcall MoreInformation1Click(TObject *Sender);
    void __fastcall ViewLayoutDirClick(TObject *Sender);
    void __fastcall StartMultiTX(void);
    void __fastcall StartTX(void);
    void __fastcall SendSignal(int SignalNo, int AspectNo);
    void __fastcall RadioSendDataSent(TObject *Sender, WORD ErrCode);
    void __fastcall RadioSendSessionClosed(TObject *Sender, WORD ErrCode);
    void __fastcall RadioSendSessionConnected(TObject *Sender, WORD ErrCode);

    void __fastcall StartRX();
    void __fastcall RadioRecvDataAvailable(TObject *Sender, WORD ErrCode);
    void __fastcall RadioRecvSessionClosed(TObject *Sender, WORD ErrCode);
    void __fastcall RadioRecvSessionConnected(TObject *Sender, WORD ErrCode);

private:	// User declarations

	// Mouse Down handlers
	void __fastcall FlagMouseDownLocal(TObject *Sender, TMouseButton Button,
    	  TShiftState Shift, int X, int Y);
	void __fastcall SignalMouseDownLocal(TObject *Sender, TMouseButton Button,
    	  TShiftState Shift, int X, int Y);
	void __fastcall SignalMouseDownLocal1(TObject *Sender, TMouseButton Button,
    	  TShiftState Shift, int X, int Y);
	void __fastcall SignalMouseDownLocal2(TObject *Sender, TMouseButton Button,
    	  TShiftState Shift, int X, int Y);
	void __fastcall SignalMouseDownLocal3(TObject *Sender, TMouseButton Button,
    	  TShiftState Shift, int X, int Y);

    void __fastcall LoadFlagData(void);
    TAbLED * AllLED[FLAGLIMIT];
    TLabel * AllLL[FLAGLIMIT];
    bool LocalTransparencyOverride;
    String RegFileRootKey;
    bool TransparentText, ToggleFlagNameColor;
    String FlagDataFile;
    bool CreateAFlag;
    void __fastcall CreateFlag(int FlagNo, int X, int Y, int ord, String FlagName);
    bool Corrupt;
    String INIFileName;
    String SignalDataFile;
    void __fastcall LoadSignalData(void);
	void __fastcall ConstructSignal(int SignalNo, int iDir, int Col, int Row, String SignalName, int iMode, int Aspect,
	    int NumPlate, int iSP1, int iSP2);
    int HorzScrollPosition, VertScrollPosition;

    // key pressed =  0xFF8X
	// key not pressed = 0x000X
	// "X" can be 0 or 1 - indicated key was toggled
	SHORT ShiftKeyState;
	SHORT AltKeyState;
	SHORT CtrlKeyState;
    bool LayoutDataSent;				// Indicates that the Layout packet was sent
    bool RecvDataReady;             // Indicates that a Layout packet was received
    TWSocket * RadioSend, * RadioRecv;    // udp port

    #if __BORLANDC__ >= 0x720
	// Defined for CB10.1 C++ programs that use the VCL.
  	Overbyteicswinsock::TInAddr FServerAddr;
    Overbyteicswinsock::TSockAddrIn Detector;
    #else
    TInAddr FServerAddr;
    TSockAddrIn Detector;
    #endif

    bool RadioWiFiRXConnected, RadioWiFiTXConnected;
    byte cBuffer[1024];
    
public:		// User declarations
	__fastcall TLayoutForm(TComponent* Owner);
	__fastcall ~TLayoutForm(void);
    void __fastcall ShowIt();
    void __fastcall ShowImage(String FileName);

    String __fastcall GetFlagName(int FlagNo);
    int __fastcall SetFlagName(int Flagno, String FlagName);
    int __fastcall SetFlag(int FlagNo, int iState);
    int __fastcall GetFlag(int FlagNo);

    HWND ParentHandle; 		// accessed by Train_Control.cpp

    String __fastcall GetSignalName(int SignalNo, int &iNumberPlate, int &iType);
    int __fastcall CreateSignal(int SignalNo, int iDir, int iType, int Col, int Row, String SignalName, int iMode, int Aspect, int NumberPlate);
    int __fastcall SetAspect(int SignalNo, int Aspect);
    int __fastcall GetAspect(int SignalNo, int &iMode, int &iType);
    void __fastcall DeleteAllSignals(void);
    bool __fastcall Reset(TPanel * pSignal);

    TRTCHelp *SignalHelp;
};
//---------------------------------------------------------------------------
extern PACKAGE TLayoutForm *LayoutForm;
//---------------------------------------------------------------------------
#endif
