// Vector2 GetAnchorPos(SerializedProperty spriteAnchorProp, Sprite sprite) {
        //     Vector2 position = spriteAnchorProp.FindPropertyRelative("anchor.position").vector3Value
        //             * sprite.pixelsPerUnit;
        //     position.y = -position.y;
        //     Vector2 pivot = new Vector2(sprite.pivot.x, sprite.rect.height - sprite.pivot.y);
        //     Vector2 handlePos = GetSpritePos(sprite) + (pivot + position) * GetSpriteScale(sprite);
        //     return handlePos;
        // }

        // void DrawAnchor(SerializedProperty spriteAnchorProp, Sprite sprite, Vector2 pos) {
        //     Vector2 handlePos = GetAnchorPos(spriteAnchorProp, sprite);
        //     int controlId = GUIUtility.GetControlID("Slider1D".GetHashCode(), FocusType.Keyboard);
        //     GUIStyle handle = new GUIStyle("U2D.pivotDot");
        //     if (handlePos.x < handle.fixedWidth / 2 || handlePos.x > spriteThumbnailSize.x - handle.fixedWidth / 2
        //             || handlePos.y < handle.fixedHeight / 2 || handlePos.y > spriteThumbnailSize.x - handle.fixedHeight / 2)
        //         return;
        //     handlePos += pos;
        //     Rect handleRect = new Rect(handlePos.x - handle.fixedWidth / 2, handlePos.y - handle.fixedHeight / 2,
        //             handle.fixedWidth, handle.fixedHeight);
        //     if (Event.current.type == EventType.Repaint)
        //         handle.Draw(handleRect, GUIContent.none, controlId);
        // }

        // void CopyAnchor(SerializedProperty spriteAnchorProp) {
        //     string s = string.Format("__ClingyAnchor|{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}",
        //             spriteAnchorProp.FindPropertyRelative("anchor.position").vector3Value.x,
        //             spriteAnchorProp.FindPropertyRelative("anchor.position").vector3Value.y,
        //             spriteAnchorProp.FindPropertyRelative("anchor.position").vector3Value.z,
        //             spriteAnchorProp.FindPropertyRelative("anchor.rotation").quaternionValue.x,
        //             spriteAnchorProp.FindPropertyRelative("anchor.rotation").quaternionValue.y,
        //             spriteAnchorProp.FindPropertyRelative("anchor.rotation").quaternionValue.z,
        //             spriteAnchorProp.FindPropertyRelative("anchor.rotation").quaternionValue.w,
        //             spriteAnchorProp.FindPropertyRelative("anchor.sortingOffset").intValue,
        //             spriteAnchorProp.FindPropertyRelative("anchor.flipX").boolValue,
        //             spriteAnchorProp.FindPropertyRelative("anchor.flipY").boolValue);
        //     EditorGUIUtility.systemCopyBuffer = s;
        // }

        // void PasteAnchor(SerializedProperty spriteAnchorProp) {
        //     if (!EditorGUIUtility.systemCopyBuffer.StartsWith("__ClingyAnchor"))
        //         return;
        //     string[] parts = EditorGUIUtility.systemCopyBuffer.Split('|');
        //     if (parts.Length != 11)
        //         return;
        //     Vector3 v = new Vector3(float.Parse(parts[1]), float.Parse(parts[2]), float.Parse(parts[3]));
        //     spriteAnchorProp.FindPropertyRelative("anchor.position").vector3Value = v;
        //     Quaternion q = new Quaternion(float.Parse(parts[4]), float.Parse(parts[5]), float.Parse(parts[6]),
        //             float.Parse(parts[7]));
        //     spriteAnchorProp.FindPropertyRelative("anchor.rotation").quaternionValue = q;
        //     spriteAnchorProp.FindPropertyRelative("anchor.sortingOffset").intValue = int.Parse(parts[8]);
        //     spriteAnchorProp.FindPropertyRelative("anchor.flipX").boolValue = bool.Parse(parts[9]);
        //     spriteAnchorProp.FindPropertyRelative("anchor.flipY").boolValue = bool.Parse(parts[10]);
        // }

        void OnEnable()
        {
            framesRL = new ReorderableList(serializedObject, serializedObject.FindProperty("frames"), true,
                                           true, false, true);
            framesRL.drawHeaderCallback = (Rect rect) => {
                EditorGUI.LabelField(rect, "Sprites");
            };
            framesRL.elementHeightCallback = (int index) => {
                if (serializedObject.FindProperty("frames").arraySize == 0)
                {
                    return(0);
                }
                return(60);
            };
            framesRL.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
                Vector2                  thumbSize    = new Vector2(rect.height - 15, rect.height - 15);
                SpriteParams             spriteParams = (SpriteParams)target;
                SpriteParams.SpriteFrame frame        = spriteParams.GetFrame(index);
                DrawSpriteAndPreview(frame,
                                     (Sprite)serializedObject.FindProperty("previewSprite").objectReferenceValue,
                                     ClingyEditorUtils.GetSpriteAspectSize(frame.sprite, thumbSize), rect.position);
                // DrawAnchor(framesProp, sprite, rect.position);
                float btnWidth = (rect.width) / 3f - thumbSize.x + 20;
                if (GUI.Button(new Rect(rect.x + 60, rect.y + rect.height / 2 - 11, btnWidth, 20), "Edit"))
                {
                    serializedObject.FindProperty("frameToEdit").intValue = index;
                    ShowEditor();
                }
                // if (GUI.Button(new Rect(rect.x + 60 + btnWidth + 5, rect.y + rect.height / 2 - 11, btnWidth, 20),
                //         "Copy")) {
                //     // CopyAnchor(framesProp);
                // }
                // if (GUI.Button(new Rect(rect.x + 60 + btnWidth * 2 + 10, rect.y + rect.height / 2 - 11, btnWidth, 20),
                //         "Paste")) {
                //     // PasteAnchor(framesProp);
                // }
            };
        }
        void OnGUI()
        {
            if (!Selection.activeObject || !(Selection.activeObject is SpriteParams))
            {
                Reset();
                GUILayout.Label("No Clingy Sprite Params selected");
                return;
            }

            SpriteParams _spriteParams = (SpriteParams)Selection.activeObject;

            if (spriteParams != _spriteParams || serializedObject == null)
            {
                Reset();
                spriteParams     = _spriteParams;
                serializedObject = new SerializedObject(spriteParams);
            }

            viewSize = new Vector2(position.width, position.height) * 1.5f;

            serializedObject.Update();

            SerializedProperty framesProp = serializedObject.FindProperty("frames");

            if (framesProp.arraySize == 0)
            {
                paramsRL    = null;
                frameToEdit = -1;
                GUILayout.Label("Add some sprites in the inspector to get started!");
                return;
            }

            int _frameToEdit = serializedObject.FindProperty("frameToEdit").intValue;

            if (_frameToEdit >= framesProp.arraySize)
            {
                _frameToEdit = 0;
            }

            SerializedProperty frameProp = framesProp.GetArrayElementAtIndex(_frameToEdit);

            if (_frameToEdit != frameToEdit || paramsRL == null)
            {
                paramsRL = ParamListEditor.CreateReorderableList(serializedObject,
                                                                 frameProp.FindPropertyRelative("paramList._params"), "Params");
            }
            frameToEdit = _frameToEdit;
            SerializedProperty spriteProp = frameProp.FindPropertyRelative("sprite");
            Sprite             sprite     = (Sprite)spriteProp.objectReferenceValue;

            if (!sprite)
            {
                return;
            }

            GUI.Box(new Rect(0, 0, position.width - 14, position.height - 14), "", new GUIStyle("preBackground"));

            scrollPos = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height), scrollPos,
                                            new Rect(0, 0, viewSize.x, viewSize.y), true, true);
            SpriteParams.SpriteFrame frame = spriteParams.GetFrame(frameToEdit);
            SpriteParamsEditor.DrawSpriteAndPreview(frame,
                                                    (Sprite)serializedObject.FindProperty("previewSprite").objectReferenceValue,
                                                    GetSpriteAspectSize(frame.sprite), GetSpritePos(frame.sprite), clip: false);
            DoHandle(frame);
            GUI.EndScrollView();

            DrawParamsWindow();

            serializedObject.ApplyModifiedProperties();
        }