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

public class GameControllerScript : MonoBehaviour {

    //public Text pointText;

    public int totalPoint = 0;

    public static GameControllerScript shared;

    public GameObject deathParticles;
    public Transform playerTransform;

    private bool hasDied = false;

    private Transform camTransform;

    private Vector3 originalPosLocal;
    private int maxShakeCount = 10;
    private int shakeCount = 0;
    public float shakePower = 0.3f;

    private void Start()
    {
        //DontDestroyOnLoad(this.gameObject);
        shared = this;
        //playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
        camTransform = GameObject.FindGameObjectWithTag("MainCamera").transform;

        originalPosLocal = camTransform.localPosition;

        int hej = 0;
        hej += 1;
        print(hej);
        float tal = 10;
        float tal2 = 2;
        print(tal*tal2);
        bool test = false;
        if (test != false)
        {
            test = true;
        }
        print(test);

    }

    void Update () {
        //pointText.text = totalPoint.ToString();

        if (Input.GetKeyDown(KeyCode.R))
        {
            Die();
        }
        if (Input.GetKeyDown(KeyCode.I))
        {
            ShakeScreen(0.3f, 30);
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            print("Close program!");
            Application.Quit();
        }
    }
    public void Die()
    {
        if (hasDied == false)
        {
            hasDied = true;
            //Instantiate(deathParticles, playerTransform.position, Quaternion.identity, playerTransform);
            print("To be continued!");
            Invoke("DeathDelay", 5.0f); 
        }
    }
    void DeathDelay()
    {
        print("Delayed death");
        int newScene = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(newScene);
    }

    public void ShakeScreen(float power = 0.3f, int totalShake = 10)
    {
        shakePower = power;
        maxShakeCount = totalShake;
        print("shake it !");
        shakeCount = 0;
        Invoke("Shake", 0.02f);
    }
    void ShakeBack()
    {
        if (shakeCount < maxShakeCount)
        {
            shakeCount++;
            camTransform.localPosition = originalPosLocal;
            Invoke("Shake", 0.02f);
        } else
        {
            camTransform.localPosition = originalPosLocal;
        }
    }
    void Shake()
    {
        if (shakeCount < maxShakeCount)
        {
            shakeCount++;
            camTransform.localPosition += new Vector3(Random.Range(-shakePower, shakePower), Random.Range(-shakePower, shakePower), Random.Range(-shakePower, shakePower));
            Invoke("ShakeBack", 0.02f);
        } else
        {
            camTransform.localPosition = originalPosLocal;
        }
    }


    List<int> itemList = new List<int>();
    string itemString = "";

    void ListToString()
    {
        for (int i = 0; i < itemList.Count; i++)
        {
            itemString += itemList[i];
        }
        print(itemString);
    }

}
