﻿using UnityEngine;
using UnityEngine.Animations;
using UnityEditor;
using UnityEditor.Animations;
using VRC.SDK3.Avatars.Components;
using VRC.SDK3.Avatars.ScriptableObjects;
using System.IO;


namespace CRPen2
{
    public partial class CRPen2Editor : Editor
    {
        // CreateFolders.
        private void CreateFolders(string path)
        {
            if (AssetDatabase.IsValidFolder(path))
                return;

            string parent = System.IO.Path.GetDirectoryName(path);
            string child = System.IO.Path.GetFileName(path);
            CreateFolders(parent);
            AssetDatabase.CreateFolder(parent, child);
        }

        // CopyAsset.
        private Object CopyAsset(Object source, string path)
        {
            string assetPath = AssetDatabase.GetAssetPath(source);

            return CopyAsset(assetPath, path);
        }
        private Object CopyAsset(string source, string path)
        {
            if (AssetDatabase.CopyAsset(source, path))
            {
                return AssetDatabase.LoadAssetAtPath<Object>(path);
            }

            return null;
        }

        // Make Backup.
        private string MakeBackup(Object target, string folderNanme)
        {
            string path = AssetDatabase.GetAssetPath(target);
            string directory = Path.GetDirectoryName(path);
            string name = Path.GetFileName(path);

            string backupPath = directory + "/" + folderNanme + "/" + name;

            CreateFolders(Path.GetDirectoryName(backupPath));
            CopyAsset(target, backupPath);

            return backupPath;
        }

        // Draw GuiLine.
        void GuiLine(int i_height = 1, int padding = 5)
        {
            GUILayout.Space(padding);
            Rect rect = EditorGUILayout.GetControlRect(false, i_height);
            rect.height = i_height;
            EditorGUI.DrawRect(rect, new Color(0.5f, 0.5f, 0.5f, 1));
            GUILayout.Space(padding);
        }
    }
}