using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class Player : MonoBehaviour
{
    float speed = 50;
    int mode = 1;
    public GameObject inner, outer, pulsar;
    private Pulsar puls;
    public bool controlsActive = false;

    void Start()
    {
        puls = pulsar.GetComponent<Pulsar>();
    }

    void Update()
    {
        puls.setOutward(mode != 0);

        inner.transform.localRotation = Quaternion.Euler(0, Time.realtimeSinceStartup * 90, 0);
        outer.transform.localRotation = Quaternion.Euler(0, -Time.realtimeSinceStartup * 90, 0);

        if (controlsActive)
        {
            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
            {
                gameObject.transform.position += new Vector3(-speed * Time.deltaTime, 0f, 0f);
            }
            if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            {
                gameObject.transform.position += new Vector3(speed * Time.deltaTime, 0f, 0f);
            }
            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
            {
                gameObject.transform.position += new Vector3(0, speed * Time.deltaTime, 0f);
            }
            if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            {
                gameObject.transform.position += new Vector3(0f, -speed * Time.deltaTime, 0f);
            }

            if (Input.GetKey(KeyCode.Alpha1) || Input.GetButton("Fire1")) mode = 1;
            if (Input.GetKey(KeyCode.Alpha2) || Input.GetButton("Fire2")) mode = 0;
        }
    }

    void OnTriggerEnter(Collider other)
    {
        Particle obj = other.gameObject.GetComponent<Particle>();
    }

    void OnTriggerStay(Collider other)
    {
        if (other == null) return;

        Particle obj = other.gameObject.GetComponent<Particle>();

        if (obj == null) return;

        // attract
        if (mode == 0)
        {
            Vector3 attractPt = gameObject.transform.localPosition;
            obj.frc = (attractPt - other.gameObject.transform.localPosition); // we get the attraction force/vector by looking at the mouse pos relative to our pos
           // obj.frc.Normalize(); //by normalizing we disregard how close the particle is to the attraction point 

            float dist = Vector3.Distance(attractPt, other.gameObject.transform.localPosition);
           
            //other.rigidbody.velocity *= obj.drag; //apply drag
//             if (dist > 15)
//             {
//                 obj.frc.Normalize();
//                 other.rigidbody.velocity += obj.frc * (dist * 0.5f); //apply force
//             }
            if (dist < 12)
            {
                obj.frc.Normalize();
                other.rigidbody.velocity += -obj.frc * 0.2f;

                //other.rigidbody.velocity += obj.frc * .005f; //apply force

//                 obj.frc.x += Mathf.PerlinNoise(obj.uniqueVal, obj.uniqueVal);
//                 obj.frc.y += Mathf.PerlinNoise(obj.uniqueVal, obj.uniqueVal);
                //                 other.rigidbody.velocity += obj.frc * 0.5f;
            }
            else
            {
                //obj.frc.Normalize();
                obj.frc.x += Mathf.PerlinNoise(obj.uniqueVal, obj.uniqueVal);
                obj.frc.y += Mathf.PerlinNoise(obj.uniqueVal, obj.uniqueVal);
                other.rigidbody.velocity += obj.frc * (dist * 0.3f); //apply force
            }
        }
        else
        {
            // repel
            Vector3 attractPt = gameObject.transform.localPosition;
            obj.frc = attractPt - other.gameObject.transform.localPosition;
            obj.frc.Normalize();

            //let get the distance and only repel points close player
            float dist = Vector3.Distance(attractPt, other.gameObject.transform.localPosition);

            other.rigidbody.velocity *= obj.drag;
            if (dist < 20)
            {
                other.rigidbody.velocity += -obj.frc * 11.6f;
            }
            else
            {		
                obj.frc.x = Mathf.PerlinNoise(obj.uniqueVal, other.gameObject.transform.position.y * 0.01f);
                obj.frc.y = Mathf.PerlinNoise(obj.uniqueVal, other.gameObject.transform.position.x * 0.01f);
                other.rigidbody.velocity += obj.frc * 0.4f;
            }
        }

    }

    public void HardSetPosition(Vector3 pos, Vector3 dir)
    {
        transform.position = pos;
    }
}