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

public class WalkingCrew : MonoBehaviour {
//    public PlayerState PState;

//    public GameObject ShipPrefab;
//    public enum PlayerState
//    {
//        Normal
//    }
//    public bool CanFire
//    {
//        get { return Time.time - LastFireTime > TimeBetweenFire; }
//    }

//    public bool IsCharging
//    {
//        get; private set;
//    }
//    private float StartChargeTime = 0;
//    public float MaxChargeTime;
    private GameObject ReticleRenderer;

//    private float LastFireTime = -5;
//    private float TimeBetweenFire = 10;

//    public int PlayerNum
//    {
//        get
//        {
//            Controller c = this.GetComponent<Controller>();
//            if (c) { return c.playernum; }
//            else { return -1; }
//        }
//    }

//    public float CurrentCharge
//    {
//        get
//        {
//            return Mathf.Clamp(Time.time - this.StartChargeTime, 0, MaxChargeTime) / MaxChargeTime;
//        }
//    }

//    public float charge;
    public float FiringAngle;

//    private Vector3 GetFiringAngle()
//    {
        // return Camera.main.ScreenToWorldPoint(Input.mousePosition) - this.transform.position;

//        Controller c = this.GetComponent<Controller>();
//        if (c)
//        if (true)
//        {
//            float xaxis = c.h_axis;
//            float yaxis = c.v_axis;
//            Vector3 v = new Vector3(xaxis, yaxis, 0);
//            Vector3 v = new Vector3(-1,-1, 0);
//            return v.normalized;
//        }
//        else { return Vector3.zero; }
//    }
//    public Controller controller
//    {
//        get
//        {
//            return this.GetComponent<Controller>();
//        }
//    }

    void Start()
    {
        StartCoroutine(Wander());
    }
   
    IEnumerator Wander()
    {
        this.FiringAngle -= (Random.Range(-2f, +2f));
//        yield return new WaitForSeconds(Random.Range(3.0f, 6.0f));
        yield return new WaitForSeconds(3.0f);

    }

    void Awake()
    {
        foreach(Transform sr in this.GetComponentsInChildren<Transform>())
        {
            if (sr.name == "CrewContainer")
            {
                this.ReticleRenderer = sr.gameObject;
            }
        }
    }
    
//    private void OnTriggerEnter2D(Collider2D collision)
//    {
//    }
//    private void OnTriggerExit2D(Collider2D collision)
//    {
//    }
	
	// Update is called once per frame
	void FixedUpdate () {
        //        charge = CurrentCharge;

        //        Controller c = this.GetComponent<Controller>();

        //        this.FiringAngle -= c.h_axis*2f;
        //        this.FiringAngle -= -1*2f;

//        if (this.CanFire)
//        {
//            this.FiringAngle -= (Random.Range(-2f, +2f));
//        }

//        if (this.IsCharging)
//        {
//            if (!c.a_button)
//            {
                // Fire!
//                this.IsCharging = false;
//                float firePower = CurrentCharge;

//                GameObject ship = Instantiate(ShipPrefab);
//                OrbitController oc = ship.GetComponent<OrbitController>();
//                oc.percentPower = CurrentCharge;
//                oc.tiltAngle = this.FiringAngle;
//                oc.Owner = this;
//                this.LastFireTime = Time.time;
//            }
//        }
//        else if (!this.IsCharging && this.CanFire && c.a_button)
//        {
//            // Start Charge
//            this.IsCharging = true;
//            this.StartChargeTime = Time.time;
//        }

        //if(c)
        //{
        //    if (c.a_button && this.CanFire)
        //    {
                
        //    }

        //    if (!this.CanFire)
        //    {
        //        this.IsCharging = false;
        //    }

        //    if (this.CanFire && c.a_button)
        //    {
        //        //CanFire cf = this.GetComponent<CanFire>();
        //        if (!this.IsCharging)
        //        {
        //            this.IsCharging = true;
        //            this.StartChargeTime = Time.time;
        //        }
        //    }
        //    else
        //    {
        //        if (this.IsCharging)
        //        {
        //            this.IsCharging = false;
        //            float firePower = CurrentCharge;

        //            Instantiate(ShipPrefab);
        //            this.LastFireTime = Time.time;

        //            //CanFire cf = this.GetComponent<CanFire>();
        //            //if (cf)
        //            //{
        //            //    cf.Fire(GetFiringAngle() * firePower);
        //            //}
        //        }
        //    }

        //    //if(this.IsCharging)
        //    //{
        //    //    c.Vibrate(Controller.MediumVibrationFactor, Controller.MediumVibrationFactor);
        //    //}
        //}
    }

    float VectorToAngle(Vector2 vec)
    {
        return Mathf.Atan2(vec.y, vec.x) * 180f / Mathf.PI;
    }

    public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
    {
        return Quaternion.Euler(angles) * (point - pivot) + pivot;
    }

    void LateUpdate()
    {
        if (this.ReticleRenderer)
        {
            //float ang = this.VectorToAngle(this.GetFiringAngle());
            //Vector2 a = this.GetFiringAngle();
            //this.ReticleRenderer.GetComponentInChildren<SpriteRenderer>().gameObject.SetActive(a.magnitude >= .001f);

            //float ang = Vector2.Angle(Vector2.up, a);
            //Vector3 cross = Vector3.Cross(Vector2.up, a);

            //if (cross.z > 0)
            //{
            //    ang = 360 - ang;
            //}

            //Vector3 currentRotation = this.ReticleRenderer.transform.localEulerAngles;
            //currentRotation.z = ang - 90;
            //this.ReticleRenderer.transform.rotation = RotatePointAroundPivot(
            //    new Vector3(0, 3, 0),
            //    Vector3.zero,
            //    currentRotation);//Quaternion.AngleAxis(ang, -1*Vector3.forward);

            float ang = this.FiringAngle;
            this.ReticleRenderer.transform.rotation = Quaternion.AngleAxis(-1*ang+90, -1 * Vector3.forward * 3);
        }

    }
}
