using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.ImageEffects;

public class SamuraiWave : MonoBehaviour {

    [SerializeField]private Light pointLight;
    [SerializeField]private float lightGrowDuration;
    [SerializeField]private float lightShrinkDuration;    

    [SerializeField]private float lightOnDuration;
    [SerializeField]private GameObject lightOnParticle;
    public bool lightOnAnimating = false;    
    
    private float timeLerpStarted;
    private float activeTimer;

    private bool lightOn;
    private bool lightTransition;

    [SerializeField]private BlurOptimized blurEffect;
    [SerializeField]private float blurTransitionDuration;
    private int defaultDownsample;
    private float defaultSize;
    private int defaultIterations;
    
    private float timeBlurTransitionStarted;
    private bool changeBlurState;
    
    private const float maxLightRange = 120f;    
    private const float minLightRange = 0f;

    private Animator anim;
    private PlayerMovement mov;

    [SerializeField]private AudioSource swordLightSound;

    private void Start()
    {
        defaultDownsample = blurEffect.downsample;
        defaultSize = blurEffect.blurSize;
        defaultIterations = blurEffect.blurIterations;
        anim = GetComponent<Animator>();
        mov = GetComponent<PlayerMovement>();
    }

    // Update is called once per frame
    void Update () {
        CheckLightInput();
        ChangeLightRange();
        BlurTransition();

        if (lightOn && !lightTransition)
        {
            CountActiveTime();
        }
	}

    private void ChangeLightState ()
    {
        lightTransition = true;
        lightOn = !lightOn;
        timeLerpStarted = Time.time;

        Globals.CanMove = !Globals.CanMove;

        if (lightOn)
        {
            changeBlurState = true;
            timeBlurTransitionStarted = Time.time;
        }
    }

    private void CheckLightInput ()
    {
        if (!mov.IsHit)
        {
            if (Input.GetButtonDown("Fire2") && !lightTransition && !lightOn && mov.CanLightOn() && !lightOnAnimating)
            {
                mov.ResetVelocity();
                anim.SetTrigger("LightOn");
                lightOnAnimating = true;
            }
        }
    }

    //called from animation
    public void LightOn() {
        GameObject obj = Instantiate(lightOnParticle, transform.position, transform.rotation);
        obj.transform.SetParent(transform);
        obj.transform.localPosition = new Vector2(0.08f, 0.36f);
        ChangeLightState();
        swordLightSound.Play();
    }

    private void ChangeLightRange ()
    {
        if (lightTransition)
        {
            float timeSinceLerpStarted = Time.time - timeLerpStarted;
            float lerpRate = 0f;

            if (lightOn)
            {
                lerpRate = timeSinceLerpStarted / lightGrowDuration;
                pointLight.spotAngle = Mathf.Lerp(minLightRange, maxLightRange, lerpRate);
            }
            else
            {
                lerpRate = timeSinceLerpStarted / lightShrinkDuration;
                pointLight.spotAngle = Mathf.Lerp(maxLightRange, minLightRange, lerpRate);
            }

            if (!lightOn && lerpRate >= 0.5f && !changeBlurState)
            {
                changeBlurState = true;
                timeBlurTransitionStarted = Time.time;
            }

            if (lerpRate >= 1f)
            {
                lightTransition = false;                             
            }
        }
    }

    private void CountActiveTime ()
    {
        activeTimer += Time.deltaTime;

        if (activeTimer >= lightOnDuration)
        {
            ChangeLightState();
            activeTimer = 0f;
        }
    }

    private void BlurTransition ()
    {
        if (changeBlurState)
        {
            float timeSinceLerpStarted = Time.time - timeBlurTransitionStarted;
            float lerpRate = timeSinceLerpStarted / blurTransitionDuration;

            float tempDownsample = 0;
            float tempBlurSize = 0f;
            float tempBlurIterations = 0;

            if (lightOn)
            {
                tempDownsample = Mathf.Lerp(defaultDownsample, 0, lerpRate);
                tempBlurSize = Mathf.Lerp(defaultSize, 0f, lerpRate);
                tempBlurIterations = Mathf.Lerp(defaultIterations, 0f, lerpRate);
            }
            else
            {
                tempDownsample = Mathf.Lerp(0f, defaultDownsample, lerpRate);
                tempBlurSize = Mathf.Lerp(0f, defaultSize, lerpRate);
                tempBlurIterations = Mathf.Lerp(0f, defaultIterations, lerpRate);
            }

            blurEffect.downsample = (int)tempDownsample;
            blurEffect.blurSize = tempBlurSize;
            blurEffect.blurIterations = (int)tempBlurIterations;

            if (lerpRate >= 1f)
            {
                changeBlurState = false;
            }
        }
    }

    //using in animation
    public void FinishLightOnAnimation() {
        lightOnAnimating = false;
    }
}
