
using Godot;
using Rokojori;
using System.Collections.Generic;
 
namespace Rokojori
{ 
  [Tool]
  [GlobalClass,Icon("res://addons/rokojori_action_library/Icons/UIImage.svg")]
  public partial class UIImage:TextureRect, UIStylePropertyContainer
  { 
    [Export]
    public bool freezeImageType = false;

    UIImageType _imageType;
    [Export]
    public UIImageType imageType
    {
      get => _imageType;
      set 
      {
        if ( freezeImageType )
        {
          return;
        }
        
        if ( _imageType != value  )
        {
          if ( _imageType != null )
          {
            _imageType.Clear( this );
          }
        }

        _imageType = value;

        this.LogInfo( Material == null ? "null" : ( Material.GetType().Name ) );
        UpdateImageType();
        this.LogInfo( Material == null ? "null" : ( Material.GetType().Name ) );
      }
    }

    void UpdateImageType()
    {
      ResetImageType();
      
      if ( _imageType != null )
      {
        _imageType.Assign( this );
      }
    }

    void ResetImageType()
    {
      ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize;
      StretchMode = TextureRect.StretchModeEnum.Scale;
      Material = null;
    }

    public void CopyNumberShaderPropertyFrom( CustomMaterialProperty<float> name, UINumber number, TransitionSettings transition )
    {
      SetNumberShaderProperty( name.GetPropertyName<FloatPropertyName>(), number, transition );
    }

    public void SetNumberShaderProperty( FloatPropertyName name, UINumber number, TransitionSettings transition )
    {
      if ( name == null || name.propertyName == null )
      {
        this.LogInfo( "Name is null" );
        return;
      }

      
      for ( int i = 0; i < numberProperties.Length; i++ )
      {
        if ( numberProperties[ i ] == null || numberProperties[ i ].floatPropertyName == null )
        {
          return;
        }
      }

      if ( number == null )
      {
        var index = Arrays.FindIndex( numberProperties, p => p.floatPropertyName.propertyName == name.propertyName );
        numberProperties = Arrays.RemoveIndex( numberProperties, index );
        return;
      }
      
      var shaderUINumber = Arrays.Find( numberProperties, p => p.floatPropertyName.propertyName == name.propertyName );

      if ( shaderUINumber == null )
      {
        shaderUINumber = new ShaderUINumber();
        shaderUINumber.floatPropertyName = name;
        numberProperties = Arrays.Add( numberProperties, shaderUINumber );
      }

      shaderUINumber.number = number;
      shaderUINumber.transitionSettings = transition;
    }

    public void CopyColorShaderPropertyFrom( CustomMaterialProperty<Color> name, UIColor color )
    {
      SetColorShaderProperty( name.GetPropertyName<ColorPropertyName>(), color );
    }

    public void SetColorShaderProperty( ColorPropertyName name, UIColor color )
    {
      if ( name == null || name.propertyName == null )
      {
        this.LogInfo( "Name is null" );
        return;
      }

      for ( int i = 0; i < colorProperties.Length; i++ )
      {
        if ( colorProperties[ i ] == null || colorProperties[ i ].colorPropertyName == null )
        {
          return;
        }
      }


      if ( color == null )
      {
        var index = Arrays.FindIndex( colorProperties, p => p != null && p.colorPropertyName.propertyName == name.propertyName );
        colorProperties = Arrays.RemoveIndex( colorProperties, index );
        return;
      }

      
      
     

      var shaderUIColor = Arrays.Find( colorProperties, p => p != null && p.colorPropertyName.propertyName == name.propertyName );
      
      if ( shaderUIColor == null )
      {
        shaderUIColor = new ShaderUIColor();
        shaderUIColor.colorPropertyName = name;
        colorProperties = Arrays.Add( colorProperties, shaderUIColor );
      }

      shaderUIColor.color = color;
    }

    [Export]
    public UIStyle parentStyle;

    [ExportGroup("Size & Margins")]
    [Export]
    public UINumber width;

    [Export]
    public UINumber height;


    [Export] 
    public UINumber margin;

    [Export] 
    public UINumber marginLeft;
    [Export] 
    public UINumber marginTop;
    [Export] 
    public UINumber marginRight;
    [Export] 
    public UINumber marginBottom;


    [ExportGroup( "Position" )]
    [Export]
    public UIPosition position;
    [Export]
    public UILineWrap lineWrap;
    [Export]
    public UINumber left;
    [Export]
    public UINumber top;
    [Export]
    public UINumber right;
    [Export]
    public UINumber bottom;

    
    [ExportGroup( "Rotation & Scale" )]
    [Export]
    public UINumber pivotX;
    [Export]
    public UINumber pivotY;

    [Export]
    public UINumber rotation;

    [Export]
    public UINumber scale;

    [Export]
    public UINumber scaleX;
    [Export]
    public UINumber scaleY;

    [ExportGroup( "Shader Properties" )]
    [Export]
    public ShaderUIColor[] colorProperties = new ShaderUIColor[ 0 ];
    public List<ActiveStyleTransition<UIColor,ColorPropertyName>> activeShaderColorTransitions = new List<ActiveStyleTransition<UIColor,ColorPropertyName>>();
    public List<ActiveStyleTransition<UIColor,ColorPropertyName>> GetActiveShaderUIColorTransitions()
    {
      return activeShaderColorTransitions;
    }

    [Export]
    public ShaderUINumber[] numberProperties = new ShaderUINumber[ 0 ];
    public List<ActiveStyleTransition<UINumber,FloatPropertyName>> activeShaderNumberTransitions = new List<ActiveStyleTransition<UINumber,FloatPropertyName>>();
    public List<ActiveStyleTransition<UINumber,FloatPropertyName>> GetActiveShaderUINumberTransitions()
    {
      return activeShaderNumberTransitions;
    }

    [ExportGroup("Transitions")]
    [Export]
    public TransitionSettingsAll transitionSettings;
    public TransitionSettingsAll GetTransitionSettingsAll()
    {
      return transitionSettings;
    }

    [Export]
    public UINumberTransition[] numberTransitions = new UINumberTransition[0];
    public UINumberTransition[] GetNumberTransitions()
    {
      return numberTransitions;
    }
    
    public List<ActiveStyleTransition<UINumber,UIStyleNumberProperty>> activeNumberTransitions = new List<ActiveStyleTransition<UINumber,UIStyleNumberProperty>>();
    public List<ActiveStyleTransition<UINumber,UIStyleNumberProperty>> GetActiveUINumberTransitions()
    {
      return activeNumberTransitions;
    }

    [Export]
    public UIColorTransition[] colorTransitions = new UIColorTransition[ 0 ];
    public UIColorTransition[] GetColorTransitions()
    {
      return colorTransitions;
    }
    
    public List<ActiveStyleTransition<UIColor,UIStyleColorProperty>> activeColorTransitions = new List<ActiveStyleTransition<UIColor,UIStyleColorProperty>>();
    public List<ActiveStyleTransition<UIColor,UIStyleColorProperty>> GetActiveUIColorTransitions()
    {
      return activeColorTransitions;
    }

    

    public UIStyle GetUIStyleParent()
    {
      return parentStyle;
    }

    public UIPosition GetUIPosition()
    {
      return position;
    }

    public UILineWrap GetUILineWrap()
    {
      return lineWrap;
    }

    public UILayout GetUILayout()
    {
      return UILayout.___;
    }


    public ShaderUIColor[] GetShaderUIColors()
    {
      return colorProperties;
    }

    public ShaderUINumber[] GetShaderUINumbers()
    {
      return numberProperties;
    }

    public UINumber GetUIStyleNumberProperty( UIStyleNumberProperty property )
    {
      switch ( property )
      {
        case UIStyleNumberProperty.Width:  return width;
        case UIStyleNumberProperty.Height: return height;

        case UIStyleNumberProperty.Margin:       return margin;

        case UIStyleNumberProperty.MarginLeft:   return marginLeft;
        case UIStyleNumberProperty.MarginRight:  return marginRight;
        case UIStyleNumberProperty.MarginTop:    return marginTop;
        case UIStyleNumberProperty.MarginBottom: return marginBottom;


        case UIStyleNumberProperty.Left:   return left;
        case UIStyleNumberProperty.Right:  return right;
        case UIStyleNumberProperty.Top:    return top;
        case UIStyleNumberProperty.Bottom: return bottom;

        case UIStyleNumberProperty.PivotX:   return pivotX;
        case UIStyleNumberProperty.PivotY:   return pivotY;
        case UIStyleNumberProperty.Rotation: return rotation;

        case UIStyleNumberProperty.Scale:    return scale;
        case UIStyleNumberProperty.ScaleX:   return scaleX;
        case UIStyleNumberProperty.ScaleY:   return scaleY;
      }

      return null;
    }

    public UIColor GetUIStyleColorProperty( UIStyleColorProperty property )
    {
      return null;
    }

    public Font GetFont()
    {
      return null;
    }

    public Vector2 GetUISize()
    {
      return GetSize();
    }
  }
}