-module(spierlangDB).
-compile(export_all).

-record(pos,{x,y}).
-record(bubble,{id,pos=#pos{},ray,tag=[]}).

makePos(X,Y) -> #pos{x=X,y=Y}.
makeBubble(Id,X,Y,Ray) -> makeBubble(Id,X,Y,Ray,[]).
makeBubble(Id,X,Y,Ray,Tag) when is_list(Tag) -> 
	#bubble{id=Id,pos=makePos(X,Y),ray=Ray,tag=Tag}.

initMnesia() ->
	mnesia:create_table(bubble,[{attributes,record_info(fields,bubble)}]).

insertBubble(Bubble) when is_record(Bubble,bubble) ->
    Fun = fun() ->
                  mnesia:write(Bubble)
          end,
    mnesia:transaction(Fun).

getBubbleById(Id) when is_integer(Id) -> mnesia:read(bubble,Id).
getBubblesByCollision(X,Y,Ray) ->
    F = fun() ->
		MatchHead = #bubble{x=$2, y=$3, ray=$4},
		Guard = {'>', pow('$2',2)+po'$3*$3', '($4+Ray)**2'},
		Result = '$1',
		mnesia:select(Tab,[{MatchHead, [Guard], [Result]}]),
	end,
    mnesia:transaction(F).
