using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Modulator : Item
{
    public enum PowerState
    {
        BLANK, BLUE, GREEN, RED
    }

    Player player;

    
    public GameObject pipeEnd;
    [Header("Modulator sprites")]
    public Sprite modulatorBlank;
    public Sprite modulatorBlue;
    public Sprite modulatorGreen;
    public Sprite modulatorRed;
    [Header("Pipeline textures")]
    public Material pipeLineBlank;
    public Material pipeLineBlue;
    public Material pipeLineGreen;
    public Material pipeLineRed;

    public AudioClip stretchSound;
    public float stretchInterval = 0.3f;
    private float timeSinceLastStretch;

    [HideInInspector]
    public PowerState currentState = PowerState.BLANK;

    private Sprite newSprite;
    private SpriteRenderer spriteRenderer;
    private LineRenderer pipeLine;
    private bool startMovingPipeline = false;
    private bool movingPipeLine = false;

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        spriteRenderer = GetComponentInChildren<SpriteRenderer>();
        pipeLine = GetComponentInChildren<LineRenderer>();
    }

    // Update is called once per frame
    new void Update()
    {
        base.Update();

        if (movingPipeLine && Input.GetKeyDown(KeyCode.X))
        {
            DoneInteracting();
        }

        if (startMovingPipeline)
        {
            movingPipeLine = true;
            pipeLine.SetPosition(0, transform.position);
            pipeLine.SetPosition(1, new Vector2(transform.position.x, player.transform.position.y));
            pipeLine.SetPosition(2, player.transform.position);
            pipeEnd.transform.position = player.transform.position;
        }

        //Stretch audio
        if (movingPipeLine && Time.time - timeSinceLastStretch >= stretchInterval)
        {
            SoundManager.instance.PlaySound(stretchSound, true);
            timeSinceLastStretch = Time.time;
        }
    }

    public override void Hold(Transform location)
    {
        base.Hold(location);
        pipeLine.SetPosition(0, transform.position);
        pipeLine.SetPosition(1, transform.position);
        pipeLine.SetPosition(2, transform.position);
        pipeEnd.transform.position = transform.position;
    }

    public void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("SoundEmitter"))
        {
            SoundEmitter sound = collision.GetComponent<SoundEmitter>();
            if (sound.GetIsEmitting())
            {
                //print("The soundemitter is active");
                if (sound.soundType == SoundEmitter.SoundType.BLUE)
                {
                    ChangePowerState(PowerState.BLUE);
                }
                else if (sound.soundType == SoundEmitter.SoundType.GREEN)
                {
                    ChangePowerState(PowerState.GREEN);
                }
                else
                {
                    ChangePowerState(PowerState.RED);
                }
            }
            else
            {
                ChangePowerState(PowerState.BLANK);
            }
        }
    }

    public void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("SoundEmitter"))
        {
            ChangePowerState(PowerState.BLANK);
        }
    }

    public void ChangePowerState(PowerState newPowerState)
    {
        currentState = newPowerState;
        if (newPowerState == PowerState.BLANK)
        {
            newSprite = modulatorBlank;
            pipeLine.material = pipeLineBlank;
        }
        else if (newPowerState == PowerState.BLUE)
        {
            newSprite = modulatorBlue;
            pipeLine.material = pipeLineBlue;
        }
        else if (newPowerState == PowerState.GREEN)
        {
            newSprite = modulatorGreen;
            pipeLine.material = pipeLineGreen;
        }
        else
        {
            newSprite = modulatorRed;
            pipeLine.material = pipeLineRed;

        }

        if (spriteRenderer.sprite != newSprite)
        {
            spriteRenderer.sprite = newSprite;
        }
    }

    public override void Interact()
    {
        player.interacting = true;
        startMovingPipeline = true;
    }

    public void DoneInteracting()
    {
        player.interacting = false;
        startMovingPipeline = false;
        movingPipeLine = false;
    }

}
