using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

	Score score;
	FlagManager flag;
	Scene scene;

    Button buttonScript;

    //ゲーム開始判定、Updateを持つスクリプト全てから参照される
    bool isGamePlayOK = false;


    //プレイヤーがどちらのチームかの判別
	public int P1TeamNum = 0;
    //public int P2TeamNum = 0;
    //public int P3TeamNum = 0;
    //public int P4TeamNum = 0;

    //試合に登場するキャラクター格納用リスト インスペクターで見れるようにしている
    [System.SerializableAttribute]
    public class TeamList
    {
        public List<GameObject> member = new List<GameObject>();
        public TeamList(List<GameObject> obj)
        {
            member = obj;
        }
    }
    [SerializeField] public List<TeamList> teamList = new List<TeamList>();

    //各キャラクターのターゲット格納用リスト インスペクターで見れるようにしている
    [System.SerializableAttribute]
    public class MarkList
    {
        public List<GameObject> target = new List<GameObject>();
        public MarkList(List<GameObject> obj)
        {
            target = obj;
        }
    }
    [SerializeField] public List<MarkList> markList = new List<MarkList>();

    //ボールを持っているキャラクター
    public GameObject ballChara;

    //プレイヤーが操作するキャラクター
    public GameObject playerChara;

    //スローインするキャラクター
    public GameObject throwinChara;


    //
    public float outPosX;
    public float outPosZ;

	//初期化
	void Start () {

		score = GetComponent<Score>();
		flag = GetComponent<FlagManager>();
		scene = GetComponent<Scene>();

		playerChara = GameObject.Find("Player");
		playerChara.GetComponent<CPU>().playerControl = true;

		ballChara = null;
        throwinChara = null;

        //0830 *****************
        //処理遅延して呼び出し
        Invoke("Jumpball", 0.1f);
        //****************************
    }


    void Update () {
        
		//スローインをさせるためにタイプを変える
        if(throwinChara != null && throwinChara.GetComponent<CPU>().status == 0 && flag.fieldGoal)
        {
            throwinChara.GetComponent<CPU>().status = 20;
        }

        //ボールを持ったキャラクターが、同じチームで操作キャラクターとは別のキャラクターだったとき
        if (!flag.fieldGoal && ballChara != null && ballChara.GetComponent<Team>().teamNum == P1TeamNum && playerChara != ballChara)
        {
            ControlCharaChenge(ballChara);
        }

        //操作キャラクターをプレイヤーが操作できていないとき
        if (playerChara != null && !playerChara.GetComponent<CPU>().playerControl)
        {
            //プレイヤーが操作できるようにする
            playerChara.GetComponent<CPU>().playerControl = true;
        }

        //ボールが外に出た時、スローインするキャラクターを設定する
        if (flag.foul && throwinChara == null)
        {
            if (flag.teamBall[0])
            {
                SetChara(0);
            }
            else if (flag.teamBall[1])
            {
                SetChara(1);
            }
        }

        //ボタンの種類切り替え
        //buttonScript.ChangeButtonMode(flag.teamBall[P1TeamNum]);

        //試合終了
        if (flag.timeOver && !flag.buzzerBeater && !scene.sceneChenge)
		{
            /*
            if (scene.getFadeObj().activeSelf)
            {
                scene.FadeOut();
            }
            else
            {
                scene.fadeActiveTrue();
            }
            */
            scene.blackfade = true;
            scene.sceneChenge = true;
            isGamePlayOK = false;
		}
	}

    //操作キャラクターを変える
    public void ControlCharaChenge(GameObject chara)
    {
        //旧操作キャラクターの一時格納用
        GameObject tmpChara = playerChara;

        //旧操作キャラクターのプレイヤー管理を止める
        tmpChara.GetComponent<CPU>().playerControl = false;

        //操作キャラクターを変えて、プレイヤーが操作できるようにする
        if(tmpChara == playerChara)
        {
            playerChara = chara;
            
            //CPUの管理を止める
            chara.GetComponent<CPU>().playerControl = true;
        }
    }

    
    //スローインするキャラクターを設定する
    public void SetChara(int tnum)
    {
        for (int t = 0; t < 2; t++)
        {
            for (int mnum = 0; mnum < 5 && teamList[t].member[mnum] != null; mnum++)
            {
                //一度動きを止める
                teamList[t].member[mnum].GetComponent<CPU>().status = 0;
            }
        }
        for (int mnum = 0; mnum < 5 && throwinChara == null && teamList[tnum].member[mnum] != null; mnum++)
        {
            if (!teamList[tnum].member[mnum].GetComponent<CPU>().playerControl)
            {

                throwinChara = teamList[tnum].member[mnum];
            }
        }
    }

    //-------------------------------------------------------------------------
    //スローインを受け取るキャラクターを設定する
    public void CatchChara(int tnum)
    {
        for (int mnum = 0; mnum < 5 && teamList[tnum].member[mnum] != null; mnum++)
        {
            if (teamList[tnum].member[mnum].GetComponent<CPU>().status != 21)
            {
                teamList[tnum].member[mnum].GetComponent<CPU>().status = 19;
                break;
            }
        }
    }
    //-------------------------------------------------------------------------

    //-----------------------------------------------------------------
    //複数のプレイヤーにボール所持が起こらないようにする
    //-----------------------------------------------------------------
    public void HaveBallPlayerOrgange(Transform ballTrans)
    {
        GameObject[] obj = GameObject.FindGameObjectsWithTag("Player");

        for (int i = 0; i < obj.Length; i++)
        {
            if (ballTrans.parent.name == obj[i].transform.name)
            {

            }
            else
            {
                obj[i].GetComponent<Player>().setHaveBall(false);
            }
        }
    }
    /*
    //シュートの際、ボールがゴールに衝突した場合に呼び出される
    public void shootEndCameraAction()
    {
        GameObject.Find("Main Camera").GetComponent<CameraAction>().shootEnd();
    }

    */

    //0830 ジャンプボール**************************************
    //プレイヤーをジャンプポジションに移動
    void Jumpball()
    {
        // Debug.Log( teamList[0].member[0]);
        teamList[0].member[0].transform.position = GameObject.Find("jumppos").transform.position;
        Vector3 n = teamList[0].member[0].transform.position;
        teamList[1].member[0].transform.position = GameObject.Find("jumppos2").transform.position;
        n = teamList[1].member[0].transform.position;
        Invoke("Jump", 1.0f);
        //Jump();
    }
    //ジャンプさせる
    void Jump()
    {
        teamList[0].member[0].GetComponent<PlayerAnimation>().jstartf = true;
        teamList[1].member[0].GetComponent<PlayerAnimation>().jstartf = true;

        teamList[0].member[0].GetComponent<PlayerAnimation>().shootanm = true;
        teamList[1].member[0].GetComponent<PlayerAnimation>().shootanm = true;

        teamList[0].member[0].GetComponent<PlayerAnimation>().jumpJudge(2f);
        teamList[1].member[0].GetComponent<PlayerAnimation>().jumpJudge(2f);
        Invoke("gameStart", 0.5f);
    }
    //******************************************************************

    void gameStart()
    {
        isGamePlayOK = true;
    }
    
    public bool IsGamePlayOK()
    {
        return isGamePlayOK;
    }
}