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

© Copyright 2015 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
/*

A note about Super TIU Mode. According to the book, the TIU remembers if
it is in Super Mode. So it is not necessary to send a command to enter Super Mode
with each connect. The Super TIU Mode window, will send the enter/leave
Super Mode with each flip of the switch. No other send is needed.

TODO : : the app sends a specially formatted 'B' command to the WTIU
when Super mode is set for a WTIU. For example, when Super Mode
is set for TIU #3, the command "B12" is sent to TIU #3. When Super mode, is turned
off for TIU #3, the command "B2" is sent to TIU #3. The 'B' command is typically used
to change a TIU number. Here the TIU number is not changed but in the case
of Super Mode going on, the '1' is sent. I don't know why as the Radio Remote
does not do this.
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Super_TIU_Mode.h"
#include "Message_Popup.h"
#include "Train_Control.h"
#include "Setup_Control.h"
#include "PBFileThread.h"
#include "Comm_Thread.h"
#include "Clipbrd.hpp"
#include "Debug_Controls.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "_GClass"
#pragma link "AbSwitch"
#pragma link "AbLED"
#pragma resource "*.dfm"

extern bool SuperTIUMode, TIUSuperMode[TIUMAX];         // Flags to indicate if Super TIU Mode is active
extern TCommThread * ptrTIUCommThread;				    // pointer to TIU communications thread
extern bool TIUSelection[TIUMAX];                       // Flags to indicate that a TIU is present
TAbRockerSwitch * TIUSuperSwitchptr[TIUMAX];     // pointers to each Super TIU Mode switch

TSuperTIU *SuperTIU;
//---------------------------------------------------------------------------
__fastcall TSuperTIU::TSuperTIU(TComponent* Owner)
	: TForm(Owner)
{
TIU1->Checked = TIUSuperMode[0];
TIU2->Checked = TIUSuperMode[1];
TIU3->Checked = TIUSuperMode[2];
TIU4->Checked = TIUSuperMode[3];
TIU5->Checked = TIUSuperMode[4];

TIUSuperSwitchptr[0] = TIU1;        // Setup pointers
TIUSuperSwitchptr[1] = TIU2;
TIUSuperSwitchptr[2] = TIU3;
TIUSuperSwitchptr[3] = TIU4;
TIUSuperSwitchptr[4] = TIU5;

SuperTIUModeLED->Checked = SuperTIUMode;

LocalTransparencyOverride = false;
}
//---------------------------------------------------------------------------
void __fastcall TSuperTIU::HideButtonClick(TObject *Sender)
{
if (SuperTIUMode) {
    if (TIU1->Checked || TIU2->Checked || TIU3->Checked || TIU4->Checked || TIU5->Checked) {
        Hide();	      // Same as setting Visible to false
        } else {
        MainForm->DebugMessagePopup("Warning", "At least one TIU must be selected", "");
        return;
        }
    } else {
    Hide();	      // Same as setting Visible to false
    }
}
//---------------------------------------------------------------------------
void __fastcall TSuperTIU::TIU1Click(TObject *Sender)
{
TIUSuperMode[0] = TIU1->Checked;
SuperTIUMode = (TIU1->Checked || TIU2->Checked || TIU3->Checked || TIU4->Checked || TIU5->Checked);
SuperTIUModeLED->Checked = SuperTIUMode;
// In WiFi Mode, send a 'B' command to the WTIU
if (Setup->WTIU_WiFi_Mode->Checked && TIUSelection[0]) {
    if (TIU1->Checked) {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B10", 1, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        } else {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B0", 1, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TSuperTIU::TIU2Click(TObject *Sender)
{
TIUSuperMode[1] = TIU2->Checked;
SuperTIUMode = (TIU1->Checked || TIU2->Checked || TIU3->Checked || TIU4->Checked || TIU5->Checked);
SuperTIUModeLED->Checked = SuperTIUMode;
// In WiFi Mode, send a 'B' command to the WTIU
if (Setup->WTIU_WiFi_Mode->Checked && TIUSelection[1]) {
    if (TIU2->Checked) {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B11", 2, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        } else {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B1", 2, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TSuperTIU::TIU3Click(TObject *Sender)
{
TIUSuperMode[2] = TIU3->Checked;
SuperTIUMode = (TIU1->Checked || TIU2->Checked || TIU3->Checked || TIU4->Checked || TIU5->Checked);
SuperTIUModeLED->Checked = SuperTIUMode;
// In WiFi Mode, send a 'B' command to the WTIU
if (Setup->WTIU_WiFi_Mode->Checked && TIUSelection[2]) {
    if (TIU3->Checked) {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B12", 3, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        } else {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B2", 3, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TSuperTIU::TIU4Click(TObject *Sender)
{
TIUSuperMode[3] = TIU4->Checked;
SuperTIUMode = (TIU1->Checked || TIU2->Checked || TIU3->Checked || TIU4->Checked || TIU5->Checked);
SuperTIUModeLED->Checked = SuperTIUMode;
// In WiFi Mode, send a 'B' command to the WTIU
if (Setup->WTIU_WiFi_Mode->Checked && TIUSelection[3]) {
    if (TIU4->Checked) {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B13", 4, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        } else {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B3", 4, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TSuperTIU::TIU5Click(TObject *Sender)
{
TIUSuperMode[4] = TIU5->Checked;
SuperTIUMode = (TIU1->Checked || TIU2->Checked || TIU3->Checked || TIU4->Checked || TIU5->Checked);
SuperTIUModeLED->Checked = SuperTIUMode;
// In WiFi Mode, send a 'B' command to the WTIU
if (Setup->WTIU_WiFi_Mode->Checked && TIUSelection[4]) {
    if (TIU5->Checked) {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B14", 5, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        } else {
	    (void)ptrTIUCommThread->Send_String_FIFO(FACTORYRESET, "B4", 5, MainForm->RemoteNo, 11, SHORT_WAIT * 20, RECORD_OK);
        }
    }
}
//---------------------------------------------------------------------------

// Workaround for SetLayeredWindowAttributes() to make transparent windows
extern lpfnSetLayeredWindowAttributes m_pSetLayeredWindowAttributes;
//---------------------------------------------------------------------------

void __fastcall TSuperTIU::FormActivate(TObject *Sender)
{
#if __BORLANDC__ >= 0x720      // CB10.1 only
AlphaBlend = false; // Remove Transparency
#else
// Remove WS_EX_LAYERED from this window styles
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) & ~WS_EX_LAYERED);
// Ask the window and its children to repaint
//Repaint();
#endif
TIU1->Enabled = TIUSelection[0];
if (!TIUSelection[0]) {
    TIU1->Checked = false;
    }
TIU2->Enabled = TIUSelection[1];
if (!TIUSelection[1]) {
    TIU2->Checked = false;
    }
TIU3->Enabled = TIUSelection[2];
if (!TIUSelection[2]) {
    TIU3->Checked = false;
    }
TIU4->Enabled = TIUSelection[3];
if (!TIUSelection[3]) {
    TIU4->Checked = false;
    }
TIU5->Enabled = TIUSelection[4];
if (!TIUSelection[4]) {
    TIU5->Checked = false;
    }
SuperTIUMode = (TIU1->Checked || TIU2->Checked || TIU3->Checked || TIU4->Checked || TIU5->Checked);
SuperTIUModeLED->Checked = SuperTIUMode;
}
//---------------------------------------------------------------------------

void __fastcall TSuperTIU::FormDeactivate(TObject *Sender)
{
if (SuperTIUMode && (!TIU1->Checked && !TIU2->Checked && !TIU3->Checked && !TIU4->Checked && !TIU5->Checked)) {
    MainForm->DebugMessagePopup("Warning", "In SuperTIUMode, at least one TIU must be selected", "");
    }
if (Setup->TransparentSpin->Value == Setup->TransparentSpin->MaxValue) return;	// Off
if (LocalTransparencyOverride) return;
#if __BORLANDC__ >= 0x720      // CB10.1 only
AlphaBlendValue = (255 * Setup->TransparentSpin->Value) / 100;
AlphaBlend = true;  // Add Transparency
#else
// Set WS_EX_LAYERED on this window
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);
//SetLayeredWindowAttributes
m_pSetLayeredWindowAttributes(Handle, NULL, (255 * Setup->TransparentSpin->Value) / 100, LWA_ALPHA);
#endif
}
//---------------------------------------------------------------------------

void __fastcall TSuperTIU::SuperTIU_LabelClick(TObject *Sender)
{
LocalTransparencyOverride = !LocalTransparencyOverride;	// Toggle transparency locally
}
//---------------------------------------------------------------------------

void __fastcall TSuperTIU::CopyrightClick(TObject *Sender)
{
// Copy the Form to the clipboard.
// In BCB4.0, there is no SetSize method associated with TBitmap
// so we have to use Width and Height separately.
//
// see:
// https://stackoverflow.com/questions/3190777/how-do-i-copy-a-form-as-an-image-to-the-clipboard
//
Graphics::TBitmap * bm;
bm = new Graphics::TBitmap;
try
	{
    #if __BORLANDC__ >= 0x720
	// Defined for CB10.1 C++ programs that use the VCL.
	bm->SetSize(ClientWidth, ClientHeight);
    #else
    bm->Width = ClientWidth;
    bm->Height = ClientHeight;
    #endif
	BitBlt(bm->Canvas->Handle, 0, 0, ClientWidth, ClientHeight, Canvas->Handle, 0, 0, SRCCOPY);
    Clipboard()->Assign(bm);
    }
__finally
	{
	delete bm;
    }    
}
//---------------------------------------------------------------------------

void __fastcall TSuperTIU::SuperTIUModeLEDClick(TObject *Sender)
{
//turn off Super TIU Mode for each TIU

for (int MyTIUNo = 1 ; MyTIUNo <= TIUMAX ; MyTIUNo++) {
    if (TIUSuperSwitchptr[MyTIUNo - 1]->Checked) TIUSuperSwitchptr[MyTIUNo - 1]->Checked = false;
    }
SuperTIUMode = false;
SuperTIUModeLED->Checked = SuperTIUMode;
}
//---------------------------------------------------------------------------

