using Godot;
using Rokojori;
 
namespace Rokojori
{ 
  public class UILayouting
  {  
    public static Vector2 GetContentSize( Control control )
    {
      if ( control is UIRegion )
      {
        return ( (UIRegion) control ).contentSize;
      }

      return control.Size;
    }

    public static Vector2 GetContentOffset( Control control )
    {
      if ( control is UIRegion )
      {
        return ( (UIRegion) control ).contentOffset;
      }

      return control.Size;
    }

    public static void UpdateChild( Control control )
    {
      if ( ! control.Visible )
      {
        return;
      }
      
      if ( control is UIRegion )
      {
        var childUIRegion = (UIRegion) control;
        childUIRegion.Layout();
      }
      else if ( control is UIImage )
      {
        var tw = 0;
        var th = 0;

        if ( control is UIImage )
        {
          var uiImage = (UIImage) control;

          if ( uiImage.Texture == null )
          {
            uiImage.Size = new Vector2( 0, 0 );
            return;
          }

          tw = uiImage.Texture.GetWidth();
          th = uiImage.Texture.GetHeight();
        }

        var container = (UIStylePropertyContainer) control;

        var w = UINumber.Compute( control, UIStyleNumberProperty.Width, tw, tw / 100f );
        var h = UINumber.Compute( control, UIStyleNumberProperty.Height, th, th / 100f );

        // RJLog.Log( "Set Image Size", w, h );
        control.Size = new Vector2( w, h );

        if ( control is UIImage )
        {
          var uiImage = (UIImage) control;

          if ( uiImage.Material != null )
          {
            var ui = Unique<UI>.Get();

            if ( ui == null )
            {              
              ui = NodesWalker.Get().GetInParents( control, n => n is UI ) as UI;
            }

            if ( ui == null )
            {
              RJLog.Log( "No UI Found" );
              return;
            }

            if ( ui.settings == null )
            {
              //  RJLog.Log( "No UI.settings Found" );
              return;
            }

            if ( ui.settings.sizePropertyName == null )
            {
              RJLog.Log( "No UI.settings.sizePropertyName Found" );
              return;
            }

            //RJLog.Log( "Setting Size", ui.settings.sizePropertyName.propertyName, HierarchyName.Of( uiImage ) );
            ui.settings.sizePropertyName.Set( uiImage.Material, uiImage.Size );

            UIShaderProperties.UpdateProperties( uiImage, uiImage.Material );
            return;
          }
                    
        }
      } 
      else if ( control is UIText )
      {
          var text = (UIText) control;          

          var container = (UIStylePropertyContainer) control;

          text.uiTextLabelSettings.FontSize = 
            UINumber.ComputeInt( control, UIStyleNumberProperty.FontSize, UINumber.em(), UINumber.em() / 100f );
          text.uiTextLabelSettings.FontColor = 
            UIColor.Compute( control, UIStyleColorProperty.FontColor, Colors.White );

          text.uiTextLabelSettings.OutlineSize = 
            UINumber.ComputeInt( control, UIStyleNumberProperty.FontOutlineSize, 0 );         

          text.uiTextLabelSettings.OutlineColor = 
            UIColor.Compute( control, UIStyleColorProperty.FontOutlineColor, Colors.Transparent );

          text.uiTextLabelSettings.ShadowSize = 
            UINumber.ComputeInt( control, UIStyleNumberProperty.FontShadowSize, 0 );

          text.uiTextLabelSettings.ShadowColor = 
            UIColor.Compute( control, UIStyleColorProperty.FontShadowColor, Colors.Black );

          text.uiTextLabelSettings.ShadowOffset = new Vector2(
            UINumber.Compute( control, UIStyleNumberProperty.FontShadowOffsetX, 0 ),
            UINumber.Compute( control, UIStyleNumberProperty.FontShadowOffsetY, 0 )
          );            


          control.UpdateMinimumSize();

          if ( text.alwaysMinimumSize )
          {
            
            control.Size = control.GetMinimumSize();
          }
          else
          {
            var minSize = control.GetMinimumSize();

            var w = UINumber.Compute( control, UIStyleNumberProperty.Width, minSize.X, minSize.X / 100f );
            var h = UINumber.Compute( control, UIStyleNumberProperty.Height, minSize.Y, minSize.Y / 100f );

            // RJLog.Log( "Set Image Size", w, h );
            control.Size = new Vector2( w, h );
          }
          
      }
      else
      {
        // control.UpdateMinimumSize();
        // control.Size = control.GetMinimumSize();
      }

      UILayouting.UpdatePivot( control );
    }

    public static void SetPositionInParentAnchor( UIStylePropertyContainer container )
    {
      var control = (Control) container;
      var p = NodesWalker.Get().Parent( control ) as Control;
      
      var pWidth  = p == null ? UI.GetWindowWidth( control ) : UILayouting.GetWidth( p );
      var pHeight = p == null ? UI.GetWindowHeight( control ) : UILayouting.GetHeight( p );   
      
      var x = p.Position.X;
      var y = p.Position.Y;


      if ( ! UINumber.IsNullOrNone( container, UIStyleNumberProperty.Left ) )
      {
        var left  = UINumber.Compute( control, UIStyleNumberProperty.Left, 0 );
        x = left;
      }
      else if ( ! UINumber.IsNullOrNone( container, UIStyleNumberProperty.Right ) )
      {
        var right  = UINumber.Compute( control, UIStyleNumberProperty.Right, 0 );
        x = ( pWidth - UILayouting.GetWidth( control ) ) - right;
      }

      if ( ! UINumber.IsNullOrNone( container, UIStyleNumberProperty.Top ))
      {
        var top  = UINumber.Compute( control, UIStyleNumberProperty.Top, 0 );
        y = top;
      }
      else if ( ! UINumber.IsNullOrNone( container, UIStyleNumberProperty.Bottom ) )
      {
        var bottom  = UINumber.Compute( control, UIStyleNumberProperty.Bottom, 0 );
        y = ( pHeight - UILayouting.GetHeight( control ) ) - bottom;
      }

      // var margin       = UINumber.Compute( control, UIStyle.Margin( container ), 0 );
      // var marginLeft   = margin + UINumber.Compute( control, UIStyle.MarginLeft( container ), 0 );
      // var marginTop    = margin + UINumber.Compute( control, UIStyle.MarginRight( container ), 0 );
      // var marginRight   = margin + UINumber.Compute( control, UIStyle.MarginRight( container ), 0 );
      // var marginBottom   = margin + UINumber.Compute( control, UIStyle.MarginBottom( container ), 0 );
      
      // UILayouting.SetPosition( control, new Vector2( x - ( marginLeft + marginRight ), y - ( marginTop + marginBottom ) ) );

      UILayouting.SetPosition( control, new Vector2( x, y ) );
    }

    public static void UpdatePivot( Control c )
    {
      if ( ! ( c is UIImage || c is UIRegion || c is UIText ) )
      {
        return;
      }

      var container = c as UIStylePropertyContainer;

      var pivotX  = UINumber.Compute( c, UIStyleNumberProperty.PivotX, 0.5f * c.Size.X, c.Size.X );
      var pivotY  = UINumber.Compute( c, UIStyleNumberProperty.PivotY, 0.5f * c.Size.Y, c.Size.Y );

      if ( c is UIText text && ! text.alwaysMinimumSize )
      {
        pivotX = 0;
        pivotY = 0;        
      }

      c.PivotOffset = new Vector2( pivotX, pivotY );
      
      

      c.Rotation = MathX.DegreesToRadians * UINumber.Compute( c, UIStyleNumberProperty.Rotation, 0 ); 

      var scale = UINumber.Compute( c, UIStyleNumberProperty.Scale, 1, 1 );

      c.Scale = new Vector2( 
        UINumber.Compute( c, UIStyleNumberProperty.ScaleX, 1, 1 ) ,
        UINumber.Compute( c, UIStyleNumberProperty.ScaleY, 1, 1 )
      ) * scale; 

    }

    public static void SetPosition( Control c, Vector2 position )
    {
      if ( UIStyling.HasInnerMargins( c ) )
      {
        var container = c as UIStylePropertyContainer;

        var margin       = UINumber.Compute( c, UIStyleNumberProperty.Margin, 0 );
        var marginLeft   = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginLeft, 0 );
        var marginTop    = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginTop, 0 );

        position.X += marginLeft;
        position.Y += marginTop;
 
      }

      c.Position = position;
    }

    public static float GetWidth( Control c )
    {
      if ( UIStyling.HasInnerMargins( c ) )
      {
        var container = c as UIStylePropertyContainer;

        var margin       = UINumber.Compute( c, UIStyleNumberProperty.Margin, 0 );
        var marginLeft   = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginLeft, 0 );
        var marginRight   = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginRight, 0 );

        return c.Size.X + marginLeft + marginRight;
 
      }

      return c.Size.X;
    } 
    
    public static float GetHeight( Control c )
    {
      if ( UIStyling.HasInnerMargins( c ) )
      {
        var container = c as UIStylePropertyContainer;

        var margin       = UINumber.Compute( c, UIStyleNumberProperty.Margin, 0 );
        var marginTop     = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginTop, 0 );
        var marginBottom  = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginBottom, 0 );

        return c.Size.Y + marginTop + marginBottom;
 
      }

      return c.Size.Y;
    }

  }
}