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


public class NewQuestionCardController : MonoBehaviour, IPointerClickHandler {

    public GameObject cardBack;
    public GameObject cardFront;

    public GameObject NewQuestionCard;

    bool rotateDone = true;
    bool backClickable = true;

    [SerializeField] GameObject startParent;
    [SerializeField] GameObject gameManager;

    public bool BackClickable { get => backClickable; set => backClickable = value; }

    public void OnPointerClick(PointerEventData eventData) {
        if (cardBack.activeSelf && rotateDone && BackClickable)
        {
            RotateCard();
        }
    }

    public void RotateCard()
    {
        StartCoroutine(Rotate(new Vector3(0, 90f, 0), 0.5f));
        StartCoroutine(secondRotation());
        StartCoroutine(startTimer());
    }

    private IEnumerator startTimer()
    {
        while (!rotateDone) {
            yield return new WaitForSeconds(0.1f);
        }
        GameObject.Find("GameManager").GetComponent<GameManagerScript>().StartTimer();
    }

    public bool getRotationDone() {
        return rotateDone;
    }
    private IEnumerator secondRotation() {
        while (!rotateDone) {
            yield return new WaitForSeconds(0.1f);
        }
        cardBack.SetActive(false);
        cardFront.SetActive(true);
        transform.Rotate(0, 180, 0);
        StartCoroutine(Rotate(new Vector3(0, 90f,0 ), 0.5f));
    }

    private IEnumerator Rotate(Vector3 angle, float duration = 1.0f) {
        rotateDone = false;
        Quaternion from = transform.rotation;
        Quaternion to = Quaternion.Euler(angle) * from;

        float elapsed = 0.0f;
        while (elapsed < duration) {
            transform.rotation = Quaternion.Slerp(from, to, elapsed / duration);
            elapsed += Time.deltaTime;
            yield return null;
        }
        transform.rotation = to;
        rotateDone = true;
    }

    private void ShowBackside() {
        cardBack.SetActive(true);
        cardFront.SetActive(false);
    }

    public void GenerateNewQuestion() {
        KeyValuePair<int, string> SignedInUser = Database.Instance.GetSignedInUser();
        string currentPlayer = GameManagerScript.GetCurrentPlayer();

        resetPosition();
        ShowBackside();
        if (gameManager.GetComponent<GameManagerScript>().GameMode.Equals(Constants.ONLINE)) {
            if (currentPlayer.Equals(SignedInUser.Value, StringComparison.InvariantCultureIgnoreCase)) {
                BackClickable = true;
            } else {
                BackClickable = false;
            }
        } else {
            BackClickable =  true;
        }
        transform.parent.GetComponent<NewQuestionsPanel>().generateNewQuestion(BackClickable);
    }

    private void resetPosition() {
        transform.SetParent(startParent.transform);
        transform.localPosition = Vector3.zero;
    }
}
