using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

public class NightsManager : MonoBehaviour
{

    public static NightsManager Instance;
    
    public static int nightNumber = 1;

    public static List<List<String>> nightStartMessages = new List<List<string>>();

    public GameObject alarmClock;
    public GameObject window;
    public GameObject phone;
    public GameObject thermometer;
    public GameObject heater;
    public GameObject monitor;

    public static Dictionary<int, String> medals = new Dictionary<int, string>();

    private void Awake()
    {
        Instance = this;

        nightStartMessages.Add(new List<string>()
        {
            "Seven Sleepy Nights\n\n" +
            "A game created from scratch in 48 hours for Ludum Dare 48 by Robert Clarke\n\n" +
            "Please use headphones for the best experience.",
            
            "Sunday Night",
            
            "This is Chuck  ---->\n" +
            "He has some kind of very important job in which he saves lives or something. \n" +
            "It is vital that he gets eight hours of deep restful sleep each night " +
            "and wakes up refreshed at 07:00, ready to start the day. \n" +
            "Your job is to help him sleep by taking care of all the distractions in his bedroom.",
            
            "The Alarm Clock\n" +
            "\n" +
            "Chuck keeps an old-fashioned mechanical alarm clock on his bedside table because " +
            "he thinks it's retro-chic.\n" +
            "He has it set to go off at his usual waking time of 0700, " + 
            "Unfortunately, the mechanism is old and it winds down too quickly and unpredictably.\n" +
            "It starts to tick faster when it is close to going off, you will have to listen for " +
            "this cue and re-wind the clock. (Click and drag the winder on the back of the clock).",
            
            "The Window\n" +
            "\n" +
            "In the morning the sun will rise and fill the room with light, so the curtain " +
            "needs to be closed. Unfortunately Chuck's curtain pole is mounted at an angle " +
            "so the curtain drifts open again a while after being closed.",
            
            "Oversleeping\n" +
            "\n" +
            "Remember, chuck needs to wake up by 0700. Sleeping later than that will lose " +
            "rather than gain score. If you overwind the alarm clock you will have to wake him " +
            "another way."
        });
        
        nightStartMessages.Add(new List<string>()
        {
            "Monday Night",
            
            "The Phone\n\n" +
            "Chuck decided to bring his phone to bed with him tonight. " +
            "Chuck is a very popular man, so he gets calls and notifications all through the " +
            "night. The phone ringing will wake him, but thankfully the screen " +
            "turns on a few seconds before the ringing starts. " +
            "Use this cue to intercept the phone calls and reject " +
            "them before Chuck wakes up.\n" +
            "The screen also lights up when a notification comes in, and the phone also buzzes. " +
            "If you hear the buzz you can ignore the phone."
        });
        
        nightStartMessages.Add(new List<string>()
        {
            "Tuesday Night",
            
            "The Light\n" +
            "\n" +
            "The family at the house opposite Chuck's have installed a powerful motion-activated " +
            "outdoor light to deter burglars. Unfortunately, it's a bit too sensitive " +
            "and turns on at the slightest provocation. Thankfully, you can hear the circuit switch on " +
            "shortly before the light turns on. Close the curtain before the light disturbs Chuck."
        });
        
        nightStartMessages.Add(new List<string>()
        {
            "Wednesday Night",
            
            "Tonight is the same as last night. Maybe tomorrow night Chuck will turn his phone to silent? " +
            "Or maybe even use it as an alarm instead of a 60 year old broken piece of plastic?"
        });
        
        nightStartMessages.Add(new List<string>()
        {
            "Thursday Night",
            
            "Temperature\n" +
            "\n" +
            "The nights have been getting colder recently, so chuck decided " +
            "to bring out the electric heater. Try to keep the thermometer in "  +
            "the green range. You can increase the temperature by switching on the " +
            "heater, or quickly decrease it by opening the sliding window."
        });
        
        nightStartMessages.Add(new List<string>()
        {
            "Friday Night",
            
            "Chuck doesn't get Saturdays off, because he never joined a union while " +
            "he had the chance."
        });
        
        nightStartMessages.Add(new List<string>()
        {
            "Saturday Night",
            
            "Chuck always gets up at 0700 on Sunday morning, because he is a good God-fearing man.",
            
            "The Monitor\n\n" + 
            "Chuck spent a few hours playing cool Ludum Dare games on his new PC, but " +
            "he left it on, and the monitor turns on every time he gets a notification.\n" +
            "When Chuck gets a notification, his phone buzzes right way but his monitor " +
            "takes a while to light up. Click on the monitor to turn it back off before " +
            "it disturbs Chuck's sleep."
        });
    }

    // Start is called before the first frame update
    void Start()
    {
        startNight();
    }

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

    public static void activateObjectsByNight()
    {
        if (nightNumber >= 1)
        {
            Instance.alarmClock.SetActive(true);
            Instance.window.SetActive(true);
        }

        if (nightNumber >= 2)
        {
            Instance.phone.SetActive(true);
        }

        if (nightNumber >= 3)
        {
            Instance.window.GetComponent<Window>().lightIsActive = true;
        }
        
        if (nightNumber >= 5)
        {
            Instance.thermometer.SetActive(true);
            Instance.heater.SetActive(true);
        }
        
        if (nightNumber >= 6)
        {
            Instance.phone.GetComponent<Phone>().mtt_call = 14f;
        }

        if (nightNumber >= 7)
        {
            Instance.monitor.SetActive(true);
        }
    }

    public static void advanceNight()
    {
        nightNumber++;

        if (nightNumber > nightStartMessages.Count)
        {
            winGame();
        }
        
        startNight();
    }
    public static void startNight()
    {
        activateObjectsByNight();
        Timer.Instance.reset();
        SleepDepth.Instance.reset();
        Window.Instance.reset();

        if (Instance.thermometer.activeInHierarchy)
        {
            Instance.thermometer.GetComponent<Thermometer>().reset();
        }

        if (Instance.monitor.activeInHierarchy)
        {
            Instance.monitor.GetComponent<Monitor>().reset();
        }
        

        UI.displayMessage(nightStartMessages[nightNumber-1]);
    }

    public static void endNight()
    {
        float perc = (SleepDepth.score / SleepDepth.PERFECT_SCORE);

        if (perc > .9)
        {
            medals[nightNumber] = "GOLD";
            UI.displayMessage(new List<string>()
            {
                $"Night {nightNumber} is over. \n" +
                "Chuck had a great night's sleep! You got the GOLD rank."
            }, advanceNight);
        }
        else if (perc > .75)
        {
            medals[nightNumber] = "SILVER";
            UI.displayMessage(new List<string>()
            {
                $"Night {nightNumber} is over. \n" +
                "Chuck wasn't too sleepy today. You got the SILVER rank."
            }, advanceNight);
        }
        else if (perc >= .6)
        {
            medals[nightNumber] = "BRONZE";
            UI.displayMessage(new List<string>()
            {
                $"Night {nightNumber} is over. \n" +
                "Chuck was groggy all day, but made it through. You got the BRONZE rank."
            }, advanceNight);
        }
        else
        {
            String reason = nightNumber == 7
                ? "Chuck missed mass today. He was later hit by a bus and went to hell"
                : "Chuck was fired from his job one day before discovering a cure for cancer";
            
            UI.displayMessage(new List<string>()
            {
                $"Night {nightNumber} is over. \n" +
                $"{reason}. You FAILED."
            }, startNight, "Retry");
        }

        if (Instance.alarmClock.activeInHierarchy)
        {
            AlarmClock.Instance.reset();
        }

        if (Instance.phone.activeInHierarchy)
        {
            Phone.Instance.reset();
        }
    }

    public static void winGame()
    {
        var message = new List<string>()
        {
            "Congratulations, you've helped Chuck sleep through the seven sleepy nights!",
            "Medals\n\n" +
            $"Sunday Night: {medals[1]}\n" +
            $"Monday Night: {medals[2]}\n" +
            $"Tuesday Night: {medals[3]}\n" +
            $"Wednesday Night: {medals[4]}\n" +
            $"Thursday Night: {medals[5]}\n" +
            $"Friday Night: {medals[6]}\n" +
            $"Saturday Night: {medals[7]}\n",
            
            "Credits\n\n" +
            "Pixel10 font by Tom7 \n" +
            "Dragon Warrior III font by myname5749 \n" +
            "Special thanks to my friend Russell for also participating in Ludum Dare 48 " +
            "and pushing me through competition."
            
        };

        if (medals.Values.All(s => s.Equals("GOLD")))
        {
            message.Add("I acknowledge that you got all gold medals, but I don't want to encourage you any further.");
        }
        
        UI.displayMessage(message, restart, "Restart");
    }

    public static void restart()
    {
        SceneManager.LoadScene("SampleScene");
    }
}
