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

public class ControllerManager : MonoBehaviour
{
    public static ControllerManager instance;
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(this);
        }
    }

    [SerializeField]
    float p_myMovementSpeed;
    [SerializeField]
    float p_myClampY;

    GameObject p_myCamera;

    float p_myLastMousePositionX;
    float p_myMousePositionX;
    float p_myLastMousePositionY;
    float p_myMousePositionY;

    Power p_myActivatedPower;

    int p_myClickNumber;
    int p_myCount;

    bool inProgress;

    public void setPower(Power power)
    {
        p_myActivatedPower = power;
        p_myClickNumber = power.getClickNumber();
        p_myCount = 0;
    }

    void Start ()
    {
        inProgress = false;
        p_myCamera = GameObject.FindGameObjectWithTag("MainCamera").transform.parent.gameObject;
	}

	void Update ()
    {
        p_myLastMousePositionX = p_myMousePositionX;
        p_myLastMousePositionY = p_myMousePositionY;
        p_myMousePositionX = Input.mousePosition.x;
        p_myMousePositionY = Input.mousePosition.y;
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ScreenShakeManager.instance.ScreenShake();
        }

        if (Input.GetMouseButton(1))
        {
            ChangeCameraPosition();
        }
        else if(!inProgress && p_myActivatedPower != null)
        {
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;
                Vector3 pos = Input.mousePosition;
                pos.z = 10;
                pos = Camera.main.ScreenToWorldPoint(pos);
                if (Physics.Raycast(pos, p_myCamera.transform.forward, out hit))
                {
                    if (hit.collider.tag == "Earth")
                    {
                        p_myCount++;
                        if (p_myCount >= p_myClickNumber)
                        {
                            inProgress = true;
                            p_myActivatedPower.PowerEffect(hit.point);
                            StartCoroutine(UnlockCamera());
                        }
                    }
                }
            }
        }
    }

    IEnumerator UnlockCamera()
    {
        yield return new WaitForSeconds(0.5f);
        //p_myActivatedPower = null;
        inProgress = false;
    }

    void ChangeCameraPosition()
    {
        float distanceX = (p_myMousePositionX - p_myLastMousePositionX) * p_myMovementSpeed * Time.deltaTime;
        float distanceY = (p_myLastMousePositionY - p_myMousePositionY) * p_myMovementSpeed * Time.deltaTime;
        Vector3 newRot = p_myCamera.transform.eulerAngles + new Vector3(distanceY, distanceX, 0);
        float distanceHi = Mathf.Abs(-90 - newRot.x);
        float distanceLo = Mathf.Abs(270 - newRot.x);
        if (distanceLo < distanceHi)
        {
            newRot.x -= 360;
        }
        newRot.x = Mathf.Clamp(newRot.x, -p_myClampY, p_myClampY);
        p_myCamera.transform.eulerAngles = newRot;
    }
}