#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Window.H>
#include "../../../std_lib_facilities.h"
#include "../../../Point.h"
#include "../../../Graph.h"
#include "../../../GUI.h"
#include "../../../Simple_window.h"
#include "../../../Window.h"

using namespace Graph_lib;

struct Coulomb_window :Window{
    Coulomb_window(Point xy, int w, int h, const string& title );

private:
	Circle circle;
	Button start_button;
	static void cb_start(Address,Address);
	void start();
	int handle(int);
	friend void timeout_callback(void*);
};




Coulomb_window::Coulomb_window(Point xy, int w, int h, const string& title )
	:Window(xy,w,h,title),
	circle(Point(100,100),50),
	start_button(Point(x_max()-150,5),40,20,"start",cb_start)
	
{
	circle.set_fill_color(Color::red);
	circle.set_color(Color::invisible);
	attach(circle);
	attach(start_button);
};



void Coulomb_window::cb_start(Address,Address pw)
{
	reference_to<Coulomb_window>(pw).start();
};

void timeout_callback(void* ptr)
{
	Coulomb_window* this_ = static_cast<Coulomb_window*>(ptr);
	this_->circle.move(10,0); this_->redraw();
	Fl::repeat_timeout(0.1, timeout_callback, static_cast<void*>(this_));
}

//円を動かす
void Coulomb_window::start()
{
	Fl::add_timeout(0.1, timeout_callback, static_cast<void*>(this));
}

int Coulomb_window::handle(int e)
{
	int ret = 0;
	switch (e) {
	case FL_FOCUS:
	case FL_UNFOCUS:
		ret = 1;
		break;
	case FL_KEYDOWN:
		if ( Fl::event_key('A') ) { ret = 1; circle.move(10,0); redraw(); }
		break;
	}
	return ret || Fl_Window::handle(e);
}

int main()
try{
	Coulomb_window win(Point(100,100),600,400,"Coulomb's_law");
	return gui_main();
}

catch(exception& e) {
	cerr << "exception: " << e.what() << '\n';
	return 1;
}
catch (...) {
	cerr << "some exception\n";
	return 2;
}
