using UnityEngine;
using System.Collections;
using Framework.Entities;
using Framework.Utility;
using Framework.StatSystem;
using Framework.Extensions;
using UnityStandardAssets.ImageEffects;

public class AlchemistShadowAbility : CoreBehaviour 
{
    private bool shadowActive = false;
    public UnityTimer shadowTime = new UnityTimer(5f, false);
    public GameObject camera;
    public AudioClip startClip;

    private GameObject visual;
    private ActorClassifier actorClassifier;
    private AlchemistBag bag;
    private ScaleModifier stealthModifier = new ScaleModifier(0.1f);
    private MeshRenderer renderer;

    public void StartShadow()
    {
        if (!this.shadowActive && this.bag.invisibilityCount >= 1)
        {
            this.bag.invisibilityCount--;
            this.shadowActive = true;
            this.actorClassifier.stealth.AddModifier(this.stealthModifier);
            Color currentColor = this.visual.GetComponent<SpriteRenderer>().color;
            this.visual.GetComponent<SpriteRenderer>().color = new Color(currentColor.r, currentColor.g, currentColor.b, 0.1f);
            camera.GetComponent<ColorCorrectionCurves>().enabled = true;
            //AudioSource.PlayClipAtPoint(startClip, this.transform.position);
        }
    }

    protected override void InnerAwake()
    {
        this.visual = this.transform.RecursivelyFindChild("Visual").gameObject;
        this.bag = GetComponent<AlchemistBag>();
        this.actorClassifier = GetComponent<ActorClassifier>();
        this.renderer = GetComponent<MeshRenderer>();
    }

    protected override void InnerUpdate()
    {
        if (this.shadowActive)
        {
            this.shadowTime.Advance();
        }


        if (this.shadowTime.IsComplete())
        {
            this.actorClassifier.stealth.RemoveModifier(this.stealthModifier);
            this.shadowTime.Reset();
            Color currentColor = this.visual.GetComponent<SpriteRenderer>().color;
            this.visual.GetComponent<SpriteRenderer>().color = new Color(currentColor.r, currentColor.g, currentColor.b, 1f);
            camera.GetComponent<ColorCorrectionCurves>().enabled = false;
            this.shadowActive = false;
        }
    }
}
