using DarkTonic.CoreGameKit;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IdentifierPhase : AbstractPhase {

    private static IdentifierPhase _instance;
    public static IdentifierPhase Instance
    {
        get
        {
            return _instance;
        }
    }

    public GameObject confirmPopup;

    public GameObject mLine;
    public Body[] Bodies;
    [HideInInspector] public Face[] mFaces;
    public Transform mFace;
    [HideInInspector] public int TrueAssassinIndex;
    public Animator mAnimatorSuspects;

    [HideInInspector] public int selectedIndex;

    private void Awake()
    {
        _instance = this;
        mFaces = new Face[5];
    }

    public override void BeginPhase()
    {
        Painter.Instance.rawImg.gameObject.SetActive(true);
        mLine.SetActive(true);

        TrueAssassinIndex = Random.Range(0, 5);

        for (byte i = 0; i < Bodies.Length; i++)
        {
            if (i == TrueAssassinIndex)
            {
                Debug.Log("Index: " + TrueAssassinIndex);
                Transform trans = ControlProgressManager.Instance.TheTrueAssassin.transform;
                trans.parent = Bodies[i].facePivot;
                trans.localScale = Vector3.one;
                trans.localPosition = Vector3.zero;
                trans.localRotation = Quaternion.identity;

                if (trans != null)
                {
                    this.mFaces[i] = trans.GetComponent<Face>();

                    if (this.mFaces[i] != null)
                    {
                        Bodies[i].setBody();
                    }
                }
            }
            else
            {
                Transform trans = PoolBoss.Spawn(this.mFace, this.Bodies[i].facePivot.position, Quaternion.identity, this.Bodies[i].facePivot);

                if (trans != null)
                {
                    this.mFaces[i] = trans.GetComponent<Face>();

                    if (this.mFaces[i] != null)
                    {
                        Bodies[i].setBody();
                        this.mFaces[i].makeOtherHeads(ControlProgressManager.Instance.TheTrueAssassin);
                    }
                }
            }
        }
    }

    public override void EndPhase()
    {
        Painter.Instance.rawImg.gameObject.SetActive(false);
        CameraManager.Instance.CanvasOver.SetActive(false);
        mAnimatorSuspects.Play("SuspectsLine_Out");
        ControlProgressManager.Instance.mCurrentPhase++;
        ControlProgressManager.Instance.RunningProgress();
    }

    public void select(int i)
    {
        this.selectedIndex = i;
        this.confirmPopup.SetActive(true);

        if (this.Bodies[i].highlightAnimator != null)
        {
            this.Bodies[i].highlightAnimator.SetTrigger("show");
        }
    }

    public void cancel()
    {
        this.confirmPopup.SetActive(false);

        if (this.Bodies[this.selectedIndex].highlightAnimator != null)
        {
            this.Bodies[this.selectedIndex].highlightAnimator.SetTrigger("hide");
        }
    }
}
