using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;

namespace uTools
{
    [RequireComponent(typeof(Button))]
    [AddComponentMenu("uTools/Tween/Button Scale(uTools)")]
    public class uButtonScale : MonoBehaviour, uIPointHandler
    {
        private Button button;
        private Toggle toggle;

        public RectTransform tweenTarget;
        public Vector3 normalSize = Vector2.one;
        public Vector3 down = new Vector2(0.9f, 0.9f);
        public float downDuration = 0.2f;
        public float upDuration = 0.05f;

        public AnimationCurve downCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
        public AnimationCurve upCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));

        public bool isUseToggle = false;
        public bool ignoreTimeScale = false;
        uTweenScale ts;
        public UnityEvent onPointerDown;
        public UnityEvent onPointerUp;

        // Use this for initialization
        void Start()
        {
            if (tweenTarget == null)
            {
                tweenTarget = GetComponent<RectTransform>();
            }
            if (null == ts)
            {
                ts = tweenTarget.gameObject.AddComponent<uTweenScale>();
                ts.enabled = false;
                ts.ignoreTimeScale = ignoreTimeScale;
            }
            if (isUseToggle)
                toggle = GetComponent<Toggle>();
            else
                button = GetComponent<Button>();
        }

        void OnEnable()
        {
            if (null != ts)
            {
                ts.enabled = false;
            }
            if (null != tweenTarget)
            {
                tweenTarget.localScale = normalSize;
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (!isUseToggle)
            {
                if (button.enabled == false || button.interactable == false)
                    return;
            }
            else
            {
                if (toggle.enabled == false || toggle.interactable == false)
                    return;
            }
            Scale(down, downDuration, downCurve);
            onPointerDown.Invoke();
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            Scale(normalSize, upDuration, upCurve);
            onPointerUp.Invoke();
        }

        public void OnPointerClick(PointerEventData eventData)
        {

        }

        void Scale(Vector3 to, float duration, AnimationCurve curve)
        {
            //ts = uTweenScale.Begin(tweenTarget.gameObject, tweenTarget.localScale, to, duration);
            ts.from = tweenTarget.localScale;
            ts.to = to;
            ts.duration = duration;
            ts.animationCurve = curve;
            ts.ResetToBeginning();
            ts.PlayForward();
        }
    }
}
