using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.SceneManagement;

public class player : MonoBehaviour
{
    [SerializeField] private GameObject mündiEffekt;

    public Transform mündiMinekuAsukoht;

    private bool lendab;

    public ForceMode forcemode;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.rotation = Quaternion.Euler(0, 360, 0);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.rotation = Quaternion.Euler(0, 180, 0);
        }

        if (Input.GetKeyDown(KeyCode.Space) & !lendab)
        {
            Sounds.PlaySound(0);
            GetComponent<Rigidbody>().AddForce(new Vector3(0, 2000), forcemode);
            lendab = true;
            //transform.position += new Vector3(0, 20) * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.D))
        {
            transform.position += new Vector3(20, 0) * Time.deltaTime;
            transform.rotation = Quaternion.Euler(0, 90, 0);

        }

        if (Input.GetKey(KeyCode.A))
        {
            transform.position += new Vector3(-20, 0) * Time.deltaTime;
            transform.rotation = Quaternion.Euler(0, 270, 0);

        }
    }
    private bool kasLendab() {
        if (gameObject.transform.position.y <= 1143.05)
            return lendab = false;
        else
            return lendab = true;
        
           
       
    }
    public void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            lendab = false;
           
        }
        Debug.Log(collision);
    }

    public void OnTriggerEnter(Collider other)
    {
        if (other.tag == "kast")
        {
            Sounds.PlaySound(1);
            var targetFollower = GetComponent<TargetFollower>();
            targetFollower.kaesolevAsi = other.gameObject.transform;
            targetFollower.kaesolevAsi.DOScale(Vector3.one * 0.4f, 1f);
            targetFollower.kaesolevAsi.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
        }
        else if (other.tag == "npc")
        {
            var targetFollower = GetComponent<TargetFollower>();
            if (targetFollower.kaesolevAsi == null) return;
            var müntideArv = targetFollower.kaesolevAsi.GetComponent<Kast>().müntideArv;
            //FindObjectOfType<MüntideLugeja>().AnnaMünte(müntideArv);
            Destroy(targetFollower.kaesolevAsi.gameObject);

            for (var i = 0; i < müntideArv; i++)
            {
                var münt = Instantiate(mündiEffekt, transform.position, Quaternion.identity);
                //münt.transform.localScale = Vector3.zero;
                var seq = DOTween.Sequence();
                seq.Append(münt.transform.DOMove(mündiMinekuAsukoht.position, 0.3f + (i * 0.1f)).SetEase(Ease.InOutQuad));
                var tf = münt.AddComponent<TargetFollower>();
                //tf.kiirus = 2f + i * (1f);
                //tf.kasi = mündiMinekuAsukoht;
                //tf.kaesolevAsi = münt.transform;
                //seq.Append(münt.transform.DOScale(Vector3.one, i).SetEase(Ease.InOutQuad));
                seq.Append(münt.transform.DOShakeRotation(0.2f).SetEase(Ease.InOutQuad).OnStart(() => {
                    tf.kiirus = 10f;
                    tf.kasi = mündiMinekuAsukoht;
                    tf.kaesolevAsi = münt.transform;
                }));
                seq.OnComplete(() => { Destroy(münt); FindObjectOfType<MüntideLugeja>().AnnaMünte(1); Sounds.PlaySound(1); });
            }
        }
        else if (other.tag == "win")
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene(3);
        }
    }

    public void Surm()
    {
        FindObjectOfType<MüntideLugeja>().Nulli();
        SceneManager.LoadScene(2);
    }

}
