﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Kompanion : MonoBehaviour
{
    public float speedFactor = 1.0f;
    public float jumpFactor = 1000.0f;
    public GameObject floor = null;
    public GameObject obmocjeAlarma, obmocjeAlarma2;
    public GameObject alarmSound;
    public GameObject izstrelek;

    private Rigidbody rb = null;
    private bool jumpAllowed = false;
    private Light luc, luc2;
    private AudioSource alarmAudioSource;
    private AudioSource footStepsSource;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        luc = GameObject.Find("LucAlarma").GetComponent<Light>();
        luc2 = GameObject.Find("LucAlarma2").GetComponent<Light>();
        alarmAudioSource = alarmSound.GetComponent<AudioSource>();
        footStepsSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            //transform.Translate(0, 0, speedFactor * 10.0f * Time.deltaTime);
            rb.AddForce(0.0f, 0.0f, speedFactor * Time.deltaTime);
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            footStepsSource.Play();
        }
        if (Input.GetKeyUp(KeyCode.W))
        {
            footStepsSource.Pause();
        }
        if (Input.GetKey(KeyCode.S))
        {
            //transform.Translate(0, 0, speedFactor  * - 10.0f * Time.deltaTime);
            rb.AddForce(0.0f, 0.0f, -speedFactor * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.A))
        {
            //rb.AddTorque(0.0f, -speedFactor * Time.deltaTime, 0.0f);
            rb.AddForce(-speedFactor * Time.deltaTime, 0.0f, 0.0f);
            //transform.RotateAroundLocal(this.transform.up, -0.01f);

        }
        if (Input.GetKey(KeyCode.D))
        {
            //rb.AddTorque(0.0f, speedFactor * Time.deltaTime, 0.0f);
            rb.AddForce(speedFactor * Time.deltaTime, 0.0f, 0.0f);
            //transform.RotateAroundLocal(this.transform.up, 0.01f);
        }
        if (Input.GetKeyDown(KeyCode.Space) && jumpAllowed)
        {
            rb.AddForce(0.0f, jumpFactor * Time.deltaTime, 0.0f);
        }

        if (Input.GetKeyDown(KeyCode.F))
        {
            var muca = Instantiate(izstrelek, transform.position + Vector3.forward * 3.0f, Quaternion.identity);
            muca.GetComponent<Rigidbody>().AddForce(Vector3.forward * 1000.0f);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        //Debug.Log(collision.gameObject.name);
        if (collision.gameObject == floor)
            jumpAllowed = true;
    }

    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject == floor)
            jumpAllowed = false;
    }

    private void OnTriggerEnter(Collider other)
    {
        //Debug.Log("Trigger Enter");
        if (other.gameObject == obmocjeAlarma)
        {
            luc.intensity = 20.0f;
            alarmAudioSource.Play();
        }
        if (other.gameObject == obmocjeAlarma2)
            luc2.intensity = 20.0f;
    }

    private void OnTriggerStay(Collider other)
    {
        //Debug.Log("Trigger Stay");
        if (other.gameObject == obmocjeAlarma)
            luc.intensity = 20.0f * Mathf.Pow(Mathf.Sin(Time.time * 10.0f), 2);
        if (other.gameObject == obmocjeAlarma2)
            luc2.intensity = 20.0f * Mathf.Pow(Mathf.Sin(Time.time * 10.0f), 2);
    }

    private void OnTriggerExit(Collider other)
    {
        //Debug.Log("Trigger Exit");
        if (other.gameObject == obmocjeAlarma)
        {
            luc.intensity = 0.0f;
            alarmAudioSource.Pause();
        }
        if (other.gameObject == obmocjeAlarma2)
            luc2.intensity = 0.0f;
    }
}
