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

public class UIManager : MonoBehaviour
{
	public GraphicRaycaster graphicRaycaster;
	public EventSystem eventSystem;

	private PlaceableTypes selectedPlaceable;
	private InteractionTypes selectedInteraction;

	private void Update()
	{
		if (GameManager.PlacementManager == null || GameManager.InteractionManager == null)
		{
			return;
		}

		Vector3 mousePosition = Input.mousePosition;
		Vector3 origin = Camera.main.ScreenToWorldPoint(mousePosition);
		Vector3 direction = Camera.main.transform.forward;
		Ray ray = new Ray(origin, direction);

		PointerEventData pointerEventData = new PointerEventData(eventSystem);
		pointerEventData.position = Input.mousePosition;

		List<RaycastResult> results = new List<RaycastResult>();
		graphicRaycaster.Raycast(pointerEventData, results);

		if (results.Count > 0)
		{
			GameManager.PlacementManager.selectedPlacement = PlaceableTypes.None;
			GameManager.InteractionManager.currentInteraction = InteractionTypes.None;
		}
		else
		{
			GameManager.PlacementManager.selectedPlacement = selectedPlaceable;
			GameManager.InteractionManager.currentInteraction = selectedInteraction;
		}
	}

	public void SelectLaunch()
	{
		GameManager.Ship.LaunchShip();
	}

	public void SelectHarvester()
	{
		selectedPlaceable = PlaceableTypes.Harvester;
		selectedInteraction = InteractionTypes.Place;
	}

	public void SelectFence()
	{
		selectedPlaceable = PlaceableTypes.Fence;
		selectedInteraction = InteractionTypes.Place;
	}

	public void DecreaseRotation()
	{
		GameManager.PlacementManager.placementRotation -= 45;
	}

	public void IncreaseRotation()
	{
		GameManager.PlacementManager.placementRotation += 45;
	}

	public void SelectRepair()
	{
		selectedPlaceable = PlaceableTypes.None;
		selectedInteraction = InteractionTypes.Repair;
	}

	public void SelectReap()
	{
		selectedPlaceable = PlaceableTypes.None;
		selectedInteraction = InteractionTypes.Reap;
	}
	public void SelectReset()
	{
		GameManager.IsQuitting = true;
		SceneManager.LoadScene(SceneManager.GetActiveScene().name);
	}

	public void SelectQuit()
	{
		GameManager.Quit();
	}
}
