shader_type canvas_item;

#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Colors.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Math.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Transform.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/SDF.gdshaderinc"

uniform float opacity:hint_range(0,1) = 1;
uniform bool blendLinear = false;
uniform vec2 size; 

group_uniforms Background; 
uniform float sharpness = 5;
uniform float borderRadius = 5;
uniform float offset;
group_uniforms; 

group_uniforms BackgroundFill; 
uniform vec4 fillColor:source_color;
uniform sampler2D fill;
uniform vec4 fillUVTransform;
uniform sampler2D screenFillMultiply; 
uniform vec4 screenfillMultiplyUVTransform;
uniform vec2 screenfillMultiplyUVMovement;
group_uniforms; 

group_uniforms BackgroundStroke; 
uniform float strokeSize = 5;
uniform vec4 strokeColor:source_color;
uniform sampler2D stroke;
uniform vec4 strokeUVTransform;
uniform sampler2D screenStrokeMultiply;
uniform vec4 screenStrokeMultiplyUVTransform;
uniform vec2 screenStrokeMultiplyUVMovment;
group_uniforms; 

group_uniforms Slider; 
uniform vec2 sliderSize;
uniform vec2 sliderSizeMargins;
uniform float sliderBorders;
uniform vec2 sliderValue;
group_uniforms; 

group_uniforms SliderFill;
uniform vec4 sliderFillColor:source_color;
group_uniforms; 

group_uniforms SliderStroke;
uniform vec4 sliderStrokeColor:source_color;
uniform float sliderStrokeSize = 2;
group_uniforms; 


vec4 createBorders( float borderSize, vec2 rectSize )
{
  float minS = min( rectSize.x, rectSize.y ); 
  vec4 borders = vec4( min( minS, borderSize ) ); 
  // borders.xz = min( borders.xz, vec2( rectSize.x / 4.0 ) ); 
  // borders.yw = min( borders.yw, vec2( rectSize.y / 4.0 ) ); 

  return borders;
}

vec4 getSliderLayer( vec2 pixelPosition )
{
  vec2 sliderValueRange = size - ( sliderSize + sliderSizeMargins );
  vec2 sliderValueOffset = ( sliderSize + sliderSizeMargins ) / 2.0;
  vec2 sliderPosition = sliderValueOffset + sliderValueRange * sliderValue;



  vec4 borders = createBorders( sliderBorders, sliderSize ); 

  float sd = sdRoundedBox( pixelPosition - sliderPosition, sliderSize / 2.0, borders );

  // vec4 sliderLayer = sdf_coloredShape( sd, sliderStrokeSize, sliderFillColor, sliderStrokeColor );

  vec2 sliderShape = sdf_shape( sd, sliderStrokeSize );
  vec4 sliderFill = fade( sliderFillColor, sliderShape.x );
  vec4 sliderStroke = fade( sliderStrokeColor, sliderShape.y );

  vec4 sliderLayer = blendMode_alpha( sliderFill, sliderStroke ); 
  sliderLayer = sdf_coloredShape( sd, sliderStrokeSize, sliderFillColor, sliderStrokeColor );
  // float sliderMask = sdf_stroke( sdRoundedBox( pixelPosition - sliderPosition, sliderSize2, borders ) );
  // vec4 sliderLayer = vec4( _sliderColor.rgb, _sliderColor.a * sliderMask );

  return sliderLayer;
}

void fragment()
{
  // Fill
  vec2 fillUV = tilingOffsetRepeat( UV, fillUVTransform );
  vec2 fillMoved = screenfillMultiplyUVMovement * TIME;
  vec4 screenFillMovement = vec4( 0,0, fillMoved.x, fillMoved.y );
  vec2 screenFillMultiplyUV = tilingOffsetRepeat( SCREEN_UV, screenfillMultiplyUVTransform + screenFillMovement );
  vec4 combinedfillColor = fillColor * texture( fill, fillUV ) * texture( screenFillMultiply, screenFillMultiplyUV );
  
  // Stroke
  vec2 strokeUV = tilingOffsetRepeat( UV, strokeUVTransform );
  vec2 strokeMoved = screenStrokeMultiplyUVMovment * TIME;
  vec4 screenstrokeMovement = vec4( 0,0, strokeMoved.x, strokeMoved.y );
  vec2 screenStrokeMultiplyUV = tilingOffsetRepeat( SCREEN_UV, screenStrokeMultiplyUVTransform + screenstrokeMovement );
  vec4 combinedStrokeColor = strokeColor * texture( stroke, strokeUV ) * texture( screenStrokeMultiply, screenStrokeMultiplyUV );

  vec2 pixelPosition = size * UV;
  
  vec4 bgBorders = createBorders( borderRadius, size / 2.0 - vec2( offset  ) );

  float bgSD = sdRoundedBox( pixelPosition - size / 2.0, size / 2.0 - vec2( offset ), bgBorders );
  vec4 bgLayer = sdf_coloredShape( bgSD, strokeSize, combinedfillColor, combinedStrokeColor );
  
  float sliderDistance =  length( pixelPosition - sliderValue );
  vec4 sliderLayer = getSliderLayer( pixelPosition );
  
  if ( blendLinear )
  {
    bgLayer.rgb  = SRGBtoLINEAR( bgLayer.rgb );
    sliderLayer.rgb  = SRGBtoLINEAR( sliderLayer.rgb );
  }
  
  vec4 outputColor = blendMode_alpha( bgLayer, sliderLayer );

  if ( blendLinear )
  {
    outputColor.rgb = LINEARtoSRGB( outputColor.rgb );
  }

  vec4 tex = texture( TEXTURE, UV );

  COLOR = vec4( outputColor.rgb * tex.rgb, outputColor.a * opacity * tex.a);


}
