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

public class playerctrl : MonoBehaviour {

    public int health;
    public int maxHealth;

    public float moveSpeed = 10f;
    public Material mat;
    public string Name;
    public GameObject PlayerRot;
    public Transform player;
    public GameObject sonic;
    public float ShootForce = 0f;
    public GameObject Gun;
    public float SonicTimer;
    public float SonicTimer_CD;
    public bool SonicCD = false;

    public int t_talk;
    public int t_maxtalk = 200;
    public bool t_bool = true;

    public float sensitivity = 2000;
    public float loudness = 0;
    private AudioSource _audio;
    public FFTWindow fftWindow;

    public int audioSampleRate = 44100;
    public float minThreshold = 0;
    public float frequency;
    public AudioSource audioSource;
    public string microphone;
    public List<string> options = new List<string>();

    private Rigidbody rb;

    private void Awake () {
        _audio = GetComponent<AudioSource>();
        health = maxHealth;
        rb = this.GetComponent<Rigidbody>();
    }

    void Start () {
        foreach (string device in Microphone.devices) {
            if (microphone == null) {
                microphone = device;
            }
            options.Add(device);
        }
        SonicTimer = 0;
        _audio.clip = Microphone.Start(null, true, 100, audioSampleRate);
        _audio.mute = false;
        while (!(Microphone.GetPosition(null) > 0)) {
        }
        _audio.Play();
    }

    void Update () {
        /*PlayerAnim.SetBool ("Run", Run);
		PlayerAnim.SetBool ("Idel", Idel);
		Idel = true;
		Run = false;*/
        volume();
        if (SonicTimer >= SonicTimer_CD) {
            SonicCD = false;
        }
        move();
        Sonic();
        loudness = GetMicVolume() * sensitivity;
        t_talk++;
        SonicTimer += Time.deltaTime;
        if ((loudness >= 2f) && (loudness <= 12f) && (t_bool = true)) {
            float normalizedValue = (loudness - 2f) / 10f;
            if (SonicCD == false) {
                SonicTimer = 0;
                SonicCD = true;
                GameObject obj = Instantiate(GameManager.Instance.GetSenseSonic().gameObject, transform.position, transform.rotation) as GameObject;
                obj.GetComponent<Sonic>().Launch(obj.transform.forward, GameManager.Instance.GetNormalizedPower(normalizedValue), GameManager.Instance.GetNormalizedDuration(normalizedValue));
                if (SonicTimer >= SonicTimer_CD) {
                    SonicCD = false;
                }
            }
        }
        else if ((loudness > 12f) && (loudness <= 500f) && (t_bool = true)) {
            if (SonicCD == false) {
                SonicTimer = 0;
                SonicCD = true;
                GameObject obj = Instantiate(GameManager.Instance.GetAttackSonic().gameObject, transform.position, transform.rotation) as GameObject;
                obj.GetComponent<Sonic>().Launch(obj.transform.forward, GameManager.Instance.GetNormalizedPower(1f), GameManager.Instance.GetNormalizedDuration(1f));
                if (SonicTimer >= SonicTimer_CD) {
                    SonicCD = false;
                }
                if (player != null)
                    mat.SetVector(Name, player.position);
                else
                    Debug.Log("123");
            }
        }
    }

    public float GetMicVolume () {
        float[] data = new float[256];
        float a = 0;
        _audio.GetOutputData(data, 0);
        foreach (float s in data) {
            a += Mathf.Abs(s);
        }
        return a / 256;
    }

    void volume () {
        loudness = GetMicVolume() * sensitivity;
    }

    void move () {
        if (Input.GetKey("w")) {
            this.transform.position += transform.forward * Time.deltaTime;
        }
        if (Input.GetKey("s")) {
            this.transform.position -= transform.forward * Time.deltaTime;
        }
        if (Input.GetKey("a")) {
            gameObject.transform.Rotate(new Vector3(0f, -moveSpeed * 100 * Time.deltaTime, 0f));
        }
        if (Input.GetKey("d")) {
            gameObject.transform.Rotate(new Vector3(0f, moveSpeed * 100 * Time.deltaTime, 0f));
        }
    }

    void Sonic () {
        SonicTimer += Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.Space) && SonicCD == false) {
            SonicTimer = 0;
            SonicCD = true;
            GameObject obj = Instantiate(GameManager.Instance.GetSenseSonic().gameObject, transform.position, transform.rotation) as GameObject;
            obj.GetComponent<Sonic>().Launch(obj.transform.forward, GameManager.Instance.GetNormalizedPower(0.5f), GameManager.Instance.GetNormalizedDuration(0.5f));
            if (SonicTimer >= SonicTimer_CD) {
                SonicCD = false;
            }
        }
    }

    public void Healed (int value) {
        health += value;
        health = Mathf.Clamp(health, 0, maxHealth);
    }

    public void Damaged (int value) {
        health -= value;
        health = Mathf.Clamp(health, 0, maxHealth);
        GameManager.Instance.UpdateGameOver();
    }

}
