// TeacherLearnDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TeacherLearnDlg.h"
#include "ElevatorsApp.h"
#include "LearnDlg.h"
#include "TNeuro.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// TeacherLearnDlg dialog


TeacherLearnDlg::TeacherLearnDlg(CWnd* pParent /*=NULL*/)
	: CDialog(TeacherLearnDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(TeacherLearnDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

   endDialog  = false;
   timer = 0;
}


void TeacherLearnDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(TeacherLearnDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(TeacherLearnDlg, CDialog)
	//{{AFX_MSG_MAP(TeacherLearnDlg)
	ON_BN_CLICKED(IDC_BUTTON_GO, OnButtonGo)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// TeacherLearnDlg message handlers

UINT TeacherLearnDlg::MyControllingFunction( LPVOID pParam )
{
   TeacherLearnDlg *dlg = (TeacherLearnDlg*)pParam;

   controlSystem->messageHandler( CSM_LEARN_TEACHER, pParam );
   theApp.DoMessageBox("Обучение завершено", MB_OK, 0);

   dlg->endDialog = true;

   return 0;
}

BOOL TeacherLearnDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
   CEdit *statc = (CEdit*)GetDlgItem(IDC_TEACHER_LEARN_EDIT_LEARN);
   statc->SetWindowText( "" );

   statc = (CEdit*)GetDlgItem(IDC_TEACHER_LEARN_EDIT_CYCLE);
   statc->SetWindowText( "" );

   // graph
   CRect rect;
   GetDlgItem(IDC_TEACHER_LEARN_GRAPH)->GetWindowRect(rect);
   ScreenToClient(rect);

   graph.Create( WS_VISIBLE|WS_CHILD, rect, this );

   graph.SetRange( 0, 0.4 );
   graph.SetYUnits( "" );
   graph.SetXUnits( "" );
   graph.SetBackgroundColor(GRAPH_BACK);
   graph.SetGridColor(GRAPH_GRID);
   graph.SetPlotColor(GRAPH_PLOT);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void TeacherLearnDlg::OnButtonGo() 
{
	// TODO: Add your control notification handler code here
	
   if( !timer )
   {
      timer = SetTimer( 2, 2000, 0 );

      thread = AfxBeginThread( MyControllingFunction,
                               this );

      ((CButton*)GetDlgItem(IDC_TEACHER_LEARN_BUTTON_GO))->SetWindowText("&Stop");
   }
   else
   {
      controlSystem->messageHandler( CSM_SET_TEACHER_STOP_LEARNING, NULL );
      ((CButton*)GetDlgItem(IDC_TEACHER_LEARN_BUTTON_GO))->SetWindowText("&Go");
   }
}

void TeacherLearnDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
   CEdit *statc = (CEdit*)GetDlgItem(IDC_TEACHER_LEARN_EDIT_LEARN);
   
   CString str;
   str.Format( "%10.20f", lastDiff );
   statc->SetWindowText( str );

   graph.AppendPoint( dlgDiff );

   dlgDiff = 0.0;

   CEdit* statc2 = (CEdit*)GetDlgItem(IDC_TEACHER_LEARN_EDIT_CYCLE);

   int cycle;
   controlSystem->messageHandler( CSM_GET_TEACHER_CYCLE, 
                                  (void*)&cycle );

   str.Format( "%d", cycle );
   statc2->SetWindowText( str );

   if( endDialog )
   {
      KillTimer( timer );
      timer = 0;

      statc->SetWindowText("");
      statc2->SetWindowText("");

      graph.Reset();
      ((CButton*)GetDlgItem(IDC_TEACHER_LEARN_BUTTON_GO))->SetWindowText("&Go");
   }
   
   GetDlgItem(IDC_TEACHER_LEARN_GRAPH)->Invalidate();
   GetDlgItem(IDC_TEACHER_LEARN_GRAPH)->UpdateWindow();
	
	CDialog::OnTimer(nIDEvent);
}
