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

public class NewQuestionDragController : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{

    public static GameObject itemBeeingDragged;
    public GameObject NewQuestion;
    public GameObject NewQuestionStartParent;
    private static Text answerText;
    private Vector3 startPosition;

    public Text AnswerText { get => answerText; set => answerText = value; }

    private void Start() {
        AnswerText = GameObject.FindGameObjectWithTag("AnswerText").GetComponent<Text>();
    }
    public void OnDrag(PointerEventData eventData)
    {
        NewQuestion.gameObject.transform.position = eventData.position;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        itemBeeingDragged = null;
        GetComponent<CanvasGroup>().blocksRaycasts = true;
        // Kontrollera vart man släpper kortet och skicka upp fråga om position ok
        if (NewQuestionStartParent != transform.parent) {
            NewQuestion.gameObject.transform.position = startPosition;
        }
        if (getAnswerText().text != "???? - ????") { // Om man svarar nej måste man återställa answerText
        answerText.text = "???? - ????";
            // transform.parent.GetComponent<NewQuestionCardController>().GenerateNewQuestion();
        }
    }

    void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
    {
        itemBeeingDragged = gameObject;
        GetComponent<CanvasGroup>().blocksRaycasts = false;
        startPosition = NewQuestion.gameObject.transform.position;
        // Instanciate new object to drag
        // Save current Position on object (to return to)
    }

    public static Text getAnswerText() {
        return answerText;
    }
    public static void setAnswerText(string value) {
        if (NewQuestionDragController.itemBeeingDragged != null) {
            answerText.text = value;
        }
    }
}
