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

© Copyright 2019 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 DL_ThreadH
#define DL_ThreadH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TDispatch_List_Thread : public TThread
{
private:
	int MyRemoteNo, DL_RunTime;	// RunTime in milliseconds
    //
	// Task Dispatching Variables
	//
	struct DispatchItem * last;    // The last item of the list.
	struct DispatchItem * first;   // The first item of the list.
	volatile unsigned int DispatchListCount;   	// Zero-based count of items within the Dispatch list.
	bool DispatchListAddInProgress;		// adding to dispatch list in progress

	void __fastcall Dispatch_Command(void);
	__fastcall TDispatch_List_Thread(bool CreateSuspended);
	TForm * MyCallingthis;
    HWND MyHndl;
    bool __fastcall DumpDispatchList(void);
	bool __fastcall AddToDispatchList(unsigned int TimeValue, byte ConsistNo, byte TIUNo, char Command[], int *Flag = (int *) NULL, int FlagValue = 0,
    	int Expected = 11, int WaitTime = SHORT_WAIT, byte * ptrLashUpList = (byte *) NULL);

    bool __fastcall AddToDispatchList(TOp * CallingOp, HWND Parent, DCommand DelayedCommand,
		unsigned char iConsistNo, unsigned char iTIUNo, unsigned int TimeValue = 0, int iParameter = 0);

	struct DispatchItem * __fastcall RemoveFromDispatchList(unsigned int index);
	struct DispatchItem * __fastcall RemoveFromDispatchList(DispatchItem * pLItem);

   	TThreadList * DL_FIFO;	    //  FIFO Dispatch List - Thread Safe    

	void __fastcall SimpleCommand(void);
	void __fastcall CleanUp(void);

    TOp *  MyCallingOpScreen;
    HWND MyParent;
    DCommand MyDelayedCommand;
    byte MyConsistNo;
    byte MyTIUNo;
    unsigned int MyTimeValue;
    int MyParameter;
	DWORD ThreadID;

protected:
	void __fastcall Execute();
    void __fastcall RunOnTerminate(TObject * Sender);

public:
    __fastcall TDispatch_List_Thread(bool CreateSuspended, TForm * Callingthis, HWND Hndl, int Remote);
    __fastcall ~TDispatch_List_Thread();

    bool __fastcall Send_String_DL_FIFO(unsigned int TimeValue, unsigned char Consist, byte * Command_String,
		unsigned char TIU, unsigned char Remote, int Expected, int WaitTime, bool Record_Flag, byte * ptrLashUpEngineList = (byte *) NULL,
        int * ipFlag = NULL, int FlagValue = 0);
	bool __fastcall Send_Delayed_DL_FIFO(TOp * CallingOp, HWND Parent, DCommand Command,
		unsigned char ConsistNo, unsigned char TIU, unsigned char Remote, unsigned int TimeValue = 0, int iParameter = 0);
	bool __fastcall ClearDispatchList(void);
	int __fastcall Count_DL_FIFO(int option = 0);
	unsigned int __fastcall Count_Dispatch_List(int option = 0);

    bool Finished;
};
//---------------------------------------------------------------------------
#endif
