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

public class DayCycle : MonoBehaviour {
    public int dayCount = 0;
    public int actionPointMax = 5;
    public int actionPoints = 0;
    // Start is called before the first frame update
    void Start() {
        NewDay();
    }

    // Update is called once per frame
    void Update() {

    }

    public void NewDay() {
        dayCount++;
        actionPoints = actionPointMax;
        List<string> script = GenerateScript();
        GameManager.instance.dialog.QueueDialog(script);
    }

    public void ChangeActionPoints(int i) {
        actionPoints -= i;
    }

    public List<string> GenerateScript() {

        int startlvl = GameManager.instance.devianceController.level;
        int startdev = GameManager.instance.devianceController.deviance;

        List<string> dayScript = new List<string>();
        if (dayCount == 1) {
            for (int x = 0; x < 2; x++) {
                int rand = new System.Random().Next(genericScripts.Count);
                dayScript.AddRange(ReadScript(genericScripts[rand]));
            }
            dayScript.AddRange(ReadScript(edric1));

            dayScript.Add("say|Me|Time to do some chores");
            dayScript.Add("choice|Wipe the tables|Do the dishes|Sweep the floor|table wipe|do dishes|sweep floor");

            dayScript.Add("label|table wipe");
            dayScript.AddRange(ReadScript(tableWipeScriptName));
            dayScript.Add("jump|continue");

            dayScript.Add("label|do dishes");
            dayScript.AddRange(ReadScript(doDishesScriptName));
            dayScript.Add("jump|continue");

            dayScript.Add("label|sweep floor");
            dayScript.AddRange(ReadScript(sweepFloorScriptName));

            dayScript.Add("label|continue");
        }
        else {
            List<string> todayChar = new List<string>();
            todayChar.AddRange(GameManager.instance.dialog.characterGenerator.allCharacters);

            if (dayCount == 3 && GameManager.instance.devianceController.level >= 3) {
                dayScript.AddRange(ReadScript(gameover));
            }
            //Visitors
            bool lorrainAttempt = false;
            for (int x = 0; x < 3; x++) {
                //Generic


                if (new System.Random().Next(3) == 0) {

                    if (!Have("slime book") && new System.Random().Next(3) == 0 && !lorrainAttempt) {
                        dayScript.AddRange(ReadScript(lorraine));
                        lorrainAttempt = true;
                    }

                    else if (GameManager.instance.devianceController.level >= 1 && new System.Random().Next(8) == 0) {
                        int rand2 = new System.Random().Next(devFoodScripts.Count);
                        dayScript.AddRange(ReadScript(devFoodScripts[rand2]));
                    }
                    else {
                        int rand = new System.Random().Next(genericScripts.Count);
                        dayScript.AddRange(ReadScript(genericScripts[rand]));
                    }
                }
                else { //Special
                    int idx = new System.Random().Next(todayChar.Count);
                    string character = todayChar[idx];
                    switch (character) {
                        case "sennal":
                            if (Have("sennal1")) {
                                dayScript.AddRange(ReadScript(sennal2));
                                GameManager.instance.dialog.characterGenerator.allCharacters.Remove(character);
                            }
                            else {
                                dayScript.AddRange(ReadScript(sennal1));
                            }
                            break;
                        case "rijan":
                            if (Have("rijan3")) {
                                dayScript.AddRange(ReadScript(rijan4));
                                GameManager.instance.dialog.characterGenerator.allCharacters.Remove(character);
                            }
                            else if (Have("rijan2")) {
                                dayScript.AddRange(ReadScript(rijan3));
                            }
                            else if (Have("rijan1")) {
                                dayScript.AddRange(ReadScript(rijan2));
                            }
                            else {
                                dayScript.AddRange(ReadScript(rijan1));
                            }
                            break;
                        case "edric":
                            if (Have("edric1")) {
                                dayScript.AddRange(ReadScript(edric2));
                                GameManager.instance.dialog.characterGenerator.allCharacters.Remove(character);
                            }
                            else {
                                dayScript.AddRange(ReadScript(edric1));
                            }
                            break;
                        case "mug":
                            if (GameManager.instance.devianceController.level >= 3) {
                                dayScript.AddRange(ReadScript(mugscript));
                                break;
                            }
                            continue;
                        case "horny bard":
                            dayScript.AddRange(ReadScript(hornybard));
                            break;
                        case "rochelle":
                            if (Have("slime1") && !Have("Rochelle1")) {
                                dayScript.AddRange(ReadScript(rochelle1));
                            }
                            break;
                        default:
                            int rand = new System.Random().Next(genericScripts.Count);
                            dayScript.AddRange(ReadScript(genericScripts[rand]));
                            break;
                    }
                    todayChar.Remove(character);
                }
            }

            dayScript.Add("say|Me|Time to do some chores");

            string baseChoices = "Wipe the tables|Do the dishes|Sweep the floor|[slip of paper]Try summoning";
            string baseLabels = "table wipe|do dishes|sweep floor|summoning";

            if (GameManager.instance.devianceController.level >= 2 && new System.Random().Next(3) == 0) {
                baseChoices += "|Try sorcery";
                baseLabels += "|sorcery";
            }

            if (Have("slime book") && !Have("slime1")) {
                baseChoices += "|Read book";
                baseLabels += "|slimebook";
            }

            if (GameManager.instance.devianceController.level >= 1 && new System.Random().Next(3) == 0) {
                baseChoices += "|Do nothing";
                baseLabels += "|nothing";
            }

            if (GameManager.instance.devianceController.level >= 2 && new System.Random().Next(3) == 0) {
                baseChoices += "|Learn some martial arts";
                baseLabels += "|martialarts";
            }

            if (GameManager.instance.devianceController.level >= 1 && new System.Random().Next(3) == 0) {
                baseChoices += "|Carve some wood";
                baseLabels += "|carve";
            }

            if (GameManager.instance.devianceController.level >= 4 && new System.Random().Next(10) <= 4) {
                baseChoices += "|Leave";
                baseLabels += "|leave";
            }

            dayScript.Add($"choice|{baseChoices}|{baseLabels}");

            //table stuff
            dayScript.Add("label|table wipe");
            dayScript.AddRange(ReadScript(tableWipeScriptName));
            dayScript.Add("jump|continue");

            //dishes stuff
            dayScript.Add("label|do dishes");
            dayScript.AddRange(ReadScript(doDishesScriptName));
            dayScript.Add("jump|continue");

            //floor stuff
            dayScript.Add("label|sweep floor");
            dayScript.AddRange(ReadScript(sweepFloorScriptName));
            if (!Have("slip of paper") && !Have("did summon") && new System.Random().Next(4) <= 1) {
                dayScript.AddRange(ReadScript(findSlip));
            }
            dayScript.Add("jump|continue");

            //special actions
            dayScript.Add("label|summoning");
            dayScript.AddRange(ReadScript(summoningScriptName));
            dayScript.Add("jump|continue");

            dayScript.Add("label|sorcery");
            dayScript.AddRange(ReadScript(sorcery));
            dayScript.Add("jump|continue");

            dayScript.Add("label|slimebook");
            dayScript.AddRange(ReadScript(readbook));
            dayScript.Add("jump|continue");

            dayScript.Add("label|nothing");
            dayScript.AddRange(ReadScript(donothing));
            dayScript.Add("jump|continue");

            dayScript.Add("label|martialarts");
            dayScript.AddRange(ReadScript(martialarts));
            dayScript.Add("jump|continue");

            dayScript.Add("label|carve");
            dayScript.AddRange(ReadScript(carve));
            dayScript.Add("jump|continue");

            dayScript.Add("label|leave");
            dayScript.AddRange(ReadScript(endgame));
            dayScript.Add("jump|continue");

            dayScript.Add("label|continue");

            if (GameManager.instance.devianceController.level >= 1 && new System.Random().Next(8) == 0) {
                dayScript.AddRange(ReadScript(thighs));
            }
        }

        if (GameManager.instance.devianceController.level >= 4 && new System.Random().Next(10) <= 4) {
            dayScript.AddRange(ReadScript(daylvl1));
        }

        //day end
        if (GameManager.instance.devianceController.level != startlvl) {
            if (GameManager.instance.devianceController.level == 1) {
                dayScript.AddRange(ReadScript(daylvl1));
            }
            else if (GameManager.instance.devianceController.level == 2) {
                dayScript.AddRange(ReadScript(daylvl2));
            }
            else if(GameManager.instance.devianceController.level == 3) {
                dayScript.AddRange(ReadScript(daylvl3));
            }
        }
        else if (GameManager.instance.devianceController.deviance != startdev) {
            dayScript.AddRange(ReadScript(daydev));
        }
        else {
            dayScript.AddRange(ReadScript(dayend));
        }

        return dayScript;

    }

    public bool Have(string item) {
        return GameManager.instance.inventory.Contains(item);
    }

    public List<string> ReadScript(string scriptName) {
        StreamReader stream = new StreamReader(Application.dataPath + "/Resources/Dialog/" + scriptName + ".txt");
        string contents = stream.ReadToEnd();
        stream.Close();
        string[] lines = contents.Split('\n');

        List<string> lineList = new List<string>();
        foreach (var line in lines) {
            lineList.Add(line.Trim());
        }
        return lineList;
    }

    List<string> genericScripts = new List<string>() { "male drink 1", "male drink 2", "male drink 3", "male drink 4", "female drink 1" , "female drink 2" , "female drink 3" , "female drink 4",
    "male food 1", "male food 2", "male food 3", "male food 4", "female food 1", "female food 2", "female food 3", "female food 4"};

    List<string> devFoodScripts = new List<string>() {
        "male food 1 dev",
        "male food 2 dev",
        "male food 3 dev",
        "male food 4 dev",
        "female food 1 dev",
        "female food 2 dev",
        "female food 3 dev",
        "female food 4 dev"
    };

    string tableWipeScriptName = "table wipe";
    string sweepFloorScriptName = "sweep floor";
    string doDishesScriptName = "do dishes";
    string summoningScriptName = "Try summoning";
    string sorcery = "Try some sorcery";
    string findSlip = "get slip";
    string sennal1 = "Sennal1";
    string sennal2 = "Sennal2";
    string rijan1 = "rijan 1";
    string rijan2 = "rijan 2";
    string rijan3 = "rijan 3";
    string rijan4 = "rijan 4";
    string mugscript = "drink orders human";
    string gameover = "speedrun";
    string lorraine = "Lorraine";
    string thighs = "thigh mold";
    string readbook = "read book";
    string donothing = "Do nothing";
    string edric1 = "Edric1";
    string edric2 = "Edric2";
    string hornybard = "horny bard drink";
    string martialarts = "Learn some martial arts";
    string carve = "carve some wood";
    string dayend = "day end no dev";
    string daydev = "day end plus dev";
    string daylvl1 = "day end dev lvlup 1";
    string daylvl2 = "day end dev lvlup 2";
    string daylvl3 = "day end dev lvlup 3";
    string endgame = "end game";
    string rochelle1 = "rochelle1";
}
