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

public class rE
{
    public string pictureName, currRoom;
    public string[] desc, isChoice;
    public int descCounter, choiceCounter, amSanity; //How high does our description counter go
    public string isLink;
    public rE(int amSanity, string pictureName, string[] isChoice, string[] desc, int dC, int cC, string cR, string link = "")
    {
        this.amSanity = amSanity;
        this.pictureName = pictureName;
        this.isChoice = isChoice;
        this.desc = desc;
        this.descCounter = dC;
        this.choiceCounter = cC;
        this.currRoom = cR;
        this.isLink = link;
    }
}

public class mC
{
    public int Sanity,choicesLeft;
    public string[] prevRooms;
    public int[] threatLvl;
    public string currRoom;
    public int prevRoomC;
    public mC()
    {
        this.Sanity = 10;
        this.prevRooms = new string[5];
        this.threatLvl = new int[5];
        this.currRoom = "Assets/createEntryCode/Lobby.txt";
        this.choicesLeft = 8;
        this.prevRoomC = 0;
    }
    public mC(int sane = 10, string[] pR = null, int[] threatLvl = null, string cR = null, int cL = 8, int pRC = 0)
    {
        this.Sanity = sane;
        this.prevRooms = pR;
        this.threatLvl = threatLvl;
        this.currRoom = cR;
        this.choicesLeft = cL;
        this.prevRoomC = pRC;
    }
}

public struct storeStuff {
    public string tag, pictureName, currRoom;
    public int descCounter, choiceCounter, amSanity; //How high does our description counter go
    public string isLink;
}

public class readE : MonoBehaviour
{
    // Going into rooms w/ higher levels of insanity is a big problem
    static storeStuff vals;
    public static string[] desc = new string[50], isChoice = new string[50];
    public static Dictionary<string,rE> readText(StreamReader s, int e)
    {
        Dictionary<string, rE> readChoices = new Dictionary<string, rE>();
        Debug.Log("ENTER");
        string totString = s.ReadLine();
        string tag = "";
        while (totString != null)
        {
            Debug.Log("This is your totString right now: " + totString);
            string[] breakString = totString.Split(' ');
            if (totString == "BREAK") {
                readChoices.Add(tag, new rE(vals.amSanity,vals.pictureName,isChoice,desc,vals.descCounter,vals.choiceCounter,vals.currRoom, vals.isLink));
                vals = new storeStuff();
                desc = new string[50];
                isChoice = new string[50];
            }
            else if (breakString[0] == "--")
                vals.currRoom = breakString[1];
            else if (breakString[0] == "->") {
                //Debug.Log("totString:"+totString);
                tag = "";
                for (int i = 1; i < breakString.Length; i++) {
                    tag += breakString[i];
                    if (i + 1 < breakString.Length)
                        tag += " ";
                }
                //Debug.Log("tag:" + tag);
            }
            else if (breakString[0] == ">") {
                string combineString = "";
                for (int i = 1; i < breakString.Length; i++) {
                    combineString += breakString[i];
                    if (i + 1 < breakString.Length)
                        combineString += " ";
                }
                isChoice[vals.choiceCounter] = combineString;
                vals.choiceCounter++;
            }
            else if (breakString[0] == "*")
            {
                vals.amSanity = Convert.ToInt32(breakString[1]);
                if (breakString.Length > 2) {
                    vals.isLink = breakString[2];
                }
            }
            else if (breakString[0] == "=>")
            {
                vals.pictureName = breakString[1];
            }
            else
            {
                int cBreak = 0;
                //Debug.Log("totstring:" + totString);
                //Debug.Log("breakString[0]" + breakString[0]);
                if (breakString[0] == "lvl") {
                    cBreak = Convert.ToInt32(breakString[1]);
                    //Debug.Log("cBreak: "+ cBreak);
                    //Debug.Log("e: " + e);
                    if (cBreak == e) {
                        while (true) {
                            //Debug.Log("PrevTotString: " + totString);
                            totString = s.ReadLine();
                            //Debug.Log("CurrTotString: " + totString);
                            breakString = totString.Split(' ');
                            //Debug.Log("breakString[0]" + breakString[0]);
                            if ((totString.Equals("==")) || (totString.Equals("==="))) {
                                //Debug.Log("Getting out of the Loop");
                                while (!(totString.Equals("==="))) {
                                    totString = s.ReadLine();
                                }
                                break;
                            }
                            //Debug.Log("vals.descCounter:"+vals.descCounter);
                            desc[vals.descCounter] = totString;
                            vals.descCounter++;
                        }
                        //Debug.Log("Out of the loop");
                    }
                    else {
                        //Debug.Log("e:" + e);
                        while (cBreak != e) {
                            totString = s.ReadLine();
                            breakString = totString.Split(' ');
                            if (breakString[0] == "lvl") {
                                cBreak = Convert.ToInt32(breakString[1]);
                                //Debug.Log("totString:" + totString);
                            }
                        }
                        while (true) {
                            totString = s.ReadLine();
                            breakString = totString.Split(' ');
                            if ((totString.Equals("==")) || (totString.Equals("===")))
                            {
                                //Debug.Log("Getting out of the Loop");
                                while (!(totString.Equals("==="))) {
                                    totString = s.ReadLine();
                                }
                                break;
                            }
                            desc[vals.descCounter] = totString;
                            vals.descCounter++;
                        }
                    }
                    vals.amSanity -= cBreak;
                }
                else {
                    desc[vals.descCounter] = totString;
                    vals.descCounter++;
                }
            }
            totString = s.ReadLine();
        }
        return readChoices;
    }
    public static mC returnCharacter(string read)
    {
        //Debug.Log("============================================RETURNCHAR");
        StreamReader c = new StreamReader(read);
        string totString2 = c.ReadLine();
        string[] breakString = totString2.Split(' ');
        string[] breakTot = new string[5];
        int[] threatLvl = new int[5];
        int i = 2, j = 0;
        while (breakString[i] != "]") {
            //Debug.Log("breakString[i]: " + breakString[i]);
            breakTot[j] = breakString[i];
            i++;
        }
        j = 0;
        i+=2;
        while (breakString[i] != "]") {
            //Debug.Log("Threat Level Decoded: " +breakString[i]);
            threatLvl[j] = Convert.ToInt32(breakString[i]);
            //Debug.Log("threatLvl[j]: " + threatLvl[j]);
            i++;
        }
        //Debug.Log("Current item: " + breakString[i]);
        i+=2;
        //Debug.Log("TEMP1 item: " + breakString[i]);
        int temp1 = Convert.ToInt32(breakString[i+1]);
        int temp2 = Convert.ToInt32(breakString[i+2]);
        //More to this later
        mC retChar = new mC(Convert.ToInt32(breakString[0]), breakTot, threatLvl, breakString[i],temp1,temp2);
        retChar.prevRoomC = retChar.prevRooms.Length - 1;
        c.Close();
        //Debug.Log("============================================RETURNCHAREND");
        return retChar;
    }
    public static mC initialize(){
        mC character = new mC();
        character.prevRooms[character.prevRoomC] = "Asset/createEntryCode/Lobby.txt";
        character.prevRoomC++;
        return character;
    }
}
