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

[RequireComponent(typeof(BoxCollider2D))]
public class ButtonGeneral : MonoBehaviour, IInteragable {

    public bool mScaleDown, mDecreaseIntensity;

    private float mPercentScale = 1, mPercentIntensity = 1, mPercentExceedCollider = 1.1f;
    private BoxCollider2D mBoxCollider;
    private bool mPressed;
    private Color mTextColor;

    public UnityEvent mOnClick;

    void Start()
    {
        mBoxCollider = GetComponent<BoxCollider2D>();
        mBoxCollider.size *= mPercentExceedCollider;
        if (mScaleDown)
        {
            mPercentScale = 0.95f;
        }
        if (mDecreaseIntensity)
        {
            mPercentIntensity = 0.4f;
        }

        if (GetComponentInChildren<Text>())
        {
            mTextColor = GetComponentInChildren<Text>().color;
        }
    }

    public void BtPressed()
    {
        if (!mPressed)
        {
            foreach (Transform c in GetComponentsInChildren<Transform>())
            {
                if (c.gameObject.transform.childCount == 0)
                {
                    c.localScale *= mPercentScale;
                    if (c.GetComponent<Image>())
                    {
                        c.GetComponent<Image>().color = new Color(1 * mPercentIntensity, 1 * mPercentIntensity, 1 * mPercentIntensity);
                    }
                    else if (c.GetComponent<Text>())
                    {
                        c.GetComponent<Text>().color = new Color(mTextColor.r * mPercentIntensity, mTextColor.g * mPercentIntensity, mTextColor.b * mPercentIntensity);
                    }
                }
            }
            mPressed = true;
        }
    }

    public void BtPressing()
    {
        if (!mPressed)
        {
            foreach (Transform c in GetComponentsInChildren<Transform>())
            {
                if (c.gameObject.transform.childCount == 0)
                {
                    c.localScale *= mPercentScale;
                    if (c.GetComponent<Image>())
                    {
                        c.GetComponent<Image>().color = new Color(1 * mPercentIntensity, 1 * mPercentIntensity, 1 * mPercentIntensity);
                    }
                    else if (c.GetComponent<Text>())
                    {
                        c.GetComponent<Text>().color = new Color(mTextColor.r * mPercentIntensity, mTextColor.g * mPercentIntensity, mTextColor.b * mPercentIntensity);
                    }
                }
            }
            mPressed = true;
        }
    }

    public void BtReleased()
    {
        if (mPressed)
        {
            foreach (Transform c in GetComponentsInChildren<Transform>())
            {
                if (c.gameObject.transform.childCount == 0)
                {
                    c.localScale /= mPercentScale;
                    if (c.GetComponent<Image>())
                    {
                        c.GetComponent<Image>().color = new Color(1, 1, 1);
                    }
                    else if (c.GetComponent<Text>())
                    {
                        c.GetComponent<Text>().color = mTextColor;
                    }
                }
            }
            mPressed = false;
            mOnClick.Invoke();
        }
    }

    public void BtOutArea()
    {
        if (mPressed)
        {
            foreach (Transform c in GetComponentsInChildren<Transform>())
            {
                if (c.gameObject.transform.childCount == 0)
                {
                    c.localScale /= mPercentScale;
                    if (c.GetComponent<Image>())
                    {
                        c.GetComponent<Image>().color = new Color(1, 1, 1);
                    }
                    else if (c.GetComponent<Text>())
                    {
                        c.GetComponent<Text>().color = mTextColor;
                    }
                }
            }
            mPressed = false;
        }
    }
}
