/**  svg_change_dim( +Svg, +Dim, -New, -Res)

Changes the overall dimension (Dim) according to form of New.<br>
New SVG is in Res. New can either be a number, an atom or term of the form: inc(By).

The predicate changes both the Dim= attribute and the relevant dimension in viewBox attribute.

==
?- lib(os_lib).
?- 
   RelF = 'svg/examples/rea_smp-e10_fclr.svg',
   svg_size( pack(RelF), OrgW, OrgH ),
   os_postfix( long, RelF, LngF ),
   svg_change_dim( pack(RelF), width, inc(100), pack(LngF) ),
   svg_size( pack(LngF), LngW, LngH ).
==

@author nicos angelopoulos
@version  0.1 2018/2/

*/
svg_change_dim( SvgIn, Dim, NewTkn, Res ) :-
    svg( SvgIn, Svg ),
    svg_change_dim_to_abs_value( NewTkn, Svg, Dim, SvgOut ),
    % svg_change_dim_abs( Svg, Dim, New, SvgOut ),
    svg( Res, SvgOut ).

svg_change_dim_to_abs_value( inc(By), Svg, Dim, New ) :-
    !,
    svg_size( Svg, Width, Height ),
    svg_change_dim_to_abs_value_increase( Dim, Width, Height, By, Abs ),
    svg_change_dim_to_abs_value( Abs, Svg, Dim, New ).
svg_change_dim_to_abs_value( NewValPrv, Svg, Dim, NewSvg ) :-
    Svg = svg(Atts,Elems),
    ( number(NewValPrv) -> atom_concat(NewValPrv,pt,NewVal); NewVal = NewValPrv ),
    svg_change_dim_attrributes( Atts, Dim, NewValPrv, NewVal, NewAtts ),
    % trace,
    svg_change_dim_background( Elems, NewValPrv, NewElems ),
    NewSvg = svg(NewAtts,NewElems).

svg_change_dim_background( Elems, NewVal, NewElems ) :-
    select( element(g,GAtts,GElems), Elems, element(g,GAtts,NewGElems), NewElems ),
    % Elems = element(g,GAtts,GElems),
    select( element(polygon,PAtts,PElems), GElems, element(polygon,NewPAtts,PElems), NewGElems ),
    !,
    svg_change_dim_background_attrs( PAtts, NewVal, NewPAtts ).
    % NewElems = element(g,GAtts,NewGElems).
svg_change_dim_background( Elems, _NewVal, Elems ).

svg_change_dim_background_attrs( PAtts, NewVal, NewPAtts ) :-
    select( points=Points, PAtts, points=NewPoints, NewPAtts ),
    atomic_list_concat( [A,B,C,D,E], ' ', Points ),
    atomic_list_concat( [_X1,Y1], ',', C ),
    atomic_list_concat( [_X2,Y2], ',', D ),
    NewX is NewVal - 4, % fixme:
    atomic_list_concat( [NewX,Y1], ',', NewC ),
    atomic_list_concat( [NewX,Y2], ',', NewD ),
    atomic_list_concat( [A,B,NewC,NewD,E], ' ', NewPoints ),
    !.

svg_change_dim_attrributes( [], _Dim, _NumVal, _NewVal, [] ).
svg_change_dim_attrributes( [viewBox=ViewBox|T], Dim, NumVal, NewVal, [viewBox=NewViewBox|NewAtts] ) :-
    !,
    svg_change_dim_viewbox( ViewBox, Dim, NumVal, NewViewBox ),
    svg_change_dim_attrributes( T, Dim, NumVal, NewVal, NewAtts ).
svg_change_dim_attrributes( [Dim=_OldVVal|T], Dim, NumVal, NewVal, [Dim=NewVal|NewAtts] ) :-
    !,
    svg_change_dim_attrributes( T, Dim, NumVal, NewVal, NewAtts ).
svg_change_dim_attrributes( [H|T], Dim, NumVal, NewVal, [H|NewAtts] ) :-
    svg_change_dim_attrributes( T, Dim, NumVal, NewVal, NewAtts ).

svg_change_dim_viewbox( ViewBox, Dim, NewVal, NewViewBox ) :-
    atomic_list_concat( [X0,Y0,OldW,OldH], ' ', ViewBox ),
    svg_change_dim_viewbox_dim( Dim, OldW, OldH, NewVal, NewW, NewH ),
    atomic_list_concat( [X0,Y0,NewW,NewH], ' ', NewViewBox ).

svg_change_dim_viewbox_dim( width, _OldW, Height, NewW, NewW, Height ).
svg_change_dim_viewbox_dim( height, Width, _OldH, NewH, Width, NewH ).

svg_change_dim_to_abs_value_increase( width, Width, _Height, By, Abs ) :-
    Abs is Width + By.
svg_change_dim_to_abs_value_increase( height, _Width, Height, By, Abs ) :-
    Abs is Height + By.