﻿using Kino;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class EndingScript : MonoBehaviour {

    private float fadeFactorCurr;
    bool intiitalFade = true;
    public GameObject if1, if2, nnnObj, stxxs;
    bool ifctr = false;
    float ifctf;
    bool nnn = false;
    bool iiibbb = false, ssshhshs = false, boom = false;

    float since = 2.0f;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update()
    {
        if (intiitalFade) {
            if1.active = true;
            if2.active = true;
            fadeFactorCurr += Time.deltaTime;
            Camera.main.GetComponent<DigitalGlitch>().intensity = Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 0, fadeFactorCurr / 10);
            if (Camera.main.GetComponent<DigitalGlitch>().intensity== Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 0, fadeFactorCurr / 10))
            {
                intiitalFade = false;
                ifctr = true;
                ifctf = Time.time;
                fadeFactorCurr = 0;
            }
        }
        if (ifctr && Time.time - ifctf <= since)
        {
            fadeFactorCurr += Time.deltaTime;
            Camera.main.GetComponent<DigitalGlitch>().intensity = Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 1, fadeFactorCurr / 10);
            if (Camera.main.GetComponent<DigitalGlitch>().intensity == Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 1, fadeFactorCurr / 10))
            {
                ifctr = false;
                if1.active = false;
                if2.active = false;
                ifctf = Time.time;
                nnn = true;
                nnnObj.active = true;
                fadeFactorCurr = 0;
            }
        }
        if (nnn)
        {
            fadeFactorCurr += Time.deltaTime;
            Camera.main.GetComponent<DigitalGlitch>().intensity = Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 0, fadeFactorCurr / 10);
            if (Camera.main.GetComponent<DigitalGlitch>().intensity == Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 0, fadeFactorCurr / 10))
            {
                nnn = false;
                nnnObj.active = false;
                ifctf = Time.time;
                iiibbb = true;
                fadeFactorCurr = 0;
            }
        }
        if (iiibbb && Time.time - ifctf <= since)
        {
            fadeFactorCurr += Time.deltaTime;
            Camera.main.GetComponent<DigitalGlitch>().intensity = Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 1, fadeFactorCurr / 10);
            if (Camera.main.GetComponent<DigitalGlitch>().intensity == Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 1, fadeFactorCurr / 10))
            {
                iiibbb = false;
                nnnObj.active = false;
                stxxs.active = true;
                ifctf = Time.time;
                fadeFactorCurr = 0;
                ssshhshs = true;
            }
        }
        if(ssshhshs)
        {
            fadeFactorCurr += Time.deltaTime;
            Camera.main.GetComponent<DigitalGlitch>().intensity = Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 0, fadeFactorCurr / 10);
            if (Camera.main.GetComponent<DigitalGlitch>().intensity == Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 0, fadeFactorCurr / 10))
            {
                ssshhshs = false;
                boom = true;
                fadeFactorCurr = 0;
                ifctf = Time.time;
            }
        }
        if(boom && Time.time - ifctf <= since)
        {
            fadeFactorCurr += Time.deltaTime;
            Camera.main.GetComponent<AudioSource>().volume = Mathf.Lerp(Camera.main.GetComponent<AudioSource>().volume, 0, fadeFactorCurr / 10);
            Camera.main.GetComponent<DigitalGlitch>().intensity = Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 1, fadeFactorCurr / 10);
            Camera.main.GetComponent<AnalogGlitch>().scanLineJitter = Mathf.Lerp(Camera.main.GetComponent<AnalogGlitch>().scanLineJitter, 1, fadeFactorCurr / 10);
            Camera.main.GetComponent<AnalogGlitch>().verticalJump = Mathf.Lerp(Camera.main.GetComponent<AnalogGlitch>().verticalJump, 1, fadeFactorCurr / 10);
            Camera.main.GetComponent<AnalogGlitch>().horizontalShake = Mathf.Lerp(Camera.main.GetComponent<AnalogGlitch>().horizontalShake, 1, fadeFactorCurr / 10);
            Camera.main.GetComponent<AnalogGlitch>().colorDrift = Mathf.Lerp(Camera.main.GetComponent<AnalogGlitch>().colorDrift, 1, fadeFactorCurr / 10);
            if (Camera.main.GetComponent<DigitalGlitch>().intensity == Mathf.Lerp(Camera.main.GetComponent<DigitalGlitch>().intensity, 1, fadeFactorCurr / 10))
            {
                SceneManager.LoadScene("Title");
            }
        }
    }

    void startLerp()
    {
        
    }
}
