using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Main : MonoBehaviour
{
    public List<GameObject> employees = new List<GameObject>();

    public Camera camera;

    public Vector3 targetCameraPosition;
    public Quaternion targetCameraRotation;
    public float maxCameraRange;

    InteractableHandler interact;
    TimerHandler timer;
    public MainMenuHandler main;
    public AudioHandler audioHandle;
    public MenuHandler menu;

    public GameObject textCardPrefab;
    public Transform typingPanel;
    public int distanceToReact = 5;

    // UI VARIABLES
    public Image tooltipBackground;
    public Text timerText;
    public Image rightClickMenuBackground;

    public Vector3 prevMousePosition = new Vector3();
    bool pan = false;
    [Range(0.0F, 1.0F)]
    public float panSpeed;

    void Awake()
    {
        Application.targetFrameRate = 60;
    }

    void Update()
    {
        if (main.gameStarted)
        {
            interact = new InteractableHandler(camera, tooltipBackground, rightClickMenuBackground, menu);
            timer = new TimerHandler(300F, timerText);
            main.gameStarted = false;
        }

        if (!main.inMenu) {
            if (Input.GetMouseButtonDown(1) && interact.CheckIfInteractable(camera.ScreenPointToRay(Input.mousePosition), Mathf.Infinity))
            {
                interact.rightClickMenu = true;
                interact.PlaceRightClickMenu();
            }

            if ((Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(0)) && menu.clicked)
            {
                interact.rightClickMenu = false;
            }


            if (Input.GetMouseButtonDown(0))
            {
                pan = true;
                prevMousePosition = Input.mousePosition;
            }


            InterpretMessage(menu.message);


            if (Input.GetMouseButton(0) && pan)
            {
                Vector3 mousePosDifference = -Input.mousePosition + prevMousePosition;
                Vector3 currentPos = camera.transform.position;
                camera.transform.position += (camera.transform.right * mousePosDifference.x + camera.transform.up * mousePosDifference.y) * panSpeed;

                if (Vector3.Distance(camera.transform.position, targetCameraPosition) > maxCameraRange)
                    camera.transform.position = currentPos;

                prevMousePosition = Input.mousePosition;
            }

            if (Input.GetMouseButtonUp(0))
            {
                pan = false;
            }

            float scrollScale = Input.mouseScrollDelta.y * -0.3F;
            camera.orthographicSize += scrollScale;

            if (camera.orthographicSize > 8)
                camera.orthographicSize = 8;
            if (camera.orthographicSize < 5)
                camera.orthographicSize = 5;

            interact.Update();
            timer.Update();
        }


        
    }


    private void InterpretMessage(string message)
    {
        if (message == "Coffee Maker: Just Water")
        {
            foreach(GameObject emp in employees)
            {
                if (Vector3.Distance(GameObject.Find("Coffee_Maker").transform.position, emp.transform.position) < distanceToReact)
                {
                    emp.GetComponent<Employee>().ChangeGoal(GameObject.Find("Coffee_Maker").transform);
                    emp.GetComponent<Employee>().AddTaskSeconds(10);
                }
            }
        }
    }

}
