using System.Collections;
using System.Collections.Generic;
using Tobii.EyeTracking;
using UnityEngine;
[RequireComponent(typeof(GazeAware))]
public class ReactionBlock: MonoBehaviour {
    private GazeAware _gazeAware;

    [SerializeField]
    AbilityType type;

    [SerializeField] Sounds noise;

    [SerializeField]
    Sprite offState;

    [SerializeField]
    Sprite onState;

    [SerializeField]
    bool isTurnedOn;

    SpriteRenderer spriteRenderer;

    //[SerializeField]
    //Bridge bridge;

    [SerializeField]
    bool revertOnAway;



    Sprite CurrentSprite {
        get {
            if(isTurnedOn) {
                return onState;
            } else {
                return offState;
            }
        }
    }

    public AbilityType Type {
        get {
            return type;
        }

        set {
            type = value;
        }
    }

    public Sprite OnState {
        get {
            return onState;
        }

        set {
            onState = value;
        }
    }

    public Sprite OffState {
        get {
            return offState;
        }

        set {
            offState = value;
        }
    }

    void Awake() {
        spriteRenderer = GetComponent<SpriteRenderer>();
        _gazeAware = GetComponent<GazeAware>();
        transform.GetComponentInChildren<SpriteRenderer>().sprite = offState;
        fireRate = !revertOnAway ? fireRate / 1000000f : fireRate;
    }


    [SerializeField]
    float fireRate = 1f;

    [SerializeField]
    float timeToFire = 0;

    void Start() {
        //transform.GetComponentInChildren<SpriteRenderer>().sprite = CurrentSprite;

    }

    bool looked = false;
    void Update() {


        if(_gazeAware.HasGazeFocus && Time.time > timeToFire /*&& !isTurnedOn*/) {
            timeToFire = Time.time + 1.0f / fireRate;
            Debug.Log("Gaze focus");
            isTurnedOn = !isTurnedOn;
            transform.GetComponentInChildren<SpriteRenderer>().sprite = CurrentSprite;
            looked = true;
            MusicManager.Instance.PlayOneShot(MusicManager.Instance.MapAudio(noise));
            
        }


        if(looked && revertOnAway) {
                isTurnedOn = !isTurnedOn;
                transform.GetComponentInChildren<SpriteRenderer>().sprite = CurrentSprite;
            }


        looked = false;
    }

    void OnCollisionEnter(Collision col) {
        //Debug.Log("trigger enter block");

        //var hitBullet = col.transform.GetComponent<Bullet>();
        //if(hitBullet) {
        //    if(hitBullet.Type.Equals(type)) {
        //        isTurnedOn = !isTurnedOn;
        //        transform.GetComponentInChildren<SpriteRenderer>().sprite = CurrentSprite;
        //    }
        //}
        
    }

}
