using UnityEngine;
using System.Collections;

public class CameraSwitch : MonoBehaviour {

    //public Camera Cam;
    public GameObject[] blues;
    public GameObject[] greens;
    private Material blueMat;
    private Material greenMat;
    private bool transparency;

    public float noiseScale;
    public float noiseDistance;
    public float lateFade;
    private float noiseDistanceC;
    private float collDistance;
    private float distance;
    private float enemyDistance;
    private GameObject Enemy;
    private GameObject Player;

    private NoiseEffect noise;

	// Use this for initialization
	void Start () {
        Enemy = GameObject.Find("Enemy");
        Player = GameObject.Find("Character");
        collDistance = Enemy.transform.localScale.x * 0.5f;
        
        this.enabled = true;
        noise = this.GetComponent<NoiseEffect>();
        blueMat = blues[0].renderer.material;
        greenMat = greens[0].renderer.material;
        transparency = true;
        for (int i = 0; i < blues.Length; i++)
            blues[i].renderer.material = GameObject.Find("Transparent").renderer.material;
        for (int i = 0; i < greens.Length; i++)
            greens[i].renderer.material = greenMat;
	}
	
	// Update is called once per frame
    void Update()
    {
        noiseDistanceC = noiseDistance + collDistance;

        if (Application.loadedLevelName == "level1")
            AdjustNoiseY();
        else if (Application.loadedLevelName != "MainMenu")
            AdjustNoise();
        if (Input.GetKeyDown(KeyCode.M))
        {
            this.GetComponent<GrayscaleEffect>().enabled = !this.GetComponent<GrayscaleEffect>().enabled;
            transparency = !transparency;
            if (transparency)
            {
                for (int i = 0; i < blues.Length; i++)
                    blues[i].renderer.material = GameObject.Find("Transparent").renderer.material;
                for (int i = 0; i < greens.Length; i++)
                    greens[i].renderer.material = greenMat;
            }
            if (!transparency)
            {
                for (int i = 0; i < blues.Length; i++)
                    blues[i].renderer.material = blueMat;
                for (int i = 0; i < greens.Length; i++)
                    greens[i].renderer.material = GameObject.Find("Transparent").renderer.material;
            }
        }
	}

    void AdjustNoise()
    {
        enemyDistance = Mathf.Abs(Player.transform.position.x - Enemy.transform.position.x);
        float ratio = enemyDistance / noiseDistanceC;
        float i = 1;

        if (ratio < 1)
        {
            noise.grainIntensityMin = noiseScale * (i - ratio);
            noise.grainIntensityMax = noiseScale * (i - ratio) + 0.5f;
            noise.grainSize = noiseScale * (i + 0.1f - ratio);
            noise.scratchIntensityMin = noiseScale * (i - ratio);
            noise.scratchIntensityMax = noiseScale * (i - ratio) + 0.5f;

            if (transparency)
            {
                for (int j = 0; j < blues.Length; j++)
                {
                    blues[j].renderer.material.color = new Color(0.0f, 0.0f, 0.0f, lateFade - ratio);
                }
            }
        }
        else
        {
            noise.grainIntensityMin = 0;
            noise.grainIntensityMax = 0;
            noise.grainSize = 0.1f;
            noise.scratchIntensityMin = 0;
            noise.scratchIntensityMax = 0;

            if (transparency)
            {
                for (int j = 0; j < blues.Length; j++)
                {
                    blues[j].renderer.material.color = new Color(0.0f, 0.0f, 0.0f, 0.0f);
                }
            }
        }
       // noise.grainIntensityMax += n
    }

    void AdjustNoiseY()
    {
        enemyDistance = Mathf.Abs(Player.transform.position.y - Enemy.transform.position.y);
        float ratio = enemyDistance / noiseDistanceC;
        float i = 1;

        if (ratio < 1)
        {
            noise.grainIntensityMin = noiseScale * (i - ratio);
            noise.grainIntensityMax = noiseScale * (i - ratio) + 0.5f;
            noise.grainSize = noiseScale * (i + 0.1f - ratio);
            noise.scratchIntensityMin = noiseScale * (i - ratio);
            noise.scratchIntensityMax = noiseScale * (i - ratio) + 0.5f;

            if (transparency)
            {
                for (int j = 0; j < blues.Length; j++)
                {
                    blues[j].renderer.material.color = new Color(0.0f, 0.0f, 0.0f, lateFade - ratio);
                }
            }
        }
        else
        {
            noise.grainIntensityMin = 0;
            noise.grainIntensityMax = 0;
            noise.grainSize = 0.1f;
            noise.scratchIntensityMin = 0;
            noise.scratchIntensityMax = 0;

            if (transparency)
            {
                for (int j = 0; j < blues.Length; j++)
                {
                    blues[j].renderer.material.color = new Color(0.0f, 0.0f, 0.0f, 0.0f);
                }
            }
        }
    }
}

/*
The greater the distance, the less the noise is
 * and viceaversa
 * 
*/