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

public class PlayerController : MonoBehaviour
{
    [Header("Game manager")]
    public GameManager gm;
    [Header("Camera pan")]
    public Camera mainCamera;
    public float panspeed = 4F;
    public float screenEdge = 0.95F;

    [Header("Camera focus")]
    public bool isFocused = false;
    public GameObject focusTarget;
    public Vector2 focusCenter;

    [Header("Camera zoom")]
    public float minZoom;
    public float maxZoom;
    public float speed;
    private float cZoom;
    private Vector3 firstPose;

    [Header("Player and Movement")]
    public GameObject player;
    public GameObject moveableBoothIndicator;
    public BioBubble biobubble;

    [Header("Controls")]
    public string mouseClickButton;
    public string returnToFocusButton;


    private Player playerScript;
    private bool dragging = false;


    // Use this for initialization
    void Start ()
    {
        cZoom = mainCamera.orthographicSize;
	}
	
	// Update is called once per frame
	void Update ()
    {
        CameraClickAndDrag();

        if (!dragging && !playerScript.isMoving)
        {
            ClickOnElement();
            if (Input.GetButtonDown("Jump"))
            {
                RevertCharacter();
            }
        }

        if (isFocused)
        {
            CameraFocus();
        }


    }

    public void InitPlayerCharacter(GameObject player)
    {
        this.player = player;
        playerScript = player.GetComponent<Player>();
        playerScript.firstBooth = gm.entrance;
        playerScript._path = new Booth[gm.numberOfTurns];
    }
//===========Camera Functionality===========//
    void CameraClickAndDrag()
    {
		if (Input.GetMouseButtonDown(0))
        {
            firstPose = mainCamera.ScreenToWorldPoint(Input.mousePosition);
        }
        if (Input.GetMouseButton(0))
        {
            Vector3 Second = mainCamera.ScreenToWorldPoint(Input.mousePosition);
            mainCamera.transform.position += firstPose - Second;
            firstPose = mainCamera.ScreenToWorldPoint(Input.mousePosition);
        }

        //Zoom
        cZoom += -Input.GetAxis("Mouse ScrollWheel") * speed;

        cZoom = Mathf.Clamp(cZoom, minZoom, maxZoom);

        mainCamera.orthographicSize = cZoom;
    }
    void CameraFocus()
    {
        Vector3 foctgt = new Vector3(focusTarget.transform.position.x + focusCenter.x, mainCamera.gameObject.transform.position.y, focusTarget.transform.position.z + focusCenter.y);
        mainCamera.gameObject.transform.position = foctgt;
    }
    void ClickOnElement()
    {
        //primary click
        if (Input.GetButtonDown(mouseClickButton))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                Npc npc = hit.transform.gameObject.GetComponent<Npc>();
                Booth booth = hit.transform.gameObject.GetComponent<Booth>();
                //if we click on a person or booth
                if (npc != null || booth != null)
                {
                    //if we click on a person or booth
                    //isFocused = true;
                    //focusTarget = hit.transform.gameObject;
                }
                else if (hit.transform.gameObject.GetComponent<ArrowBoothHolder>() != null)
                {
                    //if we clicked on an arrow
                    MoveCharacter(hit.transform.gameObject.GetComponent<ArrowBoothHolder>().representing);
                }

                if (npc != null)
                {
                    biobubble.ShowBio(npc);
                }
                Debug.Log(hit.transform.gameObject);
            }
        }
        
        //return to focus
        if (Input.GetButtonDown(returnToFocusButton) && focusTarget != null)
        {
            isFocused = true;
        }
    }

    void MoveCharacter(Booth newBooth)
    {
        if(gm.stepForward())
        {
            playerScript.move(newBooth.GetComponent<Booth>());
        }
        
        
    }
    public void RevertCharacter()
    {
        if(gm.stepBack())
        {
            playerScript.move(playerScript._path[Player.currTime]);
        }
    }
//==========Movement Functionality=========//
}
