-module(mywidgets).

-export([color_selection/2, color_selection_process/3, color_selection_set_color/2]).

%% Color selection widget
%% Spawn a new process
color_selection(Container, Options) ->
	Pid = spawn_link(mywidgets, color_selection_process, [self(), Container, Options]),
	%% Wait for a response from the created widget
	%% with the bounding frame and return it
	receive
		{Pid, Frame} ->
			{Pid, Frame}
	end.

%% Make a new process
%% The new process makes the frame
%% This makes all the events from the frame come to this process!
color_selection_process(ParentProcess, Container, Options) ->
	%% Monitor the parent process so we can close when it does
	erlang:monitor(process, ParentProcess),

	%% Make a frame to exist in
	Frame = gs:frame(Container, Options),

	%% Some stuff should be done before resuming the calling process
	%% Overriding dimensions is a good thing to do here
	%% Actually, almost all of the setup should be done before returning
	gs:config(Frame, [{height, 195}]),

	%% This section is dependent on what you are implementing
	%% I am making a color selector
	%% See if the programmer supplied us with a color
	SuppliedColor = gs:read(Frame, data),
	if
		is_tuple(SuppliedColor), size(SuppliedColor) == 3 ->
			{Red, Green, Blue} = SuppliedColor;
		true ->
			{Red, Green, Blue} = {0, 0, 0}
	end,
	%% Make scale widgets for the colors
	gs:scale(Frame,[{text,"Red"},{y,0},{range,{0,255}},
                {orient,horizontal},
                {height,65},{data,red},{pos,Red}]),
	gs:scale(Frame,[{text,"Blue"},{y,65},{range,{0,255}},
                {orient,horizontal},
                {height,65},{data,blue},{pos,Green}]),
	gs:scale(Frame,[{text,"Green"},{y,130},{range,{0,255}},
                {orient,horizontal},
                {height,65},{data,green},{pos,Blue}]),

	%% Send a message to my parent with my process id and the frame
	ParentProcess ! {self(), Frame},

	%% Enter the event loop
	color_selection_loop(ParentProcess, Frame, {Red, Green, Blue}).

color_selection_loop(ParentProcess, Frame, {Red, Green, Blue}) ->
	gs:config(Frame, [{bg, {Red, Green, Blue}}, {data, {Red, Green, Blue}}]),
	receive
		%% Events changing color
		{gs,_,click,red,[New_Red|_]} ->
			NewColor = {New_Red,Green,Blue},
			%% Notify the parent process with an event for color change
			ParentProcess ! {mywidgets,Frame,color,NewColor,[NewColor]},
			color_selection_loop(ParentProcess, Frame,NewColor);
		{gs,_,click,green,[New_Green|_]} ->
			NewColor = {Red,New_Green,Blue},
			ParentProcess ! {mywidgets,Frame,color,NewColor,[NewColor]},
			color_selection_loop(ParentProcess, Frame,NewColor);
		{gs,_,click,blue,[New_Blue|_]} ->
			io:format("gaagdfa~n"),
			NewColor = {Red,Green,New_Blue},
			ParentProcess ! {mywidgets,Frame,color,NewColor,[NewColor]},
			color_selection_loop(ParentProcess, Frame,NewColor);
		%% Parent Process asking for color change
		{ParentProcess,set_color,NewColor} ->
			io:format("gaha~n"),
			color_selection_loop(ParentProcess, Frame,NewColor);
		%% Parent process closed
		%% Everyone needs this
		{'DOWN',_,_,ParentProcess,_} ->
			exit(normal)
	end.

%% Interface for a program to send commands to the widget
%% A blocking command could be made by waiting for the response
color_selection_set_color(Pid, Color) ->
	Pid ! {self(), set_color, Color}.


