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

public class execE : MonoBehaviour {
    string save = "Assets/createEntryCode/save.txt";
    string[] currentFiles = {"Assets/createEntryCode/Lobby.txt", "Assets/createEntryCode/BreakRoom.txt" }; // append more files as we go down
    int[] threatLevel = { -1, -1, -1, -1, -1 };
    AudioClip[] clips = new AudioClip[2];
    Dictionary<string, rE> currentDictionary;
    Canvas cV;
    string ourText = "", pastText = "";
    static mC character;
    static rE currentObj;
    string allText = "";
    void Start()
    {
        RandomMize();
        /*for(int i = 0; i < threatLevel.Length; i++)
            Debug.Log("threatLevel:" + threatLevel[i]);*/
        character = readE.initialize();
        Debug.Log(character.currRoom);
        cV = GameObject.Find("Canvas").GetComponent<Canvas>();
        StreamReader s = new StreamReader(character.currRoom);
        int pos = System.Array.IndexOf(currentFiles,character.currRoom);
        Debug.Log("pos:"+pos);
        int truePos = threatLevel[pos];
        Debug.Log("truePos:"+truePos);
        currentDictionary = readE.readText(s,truePos);
        currentObj = currentDictionary["Room1"];
        Debug.Log(currentDictionary["Room1"]);
        accessDesc("Room1");

    }
    void RandomMize() {
        System.Random r = new System.Random();
        int e = r.Next(0, 4);
        Debug.Log("Random:" + e);
        threatLevel[e] = 5;
        int i = e, j = 4;
        e -= 1;
        while (e >= 0) {
            threatLevel[e] = j;
            e--;
            j--;
        }
        i++;
        j = 4;
        while (i < 5) {
            threatLevel[i] = j;
            i++;
            j--;
        }
    }
    void Sleep() {
        FileStream f = new FileStream(save, FileMode.Create);
        StreamWriter sW = new StreamWriter(f);
        character.Sanity += 1+currentObj.amSanity;
        character.choicesLeft = 8;
        character.threatLvl = threatLevel;
        string storeThreatLvl = "";
        string storePrevrooms = "";
        for (int i = 0; i < character.prevRoomC; i++)
        {
            Debug.Log("character.prevRooms[i]: "+character.prevRooms[i]);
            storePrevrooms += character.prevRooms[i] + " ";
        }
        for (int j = 0; j < character.threatLvl.Length; j++)
            storeThreatLvl += character.threatLvl[j] + " ";
        string evTot = character.Sanity+" [ "+storePrevrooms+"] "
            +"[ "+storeThreatLvl+"] "+" "+character.currRoom+
            " "+character.choicesLeft+" "+character.prevRoomC;
        sW.WriteLine(evTot);
        sW.Close();
    }
    void accessDesc(string s) {
        Debug.Log(currentDictionary[s].descCounter);
        if (allText != "\n")
            allText += "\n";
        for (int i = 0; i < currentDictionary[s].descCounter; i++)
            allText += currentDictionary[s].desc[i] + "\n"; //Description shit
        Debug.Log("All Text: " + allText);
    }
    // Update is called once per frame
    void Update() {
        //Debug.Log("ourText:"+ourText);
        /*Debug.Log("currentObj.amSanity: " + currentObj.amSanity);
        Debug.Log("character.Sanity: "+character.Sanity);
        Debug.Log("character.ChoicesLeft: "+character.choicesLeft);
        Debug.Log("character.prevRooms[0]: "+ character.prevRooms[0]);*/
        if (character.Sanity <= 0) {
            pastText = "";
            allText = "All has been lost.";
            Application.Quit();
        }
        foreach (char c in Input.inputString)
        {
            if (c == "\b"[0] && ourText.Length >= 0)
                if (ourText.Length > 1)
                    ourText = ourText.Substring(0, ourText.Length - 1);
                else
                    ourText = "";
            else if (Input.GetKeyDown(KeyCode.Return)) {
                int s = 0;
                if (ourText.Equals("Load"))
                {
                    character = readE.returnCharacter(save);
                    threatLevel = character.threatLvl;
                }
                else if (ourText.Equals("Sleep"))
                {
                    RandomMize();
                    Sleep();
                }
                else {
                    while (ourText != currentObj.isChoice[s] && s < currentObj.choiceCounter)
                        s++;
                    Debug.Log("s: " + s);
                    Debug.Log("currentObj.choiceCounter: " +currentObj.choiceCounter);
                    if (s <= currentObj.choiceCounter && ourText == currentObj.isChoice[s])
                    {
                        character.Sanity += currentDictionary[currentObj.isChoice[s]].amSanity;
                        currentDictionary[currentObj.isChoice[s]].amSanity = 0; //Check in with Stephen on this
                        string e = currentObj.isChoice[s];
                        if (pastText == "")
                            pastText = allText;
                        allText = "";
                        accessDesc(e);
                        string[] splitText = ourText.Split(' ');
                        /*Debug.Log("splitText[0]:" + splitText[0]);
                        Debug.Log("splitText[2]:" + splitText[2]);*/
                        if (splitText.Length > 2) {
                            if (splitText[0] == "Open" && splitText[2] == "Door") {
                                Debug.Log("---------------Inner Circle---------------");
                                string saveLink = currentDictionary[ourText].isLink;
                                Debug.Log("saveLink:" + saveLink);
                                string path = Path.Combine("Assets/createEntryCode/", currentDictionary[ourText].currRoom);
                                Debug.Log("Path:" + path);
                                StreamReader y = new StreamReader(path);
                                character.currRoom = path;
                                int pos = System.Array.IndexOf(currentFiles, path);
                                int truePos = threatLevel[pos];
                                currentDictionary = readE.readText(y, truePos);
                                currentObj = currentDictionary[saveLink];
                                accessDesc(saveLink);
                                pastText = "";
                            }
                        }
                    }
                    if (character.choicesLeft >= 0)
                        character.choicesLeft--;
                    else
                        character.Sanity--;
                }
                ourText = "";
            }
            else {
                ourText += c;
            }
        }
        //Debug.Log(allText);
        //Debug.Log(ourText);
    }
    void OnGUI() {
        //Debug.Log("currentObj.pictureName: "+currentObj.pictureName);
        //Texture place = (Texture) UnityEditor.AssetDatabase.LoadAssetAtPath(currentObj.pictureName,typeof(Texture));
        //GUI.DrawTexture(new Rect(0, 0, cV.pixelRect.width, cV.pixelRect.height),place);
        GUI.color = Color.green;
        GUI.Label(new Rect(0, 0, cV.pixelRect.width, cV.pixelRect.height), pastText+"\n"+allText+"\n"+ ourText);
        //Debug.Log("WIDTH: "+cV.pixelRect.width);
        //Debug.Log("HEIGHT: " + cV.pixelRect.height);
    }
}
