﻿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 screenShakePower = 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 () {

        if (pointText != null)
        {
            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;
            if (deathParticles != null)
            {
                Instantiate(deathParticles, playerTransform.position, Quaternion.identity, playerTransform); 
            }
            print("To be continued!");
            Invoke("DeathDelay", 0.5f);
            ShakeScreen();
        }
    }
    void DeathDelay()
    {
        //print("Delayed death");
        int newScene = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(newScene);
    }

    public void ShakeScreen(float power = 0.3f, int totalShake = 10)
    {
        screenShakePower = 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(-screenShakePower, screenShakePower), Random.Range(-screenShakePower, screenShakePower), Random.Range(-screenShakePower, screenShakePower));
            Invoke("ShakeBack", 0.02f);
        } else
        {
            camTransform.localPosition = originalPosLocal;
        }
    }
}
