/*void RenderGenerateToggle(ref FGAnimation animation)
     * {
     *  toggle = (bool)EditorGUILayout.BeginToggleGroup("Generate From Sheet", toggle);
     *  sheet = (Texture2D)EditorGUILayout.ObjectField(sheet, typeof(Texture2D), true);
     *  numRows = (int)EditorGUILayout.IntField("Number of Rows", numRows);
     *  numCols = (int)EditorGUILayout.IntField("Number of Columns", numCols);
     *  startIndex = (int)EditorGUILayout.IntField("Start Index", startIndex);
     *  endIndex = (int)EditorGUILayout.IntField("End Index", endIndex);
     *
     *  if (GUILayout.Button("Generate"))
     *  {
     *      if (numRows > 0 && numCols > 0 && endIndex >= startIndex)
     *      {
     *          Sprite[] createdSprites = GenerateFromSpriteSheet(sheet, numRows, numCols, startIndex, endIndex);
     *          for (int i = 0; i < createdSprites.Length; i++)
     *          {
     *              animation.AddFrame(new FGAnimationKeyFrame(createdSprites[i]));
     *          }
     *      }
     *  }
     *  EditorGUILayout.EndToggleGroup();
     * }*/

    void BuildAnimation(string spriteSheetName, int indexBegin, int indexEnd)
    {
        if (indexEnd < indexBegin || indexEnd < 0 || indexBegin < 0)
        {
            return;
        }

        Sprite[] allSprites = Resources.LoadAll <Sprite>(spriteSheetName);

        if (indexEnd >= allSprites.Length || indexBegin >= allSprites.Length)
        {
            return;
        }

        FGAnimation data = (FGAnimation)target;

        for (int i = indexBegin; i <= indexEnd; i++)
        {
            data.AddFrame(new FGAnimationKeyFrame(allSprites[i]));
        }
    }
    public override void OnInspectorGUI()
    {
        //base.DrawDefaultInspector();
        GUILayout.BeginVertical();
        FGAnimation data = (FGAnimation)target;

        data.animName = EditorGUILayout.TextField("Animation name: ", data.animName) as string;
        data.type     = (FGAnimationType)EditorGUILayout.Popup("Animation play type: ", (int)data.type, new string[] { "Loop", "Once", "Bounce", "Reverse" });

        //data.positionOffset = (Vector2)EditorGUILayout.Vector2Field("Offset: ", data.positionOffset);

        data.imageOffset = (Vector2)EditorGUILayout.Vector2Field("Image Offset Position: ", data.imageOffset);
        data.imageSize   = (Vector2)EditorGUILayout.Vector2Field("Image Size: ", data.imageSize);

        if (data.type == FGAnimationType.ONCE)
        {
            data.nextAnimation = EditorGUILayout.TextField("Next animation to go to after this one completes: ", data.nextAnimation) as string;

            if (data.attackData == null)
            {
                data.attackData = new FGAttackData();
            }
            data.attackData.guard           = (Guard)EditorGUILayout.Popup("Guard Type: ", (int)data.attackData.guard, new string[] { "LOW", "MID", "HIGH" });
            data.attackData.frameAdvOnBlock = (int)EditorGUILayout.IntField("Frame adv on block: ", (int)data.attackData.frameAdvOnBlock);
            data.attackData.frameAdvOnHit   = (int)EditorGUILayout.IntField("Frame adv on hit: ", (int)data.attackData.frameAdvOnHit);
            data.attackData.damage          = (int)EditorGUILayout.IntField("Damage: ", (int)data.attackData.damage);
            data.attackData.attackVector    = (Vector2)EditorGUILayout.Vector2Field("Direction vector for attack pushback", data.attackData.attackVector);
        }

        //RenderGenerateToggle(ref data);

        //List<int> removeFrames = new List<int>();
        if (data.frames.Length <= 0)
        {
            GUILayout.Label("Create animation from sprite sheet?");
            spriteSheet = (Texture2D)EditorGUILayout.ObjectField(spriteSheet, typeof(Texture2D), true);

            indxBeg = (int)EditorGUILayout.IntField("Starting Index: ", indxBeg);
            indxEnd = (int)EditorGUILayout.IntField("Ending Index: ", indxEnd);

            if (GUILayout.Button("Generate"))
            {
                if (spriteSheet != null)
                {
                    BuildAnimation(spriteSheet.name, indxBeg, indxEnd);
                }
            }
        }
        else
        {
            for (int i = 0; i < data.frames.Length; i++)
            {
                GUILayout.BeginVertical();
                FGAnimationStageType stage = (FGAnimationStageType)EditorGUILayout.Popup("Stage in animation: ", (int)data.frames[i].type, new string[] { "Startup", "Active", "Recovery" });
                int    frameCount          = (int)EditorGUILayout.IntField("Frame count: ", data.frames[i].frameCount);
                Sprite sprite = (Sprite)EditorGUILayout.ObjectField(data.frames[i].sprite, typeof(Sprite), true);

                data.ModifyFrameAtIndex(i, sprite, frameCount, stage);

                if (stage == FGAnimationStageType.ACTIVE)
                {
                    if (data.frames[i].hitBox == null)
                    {
                        data.frames[i].hitBox = new Rect();
                    }

                    data.frames[i].hitBox = (Rect)EditorGUILayout.RectField(data.frames[i].hitBox);
                }


                GUILayout.EndVertical();

                /*GUILayout.BeginHorizontal();
                 * for (int j = 0; j < data.frames[i].frameCount; j++)
                 * {
                 *  if (sprite.texture)
                 *  {
                 *
                 *  }
                 * }
                 * GUILayout.EndHorizontal();*/
                if (GUILayout.Button("Remove Key"))
                {
                    data.RemoveFrame(i);
                    break;
                }
            }
        }

        if (GUILayout.Button("Add Key Frame"))
        {
            data.AddFrame(new FGAnimationKeyFrame());
        }

        ShowAnimationPreview();
        GUILayout.EndVertical();

        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
            AssetDatabase.SaveAssets();
        }
    }