#include "enjTelnetPanel.h"
#include "bbsrc/scd_common_sock.h"
//#include "bbsrc/scd_wxssh.h"

extern "C" {
#define FRONTEND_CLASS SCD_wxCOMSOCK
int from_backend(void *frontend, int is_stderr, const char *data, int len)
{
	//return ((FRONTEND_CLASS *) frontend)->from_backend(is_stderr,data,len);
	((FRONTEND_CLASS *) frontend)->SetInputBuffer((char *)data,len);
	return 0;
}

void wxPrintMessage(void *frontend,char *buf,int len)
{
	if ( frontend ){
		FRONTEND_CLASS * fe = (FRONTEND_CLASS *) frontend;
		//int scroll_count;
		int unread = 0;
		//if (fe->unread_len ) {
		fe->SetInputBuffer(buf,len);
		wxLogMessage(wxString(buf,len));
		//memmove(fe->unread+fe->unread_len,buf,len);
		//fe->unread_len +=len;
		//if ( fe->unread_len < 1000 ) return;

		/*
		unread = fe->telnet->parse(fe->unread,fe->unread_len);
		if ( unread ) {
			memmove(fe->unread,fe->unread+fe->unread_len - unread, unread);
			wxLogMessage("unread %d",unread);
		}
		fe->unread_len = unread;
		*/
	}else {
	   	if ( len < 0 ) {
			wxLogMessage(buf);
		}else{
			wxLogMessage(wxString(buf,len));
		}
	}
}

/*
 * Print a message box and close the connection.
 */
void connection_fatal(void *frontend, char *fmt, ...)
{

    va_list ap;
    char *stuff, morestuff[100];

    va_start(ap, fmt);
    stuff = dupvprintf(fmt, ap);
    va_end(ap);
    sprintf(morestuff, "%.70s Fatal Error", appname);
    MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
    sfree(stuff);

	//((FRONTEND_CLASS *) frontend)->close_session();
}

			//	strncpy(fe->unread,buf+len-unread,unread);
			//	fe->unread_len = unread;
		//}else{
		//	unread = fe->telnet->parse(buf,len);
		//}
	//	}else{
	//		fe->unread_len = 0;
Context get_ctx(void *frontend)
{
	return NULL;
	if ( !frontend ) return NULL;
	FRONTEND_CLASS * fe = (FRONTEND_CLASS *) frontend;
	/*
	HWND hw = fe->GetHandle();
    HDC hdc;
    if (hw) {
	hdc = GetDC(hw);
	if (hdc && pal)
	    SelectPalette(hdc, pal, FALSE);
	return hdc;
    } else
	return NULL;
	*/

		//int scroll_count;
	/*
	wxClientDC *dc = new wxClientDC(fe);
   	dc->BeginDrawing();
	*/
	//return ;
	//return fe->get_ctx();
	
	/*
    if (hwnd) {
	hdc = GetDC(hwnd);
	if (hdc && pal)
	    SelectPalette(hdc, pal, FALSE);
	return hdc;
    } else
	return NULL;
	*/
}

void free_ctx(Context ctx)
{
	//if (!ctx) return;
	//wxClientDC *dc=(wxClientDC *)ctx;
	//dc->EndDrawing();
	//delete ctx;
		/*
    SelectPalette(ctx, GetStockObject(DEFAULT_PALETTE), FALSE);
    ReleaseDC(hwnd, ctx);
	*/
}
/*
 * Draw a line of text in the window, at given character
 * coordinates, in given attributes.
 *
 * We are allowed to fiddle with the contents of `text'.
 */
void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
		      unsigned long attr, int lattr)
{
	return;
	if (!ctx) return;
	FRONTEND_CLASS *fe = (FRONTEND_CLASS *)ctx;
//	fe->do_text(x, y, text, len, attr, lattr);
}

}//extern "C"

