using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class SimonsFileCreator : MonoBehaviour
{
    public static SimonsFileCreator instance;
    
    public enum TypeOfFile
    {
         city, office, bar
    }

    [Header("Diary Dialogue")]
    public TextAsset cityBlocks;
    public TextAsset theOffices;
    public TextAsset bar;
    

    [Header("Other Dialogue")]
    public List<string> cityBlocksList;
    public List<string> theOfficesList;
    public List<string> barList;
    

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
            return;
        }
    }
    
    void Start()
    {
        cityBlocksList = ReadLines(cityBlocks);
        theOfficesList = ReadLines(theOffices);
        barList = ReadLines(bar);
        
    }

    //Reads in text assets and puts strings in list and returns it
    private static List <string> ReadLines(TextAsset textAsset)
    {
        //string fullFilePath = Application.dataPath + "/Resources/TextFiles/" + fileName;
        // where data is a TextAsset
        List<String> tempStringList = new List<string>();
        string text = textAsset.text;

        StringBuilder builder = new StringBuilder();
        char newLine = '\n';
        int size = text.Length;

        for (int i = 0; i < size; ++i)
        {
            char ch = text[i];
            if (ch == newLine)
            {
                // do something with each line
                tempStringList.Add(builder.ToString());
                //Debug.Log(builder.ToString());
                builder.Clear();
                continue;
            }

            builder.Append(ch);
        }
        
        return tempStringList;
    }

    public string ChooseDialogueLine(TypeOfFile typeOfFile)
    {
        int rnd;
        switch (typeOfFile)
        {
            case TypeOfFile.city:
                rnd = Random.Range(0, cityBlocksList.Count);
                return cityBlocksList[rnd];
            case TypeOfFile.office:
                rnd = Random.Range(0, theOfficesList.Count);
                return theOfficesList[rnd];
            case TypeOfFile.bar:
                rnd = Random.Range(0, barList.Count);
                return barList[rnd];
        }
        return null;
    }

    
    
    /*public DialogueData PickDiaryEntry()
    {
        int rnd = Random.Range(0, diaryEntries.Count);
        int foundNum = -1;
        foundNum = _alreadyChoosenEntries.Find(x => x == rnd);
        if (foundNum != -1)
        {
            return diaryEntries[rnd];
            _alreadyChoosenEntries.Add(rnd);
        }
        else
        {
            return null;
        }
    }

    public DialogueData PickRandomDialogue(List<string> stringList, bool startDialogue = false)
    {
        int num = Random.Range(0, stringList.Count);
        DialogueData temp = ScriptableObject.CreateInstance(typeof(DialogueData)) as DialogueData;
        temp.conversationBlock = new List<string>();
        temp.conversationBlock.Add(stringList[num]);

        if (startDialogue)
        {
            dialogueManager.StartDialogue("Colonel", temp, DialogueManager.ImageBkg.General);
            return temp;
        }
        else
        {
            return temp; 
        }
    }
    
    //Picks a start and end from two lists and makes a full string
    //for a randome dialogue data object
    public DialogueData GenerateMeteorWarning(bool startDialogue = false)
    {
        int num1 = Random.Range(0, astWarningPreList.Count);
        int num2 = Random.Range(0, astWarningSufList.Count);
        
        DialogueData temp = ScriptableObject.CreateInstance(typeof(DialogueData)) as DialogueData;
        temp.conversationBlock = new List<string>();
        temp.conversationBlock.Add(astWarningPreList[num1]);
        temp.conversationBlock.Add(astWarningSufList[num2]);
        
        if (startDialogue)
        {
            dialogueManager.StartDialogue("Colonel", temp, DialogueManager.ImageBkg.General);
            return temp;
        }
        else
        {
            return temp; 
        }
    }

    public DialogueData GenerateStartDayDialoge(DialogueData objectiveDialogue, bool startDialogue = false)
    {
        DialogueData temp = ScriptableObject.CreateInstance(typeof(DialogueData)) as DialogueData;
        temp.conversationBlock = new List<string>();
        
        int num1 = Random.Range(0, dayStartList.Count);
        temp.conversationBlock.Add(dayStartList[num1]);
        
        foreach (string text in objectiveDialogue.conversationBlock)
        {
            temp.conversationBlock.Add(text);
        }
        
        if (startDialogue)
        {
            dialogueManager.StartDialogue("Colonel", temp, DialogueManager.ImageBkg.General);
            return temp;
        }
        else
        {
            return temp; 
        }
    }*/
}
