using Godot;
using Godot.Collections;
using System;
using System.Collections.Generic;
using System.Linq;

public partial class Game : Node
{
	public static Game sala { get; set; }

    [Export]
    public Control[] N;
    [Export]
    public AudioStream[] audios;
    [Export]
    public AudioStream[] fxs;
    public Item itemSelected = null;

    Control tempAtras;
    TextureButton atrasBtn;
    Label time;
    GridContainer Grid;
    TextureRect F;
    PackedScene _item;
    int index = 0;
    bool PuertaAbierta = false;
    double _time = 208;
    AudioStreamPlayer audio;
    AudioStreamPlayer fx;
    string claveCajaF,clavePuerta;

    public override void _Ready()
	{
		sala = this;
		F= GetNode<TextureRect>("Control/Fondo");
        Grid=GetNode<GridContainer>("Control/Items/Grid");
        time= GetNode<Label>("Control/Fondo/Negro/Time");
        _item = ResourceLoader.Load<PackedScene>("res://Tscns/Item.tscn");
		index = 0;
        N[0].Visible = true;
        audio = GetNode<AudioStreamPlayer>("Audio");
        fx = GetNode<AudioStreamPlayer>("FX");
        audio.Stream = audios[0];
        audio.Play();
        atrasBtn = GetNode<TextureButton>("Control/D");
        var r = new RandomNumberGenerator();
        r.Randomize();
        claveCajaF=r.RandiRange(111111, 999999).ToString();
        clavePuerta = r.RandiRange(111111, 999999).ToString();
    }

    public void _on_r_pressed()
	{
        N[index].Visible = false;
        index++;
        if(index>3)
			index=0;
        N[index].Visible = true;

    }

    public void _on_l_pressed()
    {
        N[index].Visible = false;
        index--;
        if (index < 0)
            index = 3;
        N[index].Visible = true;

    }

    public void _on_Atras_pressed()
    {
        tempAtras.Visible = false;
        tempAtras = null;
    }

    public void _on_buttonElement_pressed(int i)
    {
        switch (i)
        {
            case 1://CajaF
                tempAtras = GetNode<Control>("Control/1/CajaFuerte");
                tempAtras.Visible = true;
                break;
            case 2://CajaFClave
                if(GetNode<LineEdit>("Control/1/CajaFuerte/1/Panel/LineEdit").Text.Equals(claveCajaF))
                {
                    GetNode<Control>("Control/1/CajaFuerte/1").Visible=false;
                    GetNode<Control>("Control/1/CajaFuerte/2").Visible=true;
                    fx.Stream = fxs[0];
                    fx.Play();
                }
                else
                {
                    GetNode<LineEdit>("Control/1/CajaFuerte/1/Panel/LineEdit").Text = "";
                    fx.Stream = fxs[1];
                    fx.Play();
                }
                break;
            case -1://cerrado
                fx.Stream = fxs[1];
                fx.Play();
                break;
            case 4://cajon 3
                tempAtras = GetNode<Control>("Control/1/Cajon");
                tempAtras.Visible = true;
                fx.Stream = fxs[2];
                fx.Play();
                break;
            case 5://Plante grande
                if (ParseNodeItem(Grid.GetChildren()).Where(x => x.Tipo == TipoItem.Destornillador).Count() <= 0)
                {
                    fx.Stream = fxs[0];
                    fx.Play();
                    var temp1 = _item.Instantiate<Item>();
                    temp1.Tipo = (TipoItem)5;
                    Grid.AddChild(temp1);
                }
                else
                {
                    fx.Stream = fxs[1];
                    fx.Play();
                }
                break;
            case 6://reja
                if(itemSelected !=null && itemSelected.Tipo==TipoItem.Destornillador)
                {
                    GetNode<TextureButton>("Control/2/Vantilacion").QueueFree();
                    fx.Stream = fxs[0];
                    fx.Play();
                }
                else
                {
                    fx.Stream = fxs[1];
                    fx.Play();
                }
                break;
            case 7://baul
                if (itemSelected != null && itemSelected.Tipo==TipoItem.LlaveCajaF)
                {
                    tempAtras = GetNode<Control>("Control/3/InBaul");
                    tempAtras.Visible = true;
                    fx.Stream = fxs[3];
                    fx.Play();
                }
                else
                {
                    fx.Stream = fxs[1];
                    fx.Play();
                }
                break;
            case 8://in lector
                if (!PuertaAbierta)
                {
                    tempAtras = GetNode<Control>("Control/4/InLector");
                    tempAtras.Visible = true;
                }
                else
                {
                    fx.Stream = fxs[1];
                    fx.Play();
                }
                break;
            case 9://accion codigo
                if (GetNode<LineEdit>("Control/4/InLector/Panel/LineEdit").Text.Equals(clavePuerta) && !PuertaAbierta)
                {


                    if (itemSelected != null && itemSelected.Tipo != TipoItem.Tarjeta)
                    {
                        fx.Stream = fxs[1];
                        fx.Play();
                    }
                    else
                    {
                        PuertaAbierta = true;
                        tempAtras.Visible = false;
                        tempAtras = null;
                        fx.Stream = fxs[0];
                        fx.Play();
                    }
                }
                else
                {
                    GetNode<LineEdit>("Control/4/InLector/Panel/LineEdit").Text = "";
                    fx.Stream = fxs[1];
                    fx.Play();
                }
                break;
            case 11://puerta
                if(PuertaAbierta)
                {
                    GetTree().ChangeSceneToFile("res://Tscns/Final.tscn");
                }
                else
                {
                    fx.Stream = fxs[1];
                    fx.Play();
                }
                break;
            case 12://ventilacion
                if (itemSelected!=null && itemSelected.Tipo!=TipoItem.Destornillador)
                {
                    fx.Stream = fxs[1];
                    fx.Play();
                }
                else
                {
                    var temp2 = _item.Instantiate<Item>();
                    temp2.Tipo = (TipoItem)10;
                    Grid.AddChild(temp2);
                }
                break;
            case 13://nota
                fx.Stream = fxs[4];
                fx.Play();
                tempAtras.Visible = false;
                GetNode<Label>("Control/1/Nota/ClaveF").Text = claveCajaF;
                tempAtras = GetNode<Control>("Control/1/Nota");
                tempAtras.Visible = true;
                break;
            case 14://nota2
                fx.Stream = fxs[4];
                fx.Play();
                tempAtras.Visible = false;
                tempAtras = GetNode<Control>("Control/3/Nota1");
                tempAtras.Visible = true;
                break;
            case 15://nota3
                fx.Stream = fxs[4];
                fx.Play();
                GetNode<Label>("Control/3/Nota2/ClaveP").Text = clavePuerta;
                tempAtras = GetNode<Control>("Control/3/Nota2");
                tempAtras.Visible = true;
                break;
        }
    }

    private IEnumerable<Item> ParseNodeItem(Array<Node> nodes)
    {
        IEnumerable<Item> t=new List<Item>();
        return nodes.OfType<Item>();
    }

    public void _on_buttonItem_pressed(int i)
    {
        switch (i)
        {
            
            case 3://llave en cajaF
                GetNode("Control/1/CajaFuerte/2/Llave").QueueFree();
                fx.Stream = fxs[5];
                fx.Play();
                break;
            case 10://tarjeta
                fx.Stream = fxs[4];
                fx.Play();
                GetNode("Control/2/FondoV/Tarjeta").QueueFree();
                break;
        }
        var temp = _item.Instantiate<Item>();
        temp.Tipo = (TipoItem)i;
        Grid.AddChild(temp);
    }
    public void _on_Exit_pressed()
    {
        GetNode<AudioStreamPlayer>("Exit").Play();
        
    }
    public void _on_exit_finished()
    {
        GetTree().Quit();
    }
    public override void _Process(double delta)
	{
        _time-= delta;
        var m = (int)(_time / 60);
        var s = (int)(_time - m * 60);
        time.Text = (m < 10 ? "0" + m : m) + (((int)_time) % 2 == 0 ? ":" : " ") + (s < 10 ? "0" + s : s);
        if(m==2 & s==22 && audio.Stream.Equals(audios[0]))
        {
            audio.Stream = audios[1];
            audio.Play();
        }
        else if (m == 1 & s == 16 && audio.Stream.Equals(audios[1]))
        {
            audio.Stream = audios[2];
            audio.Play();
        }
        else if (m == 0 & s == 10 && audio.Stream.Equals(audios[2]))
        {
            audio.Stream = audios[3];
            audio.Play();
        }
        else if (m == 0 & s == 0 && _time < 0)
        {
            GetTree().ReloadCurrentScene();
        }
        if (tempAtras != null && !atrasBtn.Visible)
        {
            atrasBtn.Visible = true;
            GetNode<Control>("Control/R").Visible = false;
            GetNode<Control>("Control/L").Visible = false;
        }
        else if (tempAtras == null && atrasBtn.Visible)
        {
            atrasBtn.Visible = false;
            GetNode<Control>("Control/R").Visible = true;
            GetNode<Control>("Control/L").Visible = true;
        }
    }
}
