// Update is called once per frame
    void Update()
    {
        if (_agentController == null)
        {
            return;
        }

        if (!_agentController.IsRunning)
        {
            return;
        }


        if (_agentController._body._speechController.IsPlaying)
        {
            return;
        }

        if (_agentController.getJustReplied())
        {
            var reply = _agentController.getReply();


            HandleEffects(new List <Name> {
                EventHelper.ActionEnd(_agentController.RPC.CharacterName, (Name)("Speak(" + reply.CurrentState.ToString() + "," + reply.NextState.ToString() + "," + reply.Meaning.ToString() + "," + reply.Style.ToString() + ")"), (Name)"Player")
            });



            waitingforReply = false;
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            if (Time.timeScale > 0)
            {
                Time.timeScale = 0;
            }
            else
            {
                Time.timeScale = 1;
            }
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            this.Restart();
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            this.SaveState();
        }

        _agentController.UpdateEmotionExpression();

        if (Player != null)
        {
            var decision = Player.Decide().FirstOrDefault();

            if (decision == null || waitingforReply)
            {
                return;
            }

            if (decision.Target != _agentController.RPC.CharacterName)
            {
                return;
            }
            var dialogActions = _iat.GetDialogueActions(decision.Parameters.ElementAt(0), Name.BuildName("*"), Name.BuildName("*"), Name.BuildName("*"));

            UpdateButtonTexts(false, dialogActions);

            waitingforReply = true;
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (_agentController == null)
        {
            return;
        }

        if (!_agentController.IsRunning)
        {
            return;
        }

        if (_agentController.getJustReplied())
        {
            var reply = _agentController.getReply();
            waitingForReply = false;
            Initialized     = true;
            UpdateHistory(reply.Utterance);
            ClearDialogOptions();

            var cs  = reply.CurrentState;
            var ns  = reply.NextState;
            var m   = reply.Meaning;
            var sty = reply.Style;

            List <Name> events = new List <Name>();

            events.Add(EventHelper.ActionEnd(_agentController.RPC.CharacterName.ToString(),
                                             "Speak(" + cs + "," + ns + ", " + m + "," + sty + ")", "Player"));
            events.Add(EventHelper.PropertyChange("DialogueState(" + _agentController.RPC.CharacterName + ")", ns,
                                                  _agentController.RPC.CharacterName.ToString()));

            events.Add(EventHelper.PropertyChange("Has(Floor)", "Player",
                                                  _agentController.RPC.CharacterName.ToString()));

            Player.Perceive(events);
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            if (Time.timeScale > 0)
            {
                Time.timeScale = 0;
            }
            else
            {
                Time.timeScale = 1;
            }
        }

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[0].onClick.Invoke();
            }
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[1].onClick.Invoke();
            }
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[2].onClick.Invoke();
            }
        }
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[3].onClick.Invoke();
            }
        }

        if (Input.GetKeyDown(KeyCode.Alpha5))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[4].onClick.Invoke();
            }
        }

        if (Input.GetKeyDown(KeyCode.Alpha6))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[5].onClick.Invoke();
            }
        }

        if (Input.GetKeyDown(KeyCode.Keypad1))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[0].onClick.Invoke();
            }
        }

        if (Input.GetKeyDown(KeyCode.Keypad2))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[1].onClick.Invoke();
            }
        }

        if (Input.GetKeyDown(KeyCode.Keypad3))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[2].onClick.Invoke();
            }
        }

        if (Input.GetKeyDown(KeyCode.Keypad4))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[3].onClick.Invoke();
            }
        }
        if (Input.GetKeyDown(KeyCode.Keypad5))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[4].onClick.Invoke();
            }
        }
        if (Input.GetKeyDown(KeyCode.Keypad6))
        {
            if (!m_buttonList.IsEmpty())
            {
                m_buttonList[5].onClick.Invoke();
            }
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            this.SaveState();
        }
        if (_agentController.IsRunning)
        {
            _agentController.UpdateEmotionExpression();

            if (waitingForReply == false)
            {
                var action = Player.Decide().FirstOrDefault();

                if (action == null)
                {
                    return;
                }

                //       Debug.Log("Action Key: " + action.Key);

                var currentState = action.Parameters[0].ToString();



                var possibleOptions         = _iat.GetDialogueActionsByState(currentState).ToList();
                var originalPossibleActions = possibleOptions;



                if (PJScenario)
                {
                    if (!Initialized)
                    {
                        var newOptions =
                            possibleOptions.Shuffle(rand).Where(x => x.CurrentState == "Start").Take(3).ToList();


                        newOptions.AddRange(_iat.GetDialogueActionsByState("Introduction"));
                        possibleOptions = newOptions;
                    }
                    else
                    {
                        // var uhm = rand.Next(0, 10);


                        // Debug.Log(" rand " + uhm );


                        var newOptions = possibleOptions.Where(x => !alreadyUsedDialogs.ContainsKey(x.Utterance)).Shuffle(rand).Take(3).ToList();



                        var additionalOptions =
                            _iat.GetDialogueActionsByState("Start").Shuffle(rand).Take(2).ToList();


                        possibleOptions = newOptions.Concat(additionalOptions).ToList();


                        if (alreadyUsedDialogs.Count() > 12 && possibleOptions.Count() < 6)
                        {
                            var ClosureOptions = _iat.GetDialogueActionsByState("Closure").Shuffle(rand).Take(1).ToList();

                            possibleOptions = newOptions.Concat(additionalOptions).Concat(ClosureOptions).ToList();
                        }
                    }
                }
                //   UpdatePapers();

                waitingForReply = true;
                AddDialogButtons(possibleOptions);
            }
        }
    }