using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {
    public static GameController Instance { get; private set; }

    [SerializeField] private MainHUDController _mainHudController;
    [SerializeField] private DayEndHUDController _dayEndHudController;
    [SerializeField] private GameObject _playerGameObject;

    private float _money = 0; //The money the player has
    private int _charisma = 0; //The charisma of the player, 0 - 100;

    //Time stuff
    [SerializeField] private int _hoursPerDay = 8; //9 - 17
    [SerializeField] private int _realLifeMinutes = 5;

    private float _time; //The actual time in gameHours
    private float _timeScale;
    private float _elapsedTime;

    //End of day variables
    private float _gains;
    private float _rent;
    private float _profit;

    private float _startingMoney;
    private bool _dayEnded = false;

    public enum LooseCondition {
        Bankrupt,
        Charisma
    }


    public float Money {
        get => _money;
        //Set the money and update the HUD
        set {
            _money = value;
            _mainHudController.SetMoneyText(_money);
        }
    }

    public int Charisma {
        get => _charisma;
        set {
            _charisma = value;
            _mainHudController.SetCharisma(_charisma);
        }
    }

    public void Reset() {
        //Start a new day
        _time = 0;
        _dayEnded = false;
        _gains = 0;
        _rent = 0;
        _profit = 0;

        _startingMoney = Money;
        HUDManager.Instance.HideCurrentHUD();
        _playerGameObject.SetActive(true);
    }

    private void Awake() {
        if (Instance != null) {
            Debug.LogError("What? 2 GameControllers crazy!!!");
        }

        Instance = this;

        
    }

    private void CustomerLeft(object sender, CustomerManager.OnCustomerLeftArgs e) {
        if (e.Angry) {
            //Customer left angry
            Charisma -= 10;
            if(Charisma <= 0) {
                GameOver(LooseCondition.Charisma);
            }
        }
        else {
            if (_charisma + 10 <= 100) {
                _charisma += 10;
            }
        }
    }

    private void Start() {
        Money = 100;
        Charisma = 50;
        _timeScale = (float)_hoursPerDay / (_realLifeMinutes * 60);
        Reset();
        
        CustomerManager.Instance.OnCustomerLeft += CustomerLeft;

    }

    private void DayEnd() {
        _playerGameObject.SetActive(false);
        HUDManager.Instance.ShowDayEndHUD();
        _gains = _money - _startingMoney;
        _rent = 10;
        _profit = _gains - _rent;

        _money += _profit; //Add the profit to the money, also includes rent
        _mainHudController.SetMoneyText(_money);


        HUDManager.Instance.GetDayEndController().DayEnd(_gains, _rent, _profit);
    }

    private void Update() {
        if (!_dayEnded) {
            Debug.Log("Time: " + _time + " Elapsed Time: " + _elapsedTime + " TimeScale: " + _timeScale);
            _elapsedTime += UnityEngine.Time.deltaTime * _timeScale;
            if (_elapsedTime >= 1) {
                _time += 1;
                _elapsedTime = 0;
            }

            int hours = (int)_time;

            _mainHudController.SetTime(hours + 9); //Offset by 9 so it starts at 9 am

            if (_time >= _hoursPerDay) {
                //End the day
                Debug.Log("Day Ended");
                _dayEnded = true;
                DayEnd();
            }
        }
    }


    public static void GameOver(LooseCondition condition) {
        SceneManager.LoadScene(condition == LooseCondition.Bankrupt ? "Bankrupt" : "Charisma");
    }
}