using UnityEngine;
using System.Collections;

public class ClickRepulse : MonoBehaviour
{
    public float MinForce;    // Força da distância maxima
    public float MaxForce;    // Força da distância minima
    public float MinDistance; // distancia até a qual a força é max
    public float MaxDistance; // distancia a partir da qual a força é min
    public float TouchDistance; // distancia até a qual a força é max
    public float XForcePercentage;
    public Transform LivesTranform;
    public Transform cController;
    public Transform Darken;

    public AudioClip[] wallSounds;
    public AudioClip[] touchSounds;

    private AudioSource audioSource;

    private Rigidbody2D rb;
    private Lifes lif;
    private ParticleSystem ps1;
    private ParticleSystem ps2;
    // Use this for initialization
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        ps1 = transform.FindChild("MysticWaves").GetComponent<ParticleSystem>();
        ps2 = transform.FindChild("FallingShine").GetComponent<ParticleSystem>();
        rb = GetComponent<Rigidbody2D>();
        rb.isKinematic = true;
        lif = LivesTranform.GetComponent<Lifes>();
    }

    // Update is called once per frame
    void Update()
    {/*
        Vector2 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        float distance = Vector2.Distance(rb.position, mouse);
        print(distance);*/
        if (Input.GetMouseButtonDown(0))
        {
            if ((lif.getUses() > 0)&&(Darken.GetComponent<DarkControl>().IsInGame()))
            {
                if (GetComponent<Rigidbody2D>().isKinematic == true)
                {
                    ps1.Stop();
                    ps2.Stop();
                    GetComponent<MysticFloat>().stop();
                    GetComponent<Rigidbody2D>().isKinematic = false;
                    cController.GetComponent<ClickController>().StartGame();
                }
                rb.AddForce(calculateForce());
            }
        }
    }

    Vector2 calculateForce()
    {
        Vector2 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        float distance = Vector2.Distance(rb.position, mouse);
        float angle = Mathf.Atan2(rb.position.y - mouse.y, rb.position.x - mouse.x);//Mathf.Deg2Rad * Vector2.Angle(mousepos, rb.position);
        float percentage = (Mathf.Max(MinDistance, Mathf.Min(MaxDistance, distance)) / MaxDistance);

        if (distance < MaxDistance)
        {
            if (distance < TouchDistance)
            {
                rb.velocity = new Vector3(0, 0, 0);
                rb.AddTorque(mouse.x >= rb.position.x ? 2 : -2);
                return new Vector2((MaxForce - percentage * (MaxForce - MinForce)) * Mathf.Cos(angle) * XForcePercentage/5, (MaxForce - percentage * (MaxForce - MinForce)) * Mathf.Sin(angle)/5);
            }
            else
            {
                rb.velocity = new Vector3(0, 0, 0);
            }
        }

        return new Vector2((MaxForce - percentage * (MaxForce - MinForce)) * Mathf.Cos(angle) * XForcePercentage, (MaxForce - percentage * (MaxForce - MinForce)) * Mathf.Sin(angle));
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        //print (col.gameObject.name);
        if (col.gameObject.name == "Wall")
        {
            audioSource.PlayOneShot(wallSounds[Random.Range((int)0, wallSounds.Length)]);
            ps1.Emit(1);
        } else
        {
            audioSource.PlayOneShot(touchSounds[Random.Range((int)0, touchSounds.Length)]);
        }
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.name != "SoundTrigger")
        {
            cController.GetComponent<ClickController>().loseGame();
        }/*
#if UNITY_EDITOR
        // Application.Quit() does not work in the editor so
        // UnityEditor.EditorApplication.isPlaying need to be set to false to end the game
        UnityEditor.EditorApplication.isPlaying = false;
#else
         Application.Quit();
#endif*/
    }
};