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

public class Sotry : MonoBehaviour {

    List<string> stories = new List<string>();

    [SerializeField]
    GameObject leftBtn;

    [SerializeField]
    GameObject rightBtn;

    [SerializeField]
    GameObject continueBtn;

    [SerializeField]
    GameObject TextBox;

    [SerializeField]
    GameObject text;

    int storyProgress;
    int continueProgress;

    Combat combat;

    bool progressing;
    float storyTimer;

    bool first;

    bool choice;

    [SerializeField]
    GameObject GameOber;
    [SerializeField]
    GameObject NormEdn;
    [SerializeField]
    GameObject EndingScreen;
    [SerializeField]
    GameObject Game;

    void Awake()
    {
        //Ensin hetki
        string tmp = "You open your eyes and take a long look around. There's no one, only the wind caresses your cheek for a while, or was it just your imagination?";
        stories.Add(tmp);

        //Eka valinta forest // mountain
        tmp = "You find yourself on a crossroad. Ahead you have two options. The other road leads towards the mountains and the other in to the deep, dark woods. Which road you chooce?";
        stories.Add(tmp);

        //toka valinta 
        tmp = "The road takes you in the middle of the forest, deep into the thickets. Soon the road is cut off by a fallen tree. You can't get over it in your dress. Turn around. ";
        stories.Add(tmp);

        //kolmas
        tmp = "You try to climb over the fallen tree and rip your dress. Do you still want to try? ";
        stories.Add(tmp);

        //neljäs
        tmp = "The road leads you to the base of a mountain. You feel the air suddenly getting colder, from warm to almost freezing. ";
        stories.Add(tmp);

        //5
        tmp = "A foggy river opens before you. Further un the fog you can see a bridge, but the other path continues along side the riverbank. Which way do you choose?";
        stories.Add(tmp);

        //6
        tmp = "You head towards the bridge and attempt to cross it. As you step on the planks, that due further inspection are actually quite old, you hear omnious creaking. The bridge collapses before you can get across the river.  ";
        stories.Add(tmp);

        //7
        tmp = "The calmness emitting from the path alongside the riverbank draws you in stronger than the odd looking bridge. The surrounding area is beautiful but a sudden splash from middle of a bed of reeds interrupts your calm progression.  ";
        stories.Add(tmp);

        //8
        tmp = "From the river you arrive to an entrance of a cave. You head into the darkness, when suddenly dark mist surrounds you. Through the mist you can see two openings, other having a faint glow coming from inside, other is covered in webs. ";
        stories.Add(tmp);

        //9
        tmp = "You follow the faint glow and find an old, decayed shrine. The glowing light comes from within. You are enchanted by the glow and take a step towards the shrine. You then hear skittering steps from behind you.  ";
        stories.Add(tmp);

        //10
        tmp = "You choose the entrance covered in webs and enter a large space, almost a new cave in itself. The first thing to pique your interest are the large, eggsacks growing on the walls. Everything in the cave is cloaked by the web like substance. Something tells you to turn around. ";
        stories.Add(tmp);


        storyProgress = 0;
        continueProgress = 0;

        combat = GetComponent<Combat>();

        progressing = true;
        first = true;
        choice = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Keypad0))
        {
            //ProgressStory1();
           
        }

        if (!progressing)
            storyTimer += Time.deltaTime;

        if (storyTimer >= 1f)
        {
            if (storyProgress <= 4)
                ProgressStory1();
            else if (storyProgress >= 5 && storyProgress <= 6)
                ProgressStory2();
            else
                ProgressStory3();
        }
    }

    public void LeftButton()
    {
        Deacvite();
        progressing = false;
        continueBtn.SetActive(false);

        if (!first)
        {
            ContinueBtn();
        }


        first = false;

        choice = false;

        print(storyProgress);
    }

    public void Prorgress()
    {
        progressing = false;
    }


    public void RightButton()
    {
        Deacvite();
        progressing = false;

        if (storyProgress == 4)
            ContinueBtn();

        if (storyProgress == 6)
            ContinueBtn();
    }

    public void ContinueBtn()
    {
        Deacvite();

        progressing = false;

        //print(storyProgress);
        //print(continueProgress);

        if (continueProgress == 1)
        {
            combat.ChangeMap(1);
            continueProgress++;
            storyProgress = 4;
        }
        else if (continueProgress == 2)
        {
            combat.SpawnStoryEnemy();
            progressing = true;
            storyTimer = 0;
            storyProgress = 5;
            continueProgress++;
        }
        else if (continueProgress == 3)
        {
            choice = true;
            continueProgress++;
            //storyProgress = 6;
            print("couhs gaba");
        }
        else if (continueProgress == 4)
        {
            print("battle bagga");
            combat.SpawnStoryEnemy();
            progressing = true;
            storyTimer = 0;
            continueProgress++;
        }

        else if (continueProgress == 5)
        {
            choice = true;
            continueProgress++;
        }
        else if (continueProgress == 6)
        {
            combat.SpawnStoryEnemy();
            progressing = true;
            storyTimer = 0;
        }

        continueBtn.SetActive(false);
    }

    public void ContinueProgress()
    {
        progressing = false;
        storyTimer = 0;
    }

    public void ProgressStory1()
    {
        progressing = true;
        storyTimer = 0;

        leftBtn.transform.GetChild(0).GetComponent<Text>().text = "Yes";
        rightBtn.transform.GetChild(0).GetComponent<Text>().text = "No";

        if (storyProgress == 0)
        {
            text.GetComponent<Text>().text = stories[storyProgress];
            continueBtn.SetActive(true);
            TextBox.SetActive(true);
            text.SetActive(true);
        }
        else if (storyProgress == 1)
        {
            leftBtn.SetActive(true);
            leftBtn.transform.GetChild(0).GetComponent<Text>().text = "Forest";
            continueBtn.SetActive(true);
            TextBox.SetActive(true);
            text.SetActive(true);
            continueBtn.transform.GetChild(0).GetComponent<Text>().text = "Mountains";
            text.GetComponent<Text>().text = stories[storyProgress];
            continueProgress++;
        }
        else if (storyProgress == 2)
        {
            AcitvateStoryBtn();
            text.GetComponent<Text>().text = stories[storyProgress];
        }
        else if (storyProgress == 3)
        {
            leftBtn.transform.GetChild(0).GetComponent<Text>().text = "No";
            rightBtn.transform.GetChild(0).GetComponent<Text>().text = "Yes";

            AcitvateStoryBtn();
            text.GetComponent<Text>().text = stories[storyProgress];
        }
        else if (storyProgress == 4)
        {
            continueBtn.transform.GetChild(0).GetComponent<Text>().text = "Continue";
            text.GetComponent<Text>().text = stories[storyProgress];
            continueBtn.SetActive(true);
            TextBox.SetActive(true);
            text.SetActive(true);
            SoundManager.Instance.PlaySound(7);
        }

        storyProgress++;
    }

    public void ProgressStory2()
    {
        progressing = true;
        storyTimer = 0;

        if (storyProgress == 5)
        {
            first = false;

            rightBtn.SetActive(false);
            leftBtn.transform.GetChild(0).GetComponent<Text>().text = "Bridge";
            continueBtn.transform.GetChild(0).GetComponent<Text>().text = "Riverbank";

            leftBtn.SetActive(true);
            continueBtn.SetActive(true);

            text.GetComponent<Text>().text = stories[storyProgress];

            TextBox.SetActive(true);
            text.SetActive(true);
            storyProgress++;
        }        
        else if (storyProgress == 6)
        {
            print("asdf");

            if(!choice)
                text.GetComponent<Text>().text = stories[6];
            else
                text.GetComponent<Text>().text = stories[7];

            continueBtn.transform.GetChild(0).GetComponent<Text>().text = "Continue";
            continueBtn.SetActive(true);
            TextBox.SetActive(true);
            text.SetActive(true);
            storyProgress++;
        }
        
    }

    public void ProgressStory3()
    {
        progressing = true;
        storyTimer = 0;

        if (storyProgress == 7)
        {
            leftBtn.transform.GetChild(0).GetComponent<Text>().text = "Glowing entrance";
            continueBtn.transform.GetChild(0).GetComponent<Text>().text = "Webbed etnrance";

            leftBtn.SetActive(true);
            text.GetComponent<Text>().text = stories[8];
            continueBtn.SetActive(true);
            TextBox.SetActive(true);
            text.SetActive(true);
            storyProgress++;
        }
        else if (storyProgress == 8)
        {
            if (!choice)
                text.GetComponent<Text>().text = stories[9];
            else
                text.GetComponent<Text>().text = stories[10];

            continueBtn.transform.GetChild(0).GetComponent<Text>().text = "Continue";
            continueBtn.SetActive(true);
            TextBox.SetActive(true);
            text.SetActive(true);
            storyProgress++;
        }

        else if (storyProgress == 9)
        {
            Ending();
            progressing = true;
            storyTimer = 0;
            Deacvite();
        }

    }

    public void GameOver()
    {
        combat.EndCombat();
        GameOber.SetActive(true);
        EndingScreen.SetActive(true);
        EndingScreen.transform.GetChild(0).gameObject.SetActive(true);
        Game.SetActive(false);
        Time.timeScale = 0;
        SoundManager.Instance.PlaySound(3);
    }

    public void Ending()
    {
        NormEdn.SetActive(true);
        EndingScreen.SetActive(true);
        SoundManager.Instance.PlaySound(4);
    }

    void AcitvateStoryBtn()
    {
        leftBtn.SetActive(true);
        rightBtn.SetActive(true);
        TextBox.SetActive(true);
        text.SetActive(true);
    }

    void Deacvite()
    {
        leftBtn.SetActive(false);
        rightBtn.SetActive(false);
        TextBox.SetActive(false);
        text.SetActive(false);
    }
}
