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



#include <vcl.h>
#pragma hdrstop


#include "set_unit.h"
#include "set_code.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma resource "images.res"

#pragma link "ScktComp"

Tset_game *set_game;


__fastcall Tset_game::Tset_game(TComponent* Owner) : TForm(Owner)
{
}


void __fastcall Tset_game::FormPaint(TObject *Sender)
{

    board.draw(); // redraw the entire board
}

void __fastcall Tset_game::NewGame1Click(TObject *Sender)
{
/*    if (!is_server)
        set_game->Status->SimpleText = "Can't start a new game when a client";
    else*/
        board.reset(); // reset all of the game variables for a new game
}

void __fastcall Tset_game::Exit1Click(TObject *Sender)
{
    Application->Terminate(); // cose the program
}

void __fastcall Tset_game::Cheatmode1Click(TObject *Sender)
{
    // toggle the menu diaplay
    if (board.get_cheat())
    {
        board.set_cheat(0);
        Cheatmode1->Caption = "Practice";
    }
    else
    {
        board.set_cheat(1);
        Cheatmode1->Caption = "Normal Play";
    }

    board.draw();
}

void __fastcall Tset_game::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    board.card_click(X, Y);
    // convert mouse cords to grid position and update selection
//    board.toggle_select((X / (board.card_x_size + x_pad)) * 3 +
//                        (Y / (board.card_y_size + y_pad)));
}

void __fastcall Tset_game::Timer1Timer(TObject *Sender)
{
/*    game_time++;

    if (game_time % 10 == 0)
        Status->SimpleText = board.game_time();*/
}
//---------------------------------------------------------------------------













void __fastcall Tset_game::Connect1Click(TObject *Sender)
{
/*    if (InputQuery("Computer to connect to", "Address Name:", Server))
    {
       if (Server.Length() > 0)
       {
           is_server = 0;
           ClientSocket->Active = false;

           ServerSocket->Active = false;
           ClientSocket->Host = Server;
           ClientSocket->Active = true;

//           Memo1->Lines->Add("Connected to: " + Server);
       }
  }*/

}
//---------------------------------------------------------------------------

void __fastcall Tset_game::FormDestroy(TObject *Sender)
{
/*  ServerSocket->Close();
  ClientSocket->Close();
  Close();*/
}
//---------------------------------------------------------------------------

void __fastcall Tset_game::FormCreate(TObject *Sender)
{
/*    connected = 0;

    is_server = 1;
    port = 1024;

    Server = "127.0.0.1";

    screen_name = "Default";

    ClientSocket->Port = port;
    ServerSocket->Port = port;

    ClientSocket->Active = false;
    ServerSocket->Active = true;
    Memo1->Lines->Add("Waiting for connection");*/
}
//---------------------------------------------------------------------------


void __fastcall Tset_game::ServerSocketAccept(TObject *Sender,
      TCustomWinSocket *Socket)
{
/*    is_server = 1;
    connected = 1;
    Memo1->Lines->Add("client has connected: " + Socket->RemoteAddress);

    board.reset();*/
}
//---------------------------------------------------------------------------


void __fastcall Tset_game::ClientSocketConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
/*    connected = 1;

    Memo1->Lines->Add("Connected to server: " + Server);*/
}
//---------------------------------------------------------------------------



void split_message(String message,String &command,String &param)
{
/*    int index = 1;
    int found = 0;

    while ((!found) && (index <= message.Length()))
    {
        if (message[index] != '=')
            command += message[index];
        else
            found = 1;

        index++;
    }

    found = 0;
    index++;

    while ((!found) && (index <= message.Length()))
    {
        if (message[index] != '"')
            param += message[index];
        else
            found = 1;

        index++;
    }/*

}





void do_message(String message)
{
/*    String command, param;

    split_message(message, command, param);

    if (command == "CHAT") set_game->Memo1->Lines->Add("Them: " + param); else
    if (command == "NEWGAME") set_game->board.client_reset();
    if (command == "ADDCARD") set_game->board.client_add_card(set_game->board.name_to_card(param));
    if (command == "REMOVECARD")
            set_game->board.client_remove_card(set_game->board.name_to_card(param));*/
}








/**
  Network code
**/

void  Tset_game::send_message(String command, String data)
{
/*    if (connected)
    {
        if (is_server)
            ServerSocket->Socket->Connections[0]->SendText(command + "=\"" + data + "\"\t");
        else
            ClientSocket->Socket->SendText(command + "=\"" + data + "\"\t");
    }*/
}



/* deal with the rec message stream */
void receive_message(String buffer)
{
/*    char tmp;
    int index = 1;
    int end_message = 0;

    String message_str = "";

    while (index <= buffer.Length())
    {
        while ((index <= buffer.Length()) && (!end_message))
        {
            if (buffer[index] == '\t')
                end_message = 1;
            else
                message_str += buffer[index];

            index++;
        }
        do_message(message_str);

        end_message = 0;
        message_str = "";
    }*/
}
/* end of receive message */


/***
  Client side
***/



void __fastcall Tset_game::ClientSocketRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
/*    AnsiString buffer = Socket->ReceiveText();

    receive_message(buffer);*/
}
//---------------------------------------------------------------------------


void __fastcall Tset_game::ClientSocketDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
/*     connected = 0;

    Memo1->Lines->Add("Disconnected from: " + Server);*/
}
//---------------------------------------------------------------------------

/****
  End of client Side
*****/


/****
   Serer side
****/

void __fastcall Tset_game::ServerSocketClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
/*    AnsiString buffer = Socket->ReceiveText();

    receive_message(buffer);*/
}
//---------------------------------------------------------------------------




void __fastcall Tset_game::ServerSocketClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
/*     connected = 0;

    Memo1->Lines->Add("Disconnected from: " + Server);*/
}
//---------------------------------------------------------------------------


/*****
  End of server Side
****/




void __fastcall Tset_game::Edit1KeyPress(TObject *Sender, char &Key)
{
/*    if (Key == VK_RETURN)
    {
        send_message("CHAT",  Edit1->Text);

        Memo1->Lines->Add("You: " + Edit1->Text);

        Edit1->Text = "";
    }*/
}
//---------------------------------------------------------------------------





void __fastcall Tset_game::Port1Click(TObject *Sender)
{
/*     String port_str = (String) port;


    if (InputQuery("Modify Port Number", "New port number:", port_str))
    {
       if (port_str.Length() > 0)
       {
           port = StrToInt(port_str);

           if (is_server)
           {
               ServerSocket->Active = false;
               ClientSocket->Active = false;
               ServerSocket->Port = port;
               ClientSocket->Port = port;
               ServerSocket->Active = true;
           }
           else
           {
               ServerSocket->Active = false;
               ClientSocket->Active = false;
               ClientSocket->Port = port;
               ServerSocket->Port = port;
               ClientSocket->Active = true;
           }
       }
  }*/
}
//---------------------------------------------------------------------------


void __fastcall Tset_game::Disconnect1Click(TObject *Sender)
{
/*    is_server = 1;
    connected = 0;

    ClientSocket->Active = false;
    ServerSocket->Active = false;

    Memo1->Lines->Add("Waiting for connection");*/
}
//---------------------------------------------------------------------------

void __fastcall Tset_game::ScreenName1Click(TObject *Sender)
{
/*    String new_name = screen_name;

    if (InputQuery("Modify Screen Name", "New screen name:", new_name))
    {
       if (new_name.Length() > 0)
       {
       screen_name = new_name;


       send_message("NAME", screen_name);
//        if (is_server)
//            ServerSocket->Socket->Connections[0]->SendText("_SCREEN_NAME-" + screen_name);
//        else
//            ClientSocket->Socket->SendText("_SCREEN_NAME-" + screen_name);
    }

  }*/

}
//---------------------------------------------------------------------------


void __fastcall Tset_game::ServerSocketClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
//    Memo1->Lines->Add("A client has connected");
//    board.reset();
}
//---------------------------------------------------------------------------





