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

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

    public Transform parent = null;
    public Transform placeholderParent = null;
    public GameObject placeholder = null;

    public void OnDrag(PointerEventData eventData) {
        if (!eventData.pointerDrag.gameObject.name.Contains("NewQuestion")) {
            return;
        }
        this.transform.position = eventData.position;

        if (placeholder.transform.parent != placeholderParent) {
            placeholder.transform.SetParent(placeholderParent);
        }

        int newSiblingIndex = placeholderParent.childCount;
        for (int i = 0; i < placeholderParent.childCount; i++) {
            if (this.transform.position.x < placeholderParent.GetChild(i).position.x) {
                newSiblingIndex = i;
                if (placeholder.transform.GetSiblingIndex() < newSiblingIndex) {
                    newSiblingIndex--;
                }
                break;
            }
        }

        if (placeholderParent.gameObject.name.Contains("AnswerLine")) {
            int cardBefore = newSiblingIndex - 1;
            int cardAfter = newSiblingIndex + 1;

            string before = LocalizationManager.Instance.GetText("BEFORE");
            string after = LocalizationManager.Instance.GetText("AFTER");

            if (placeholder.transform.GetSiblingIndex() == cardAfter) {
                cardAfter++;
            }
            QuestionCard questionCardBefore = null;
            QuestionCard questionCardAfter = null;
            string answerTextBefore = "";
            string answerTextAfter = "";
            if (cardBefore < 0) {
                answerTextBefore = before;
            } else {
                questionCardBefore = placeholderParent.GetChild(cardBefore).GetComponent<QuestionCard>();
                if (questionCardBefore == null) {
                    if (cardBefore - 1 >= 0) {
                        questionCardBefore = placeholderParent.GetChild(cardBefore - 1).GetComponent<QuestionCard>();
                    }
                }
                if (questionCardBefore != null) {
                    answerTextBefore = questionCardBefore.GetAnswerText().text;
                } else {
                    answerTextBefore = before;
                }
            }

            if (cardAfter >= placeholderParent.childCount) {
                answerTextAfter = after;
            } else {
                questionCardAfter = placeholderParent.GetChild(cardAfter).GetComponent<QuestionCard>();
                answerTextAfter += questionCardAfter.GetAnswerText().text;
            }

            if (answerTextAfter.Equals(after)) {
                this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextAfter + " " + answerTextBefore;
            } else if (answerTextBefore.Equals(before)) { 
               this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextBefore + " " + answerTextAfter;
            } else { 
                this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextBefore + " - " + answerTextAfter;
            }
        }
        placeholder.transform.SetSiblingIndex(newSiblingIndex);
    }

    public void OnBeginDrag(PointerEventData eventData) {

        Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
        d.GetComponent<CanvasGroup>().alpha = 0.8f;
        placeholder = new GameObject();
        placeholder.transform.SetParent(this.transform.parent);
        LayoutElement le = placeholder.AddComponent<LayoutElement>();
        le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
        le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
        le.flexibleHeight = this.GetComponent<LayoutElement>().flexibleHeight;
        le.flexibleWidth = this.GetComponent<LayoutElement>().flexibleWidth;
        le.minHeight = this.GetComponent<LayoutElement>().minHeight;
        le.minWidth = this.GetComponent<LayoutElement>().minWidth;
        
        placeholder.transform.SetSiblingIndex(this.transform.GetSiblingIndex());

        parent = this.transform.parent;
        placeholderParent = parent;
        this.transform.SetParent(this.transform.parent);

        GetComponent<CanvasGroup>().blocksRaycasts = false;
    }

    public void OnEndDrag(PointerEventData eventData) {
        Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
        d.GetComponent<CanvasGroup>().alpha = 1f;
        this.transform.SetParent(parent);
        this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
        GetComponent<CanvasGroup>().blocksRaycasts = true;

        Destroy(placeholder);
    }
}
