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

public class DialogueScript : MonoBehaviour
{
    public DataScript Data;
    public TextAsset Dialogue;
    public string[] Lines;
    public GameObject DialogueBox;
    public GameObject FearAnswer;
    public GameObject AngerAnswer;
    //public GameObject NeutralAnswer;
    public GameObject DoYouKnow;
    public GameObject LoveAnswer;
    public GameObject NothingThanks;
    public GameObject VanQuestion;
    public GameObject NothingAnswer;
    public GameObject PatronFearAnswer;
    public GameObject DoYouKnowPatron;
    public GameObject KnockHisDrink;
    public GameObject WalkAway;
    public GameObject SomethingSpecial;
    public GameObject Actually;
    public GameObject OhThankYou;
    public GameObject FearPool;
    public GameObject AngerPool;
    public GameObject BigGothFear;
    public GameObject BigGothAnger;
    public GameObject BigGothLove;
    public GameObject Steal;
    public GameObject Accident;
    public GameObject ToScene2;
    public GameObject ToScene3;
    public GameObject Underwater;
    public GameObject Clyde;
    public GameObject Casino;
    public GameObject BarDoor;
    public GameObject StartButton;
    public GameObject KnockKnock;
    public GameObject textAnswer1;
    public GameObject textAnswer2;
    public GameObject textAnswer3;
    public GameObject textAnswer4;
    public GameObject textAnswer5;
    public GameObject textAnswer6;
    public GameObject textAnswer7;
    public GameObject textAnswer8;
    public GameObject ffearAnswer;
    public GameObject fangerAnswer;
    public GameObject SelkieAndThief;
    public bool angerUnlocked;
    public GameObject loveAnswer;
    public bool loveUnlocked;
    public GameObject sorrowAnswer;
    public bool sorrowUnlocked;
    public Text Text;
    public int CurrentLine;
    public int Stopper;
    public bool Flag;
    public bool AnswerTime = false;
    public bool Fear1 = false;
    public bool Anger1 = false;
    public bool AngerOption = false;
    public bool LoveOption = false;
    public bool Love1 = false;
    public bool Neutral = false;
    public bool Final = false;
    public bool Final2 = false;
    public bool PatronLove = false;
    public bool PatronAnger = false;
    public bool PatronFinal = false;
    public bool FearPoolState = false;
    public bool AngerPoolState = false;
    public bool BigGothSorrow = false;
    public bool BigGothFinal = false;
    public bool bigGothLove = false;
    public bool bigGothAnger = false;
    public bool smallGothFinal = false;
    public bool smallGothSorrow = false;
    public bool smallGothVengeance = false;
    public bool smallGothJealous = false;
    public bool StoppingFlag = false;
    bool IntroFlag = false;
    // Use this for initialization
    void Start()
    {
        Data = FindObjectOfType<DataScript>();
        //Set up dependencies
        //Open();
        if (Dialogue != null)
        {
            //From our text file we retrieve the text and we split it whenever there is a new line.
            Lines = (Dialogue.text.Split('\n'));

        }
        if (Stopper == 0)
        {
            Stopper = Lines.Length - 1;
        }

        if (Flag) //Activate or deactivate the dialogue box accordingly
        {
            Open();
        }
        else
        {
            Close();
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (!Flag)
        {
            //return;
        }

        if (LoveOption)
        {
            Data.HasLove = true;
        }
        if (AngerOption)
        {
            Data.HasAnger = true;
        }
        if (BigGothSorrow)
        {
            Data.HasSorrow = true;
        }
        if ((BigGothSorrow || bigGothAnger) && !StoppingFlag)
        {
            smallGothVengeance = true;
        }
        if (bigGothLove)
        {
            smallGothJealous = true;
        }
        if (CurrentLine == 87)
        {
            StoppingFlag = true;
            smallGothVengeance = false;
        }
        Text.text = Lines[CurrentLine];
        if (Input.GetKeyDown(KeyCode.Mouse0) && !AnswerTime && Flag)
        {
            if (CurrentLine == 0 && Fear1)
            {
                CurrentLine += 14;
            }
            else if (CurrentLine == 0 && Anger1)
            {
                CurrentLine += 19;
            }
            else if (CurrentLine == 0 && Final)
            {
                CurrentLine += 22;
            }
            else if (CurrentLine == 0 && Final2)
            {
                CurrentLine += 27;
            }
            else if (CurrentLine == 29 && PatronLove)
            {
                CurrentLine += 7;
            }
            else if (CurrentLine == 29 && PatronAnger)
            {
                CurrentLine += 14;
            }
            else if (CurrentLine == 29 && PatronFinal)
            {
                CurrentLine += 13;
            }
            else if (CurrentLine == 46 && FearPoolState)
            {
                CurrentLine += 4;
            }
            else if (CurrentLine == 46 && AngerPoolState)
            {
                CurrentLine += 7;
            }
            else if (CurrentLine == 57 && bigGothAnger)
            {
                CurrentLine += 14;
            }
            else if (CurrentLine == 57 && BigGothSorrow)
            {
                CurrentLine += 15;
            }
            else if (CurrentLine == 57 && bigGothLove)
            {
                CurrentLine += 16;
            }
            else if (CurrentLine == 57 && BigGothFinal)
            {
                CurrentLine += 17;
            }
            else if (CurrentLine == 80)
            {
                Close();
                smallGothFinal = true;
            }
            else if (CurrentLine == 87)
            {
                smallGothSorrow = true;
                Close();
            }
            else if (CurrentLine == 75 && smallGothFinal)
            {
                CurrentLine += 6;
            }
            else if (CurrentLine == 75 && smallGothVengeance)
            {
                CurrentLine += 7;
            }
            else if (CurrentLine == 75 && smallGothSorrow)
            {
                CurrentLine += 13;
            }
            else if (CurrentLine == 75 && smallGothJealous)
            {
                CurrentLine += 14;
            }
            else if (CurrentLine == 13 || CurrentLine == 21 || CurrentLine == 27 || CurrentLine == 28 || CurrentLine == 34 || CurrentLine == 35 || CurrentLine == 39 || CurrentLine == 43 || CurrentLine == 44 || CurrentLine == 62 || CurrentLine == 69 || CurrentLine == 70 || CurrentLine == 71 || CurrentLine == 72 || CurrentLine == 73 || CurrentLine == 81 || CurrentLine == 87 || CurrentLine == 88 || CurrentLine == 91 || CurrentLine == 153 || CurrentLine == 159 || CurrentLine == 164 || CurrentLine == 172 || CurrentLine == 183 || CurrentLine == 196 || CurrentLine == 204 || CurrentLine == 214 || CurrentLine == 226 || CurrentLine == 238 || CurrentLine == 241 || CurrentLine == 252 || CurrentLine == 256 || CurrentLine == 264 || CurrentLine == 279 || CurrentLine == 287 || CurrentLine == 295 || CurrentLine == 302 || CurrentLine == 346 || CurrentLine == 316 || CurrentLine == 327 || CurrentLine == 338)
            {
                Close();
            }
            else if (CurrentLine == 126)
            {
                CurrentLine += 4;
            }
            else if (CurrentLine == 127)
            {
                CurrentLine += 5;
            }
            else if (CurrentLine == 128)
            {
                CurrentLine += 5;
            }
            else if (CurrentLine == 129)
            {
                CurrentLine += 6;
            }
            else if (CurrentLine == 134)
            {
                CurrentLine += 20;
            }
            else if (CurrentLine == 135)
            {
                CurrentLine += 30;
            }
            else if (CurrentLine == 136)
            {
                CurrentLine += 37;
            }
            else if (CurrentLine == 185)
            {
                CurrentLine += 20;
            }
            else if (CurrentLine == 186)
            {
                CurrentLine += 29;
            }
            else if (CurrentLine == 227)
            {
                CurrentLine += 12;
            }
            else if (CurrentLine == 228)
            {
                CurrentLine += 14;
            }
            else if (CurrentLine == 229)
            {
                CurrentLine += 28;
            }
            else if (CurrentLine == 265)
            {
                CurrentLine += 15;
            }
            else if (CurrentLine == 266)
            {
                CurrentLine += 22;
            }
            else if (CurrentLine == 268)
            {
                CurrentLine += 28;
            }
            else if (CurrentLine == 303)
            {
                CurrentLine += 4;
            }
            else if (CurrentLine == 304)
            {
                CurrentLine += 13;
            }
            else if (CurrentLine == 305)
            {
                CurrentLine += 23;
            }
            else if (CurrentLine == 306)
            {
                CurrentLine += 33;
            }
            else
            {
                CurrentLine += 1;
            }
        }
        if (CurrentLine == 21)
        {
            //Final = false;
            //Final2 = true;
            ToScene2.SetActive(true);
            Close();
        }
        if (CurrentLine == 27)
        {
            Final = false;
            Final2 = true;
            ToScene2.SetActive(true);
            Close();
        }
        if (CurrentLine == 1)
        {

            FearAnswer.SetActive(true);
            if (AngerOption)
            {
                AngerAnswer.SetActive(true);
            }
            VanQuestion.SetActive(true);
            if (LoveOption)
            {
                LoveAnswer.SetActive(true);
            }
            AnswerTime = true;
        }
        else
        {
            VanQuestion.SetActive(false);
            //AngerAnswer.SetActive(false);
            //FearAnswer.SetActive(false);
            //AnswerTime = false;
        }
        if (CurrentLine == 4)
        {
            FearAnswer.SetActive(true);
            if (AngerOption)
            {
                AngerAnswer.SetActive(true);
            }
            NothingThanks.SetActive(true);
            AnswerTime = true;
        }
        else
        {
            //FearAnswer.SetActive(false);
            //AngerAnswer.SetActive(false);
            NothingThanks.SetActive(false);
        }
        if (CurrentLine == 14)
        {
            DoYouKnow.SetActive(true);
            if (AngerOption)
            {
                AngerAnswer.SetActive(true);
            }
            NothingAnswer.SetActive(true);
            AnswerTime = true;
        }
        else
        {
            DoYouKnow.SetActive(false);
            NothingAnswer.SetActive(false);
        }
        if (CurrentLine == 30)
        {
            KnockHisDrink.SetActive(true);
            SomethingSpecial.SetActive(true);
            AnswerTime = true;
        }
        else
        {
            SomethingSpecial.SetActive(false);
        }
        if (CurrentLine == 32)
        {
            Actually.SetActive(true);
            OhThankYou.SetActive(true);
            AnswerTime = true;
        }
        else
        {
            Actually.SetActive(false);
            OhThankYou.SetActive(false);
        }
        if (CurrentLine == 36)
        {
            KnockHisDrink.SetActive(true);
            DoYouKnowPatron.SetActive(true);
            AnswerTime = true;
        }
        else
        {
            DoYouKnowPatron.SetActive(false);
        }
        if (CurrentLine == 37)
        {
            WalkAway.SetActive(true);
            PatronFearAnswer.SetActive(true);
            AnswerTime = true;
        }
        else
        {
            WalkAway.SetActive(false);
            PatronFearAnswer.SetActive(false);
        }
        if (CurrentLine == 42)
        {
            KnockHisDrink.SetActive(true);
            WalkAway.SetActive(true);
            AnswerTime = true;
        }
        if (CurrentLine == 47 || CurrentLine == 50)
        {
            AngerPool.SetActive(true);
            FearPool.SetActive(true);
            AnswerTime = true;
        }
        if (CurrentLine == 60)
        {
            BigGothFear.SetActive(true);
            if (Data.HasAnger)
            {
                BigGothAnger.SetActive(true);
            }
            if (Data.HasLove)
            {
                BigGothLove.SetActive(true);
            }
            AnswerTime = true;
        }
        else
        {
            BigGothFear.SetActive(false);
            BigGothAnger.SetActive(false);
            BigGothLove.SetActive(false);
        }
        if (CurrentLine == 62)
        {
            Steal.SetActive(true);
            Accident.SetActive(true);
            AnswerTime = true;
        }
        else
        {
            Steal.SetActive(false);
        }
        if (CurrentLine == 63)
        {
            Accident.SetActive(true);
            AnswerTime = true;
        }
        if (CurrentLine == 27)
        {
            ToScene2.SetActive(true);
        }
        if (CurrentLine == 122)
        {
            //CancelInvoke("IntroScript");
            //AnswerTime = false;
            Application.LoadLevel("Scene1");
        }
        if (CurrentLine == 87)
        {
            ToScene3.SetActive(true);
        }
        if (CurrentLine == 95)
        {
            StartButton.SetActive(false);
        }
        if (CurrentLine == 99)
        {
            Underwater.SetActive(false);
            Clyde.SetActive(true);
        }
        if (CurrentLine == 110)
        {
            Clyde.SetActive(false);
            Casino.SetActive(true);
        }
        if (CurrentLine == 112)
        {
            Clyde.SetActive(true);
            Casino.SetActive(false);
        }
        if (CurrentLine == 121)
        {
            Clyde.SetActive(false);
            BarDoor.SetActive(true);
        }
        if (CurrentLine == 123)
        {
            KnockKnock.SetActive(false);
            if (Data.HasAnger)
            {
                angerUnlocked = true;
            }
            if (Data.HasLove)
            {
                loveUnlocked = true;
            }
            if (Data.HasSorrow)
            {
                sorrowUnlocked = true;
            }
        }
        if (CurrentLine == 125)
        {
            AnswerTime = true;
            textAnswer1.SetActive(true);
            ffearAnswer.SetActive(true);
            SelkieAndThief.SetActive(true);
            if (angerUnlocked)
            {
                fangerAnswer.SetActive(true);
            }
            if (loveUnlocked)
            {
                loveAnswer.SetActive(true);
            }
            if (sorrowUnlocked)
            {
                sorrowAnswer.SetActive(true);
            }
        }
        else if (CurrentLine == 129)
        {
            AnswerTime = true;
            textAnswer2.SetActive(true);
            ffearAnswer.SetActive(true);
            if (angerUnlocked)
            {
                fangerAnswer.SetActive(true);
            }
            if (loveUnlocked)
            {
                loveAnswer.SetActive(true);
            }
            if (sorrowUnlocked)
            {
                sorrowAnswer.SetActive(true);
            }
        }
        else if (CurrentLine == 130)
        {
            AnswerTime = true;
            textAnswer3.SetActive(true);
            if (angerUnlocked)
            {
                fangerAnswer.SetActive(true);
            }
            if (loveUnlocked)
            {
                loveAnswer.SetActive(true);
            }
            if (sorrowUnlocked)
            {
                sorrowAnswer.SetActive(true);
            }
        }
        else if (CurrentLine == 131)
        {
            AnswerTime = true;
            textAnswer4.SetActive(true);
            ffearAnswer.SetActive(true);
            if (angerUnlocked)
            {
                fangerAnswer.SetActive(false);
            }
            if (loveUnlocked)
            {
                loveAnswer.SetActive(true);
            }
            if (sorrowUnlocked)
            {
                sorrowAnswer.SetActive(true);
            }
        }
        else if (CurrentLine == 132)
        {
            AnswerTime = true;
            textAnswer5.SetActive(true);
            ffearAnswer.SetActive(true);
            if (angerUnlocked)
            {
                fangerAnswer.SetActive(true);
            }
            if (loveUnlocked)
            {
                loveAnswer.SetActive(false);
            }
            if (sorrowUnlocked)
            {
                sorrowAnswer.SetActive(true);
            }
        }
        //else if (CurrentLine == 10)
        //{
        //    AnswerTime = true;
        //    textAnswer5.SetActive(true);
        //    fearAnswer.SetActive(true);
        //    if (angerUnlocked)
        //    {
        //        angerAnswer.SetActive(true);
        //    }
        //    if (loveUnlocked)
        //    {
        //        loveAnswer.SetActive(true);
        //    }
        //    if (sorrowUnlocked)
        //    {
        //        sorrowAnswer.SetActive(false);
        //    }
        //}
        else if (CurrentLine == 133)
        {
            AnswerTime = true;
            textAnswer6.SetActive(true);
            ffearAnswer.SetActive(true);
            if (angerUnlocked)
            {
                fangerAnswer.SetActive(true);
            }
            if (loveUnlocked)
            {
                loveAnswer.SetActive(true);
            }
            if (sorrowUnlocked)
            {
                sorrowAnswer.SetActive(false);
            }

        }
        else if (CurrentLine == 212)
        {
            AnswerTime = true;
            textAnswer7.SetActive(true);
            textAnswer8.SetActive(true);
        }
        else
        {
            textAnswer1.SetActive(false);
            textAnswer2.SetActive(false);
            textAnswer3.SetActive(false);
            textAnswer4.SetActive(false);
            textAnswer5.SetActive(false);
            textAnswer6.SetActive(false);
            textAnswer7.SetActive(false);
            textAnswer8.SetActive(false);
            ffearAnswer.SetActive(false);
            fangerAnswer.SetActive(false);
            loveAnswer.SetActive(false);
            sorrowAnswer.SetActive(false);
        }
        if (CurrentLine == Stopper)
        {
            Close();
        }
    }

    /*public void IntroScript()
    {
        int x = 0;
        do
        {
            CurrentLine += 1;
            x++;
        }
        while (x < 1);
                }*/
    public void Open()
    {
        DialogueBox.SetActive(true);
        Flag = true;
    }
    public void Close()
    {
        DialogueBox.SetActive(false);
        //CurrentLine = 0;
        //Stopper = 0;
        Flag = false;
    }
    public void ReloadScript(TextAsset Text)
    {
        if (Text != null)
        {
            Lines = new string[1];
            Lines = (Text.text.Split('\n'));
        }
    }
}