using UnityEngine;
using System.Collections;

public class Accusation : MonoBehaviour
{
    public KeyCode accusationInput = KeyCode.Mouse0;
    public KeyCode suspicionInput = KeyCode.Mouse1;
    public string monkInnocent;

    private bool firstAccusation = true;

    void Update()
    {
        bool accusation = Input.GetKeyDown(accusationInput);
        bool suspicion = Input.GetKeyDown(suspicionInput);
        if (accusation || suspicion)
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (hit.collider != null && hit.collider.tag == "Monk")
            {
                if (accusation && (GameMgr.instance.CanAccuse() || firstAccusation))
                {
                    firstAccusation = false;
                    PC pcBehav = hit.collider.gameObject.GetComponent<PC>();
                    GameObject temple = GameObject.Find("Temple");
                    Monk[] monks = temple.GetComponentsInChildren<Monk>();
                    if (pcBehav.enabled) // PC
                    {
                        for (int i = 0; i < monks.Length; i++)
                        {
                            if (monks[i].gameObject != pcBehav.gameObject)
                            {
                                monks[i].gameObject.SetActive(false);
                            }
                            else
                            {
                                monks[i].Stop();
                                monks[i].enabled = false;
                                monks[i].gameObject.GetComponent<WhirlingRitual>().enabled = false;
                                monks[i].gameObject.GetComponent<MarchingRitual>().enabled = false;
                                monks[i].gameObject.GetComponent<GenuflexionRitual>().enabled = false;
                                monks[i].gameObject.GetComponent<SortingRitual>().enabled = false;
                                pcBehav.enabled = false;
                            }
                        }
                        GameMgr.instance.toWin("player2Win");

                    }
                    else // NPC
                    {
                        StopAllCoroutines();
                        StartCoroutine(GameMgr.instance.setTemporaryMessage(monkInnocent));
                        SoundManager.PlaySfx(SoundManager.Sfx.sfx_error);
                        GameMgr.instance.WrongAccusation();
                        NPC npcBehav = hit.collider.gameObject.GetComponent<NPC>();
                        npcBehav.gameObject.SetActive(false);
                    }
                }
                else if (suspicion)
                {
                    GameObject suspect = hit.collider.gameObject;
                    SuspicionMarker suspicionMarker = suspect.GetComponentInChildren<SuspicionMarker>(true);
                    suspicionMarker.enabled = true;
                    GameMgr.instance.Suspect();
                }
            }
        }
    }
}
