/*
     *
     * ======== PUBLIC FUNCTION ===========
     *
     *
     * */

    /**
     * Fungsi playGames bersifat hybrid.
     * Jika belum ada player yang memulai, dia akan menjadi host (hanya status)
     * Jika di panggil saat host menunggu player lain, maka dia akan join
     * Jika di panggil saat game sedang berlangsung, tidak akan terjadi apa-apa
     *  */
    public void PlayGames()
    {
        MyNetworkMessageClass myMess = new MyNetworkMessageClass();

        if (networkVariable.gameSession == NetworkVariable.GAME_IDLE)
        {
            networkVariable.localPlayerSession[(networkVariable.thisPlayer - 1)].playerGameSession = NetworkVariable.playerSession.WAITING;
            networkVariable.localPlayerSession[(networkVariable.thisPlayer - 1)].isHost            = true;



            myMess.syncPlayerSession = networkVariable.localPlayerSession;
            myMess.fromPlayer        = networkVariable.thisPlayer;

            Invoke("WaitForReady", networkVariable.waitLobbyTime); // panggil wait for ready setelah waktu menunggu selesai

            myClient.Send(MyNetworkMessageClass.CREATE_GAME, myMess);
        }
        else if (networkVariable.gameSession == NetworkVariable.GAME_WAITING)
        {
            if (!networkVariable.localPlayerSession[(networkVariable.thisPlayer - 1)].isHost)
            {
                networkVariable.localPlayerSession[(networkVariable.thisPlayer - 1)].playerGameSession = NetworkVariable.playerSession.JOINED;

                myMess.syncPlayerSession = networkVariable.localPlayerSession;
                myMess.fromPlayer        = networkVariable.thisPlayer;
                myClient.Send(MyNetworkMessageClass.CREATE_GAME, myMess);
            }
            else
            {
                Debug.Log("You already as host");
            }
        }
    }
    /**
     * User selesai mengikuti tutorial, dan sudah ready untuk bermain game
     * */
    public void ConfirmReady()
    {
        MyNetworkMessageClass myMess = new MyNetworkMessageClass();

        networkVariable.localPlayerSession[(networkVariable.thisPlayer - 1)].playerGameSession = NetworkVariable.playerSession.READY;
        myMess.syncPlayerSession = networkVariable.localPlayerSession;
        myMess.fromPlayer        = networkVariable.thisPlayer;

        myClient.Send(MyNetworkMessageClass.CONFIRM_READY, myMess);
    }
    /**
     * User konfirmasi kalau game sudah selesai, disini proses pengiriman score dimulai
     * */
    public void FinishGame()
    {
        MyNetworkMessageClass myMess = new MyNetworkMessageClass();

        networkVariable.localPlayerSession[(networkVariable.thisPlayer - 1)].playerGameSession = NetworkVariable.playerSession.FINISH_GAME;
        networkVariable.localPlayerSession[(networkVariable.thisPlayer - 1)].playerScore       = Random.Range(1000, 5000);
        myMess.syncPlayerSession = networkVariable.localPlayerSession;
        myMess.fromPlayer        = networkVariable.thisPlayer;

        myClient.Send(MyNetworkMessageClass.FINISH_GAME, myMess);
    }
    void finish_client(NetworkMessage message)
    {
        SyncSession(message);

        int PlayerFinish = 0;
        int i            = 0;

        for (i = 0; i < networkVariable.localPlayerSession.Length; i++)
        {
            if (networkVariable.localPlayerSession[i].playerGameSession == NetworkVariable.playerSession.FINISH_GAME)
            {
                PlayerFinish++;
            }
        }

        if (networkVariable.totalPlayerReady == PlayerFinish)
        {
            networkVariable.gameSession = NetworkVariable.GAME_FINISH;
            float maxVal   = 0;
            int   noPlayer = 0;
            //cek max score
            for (i = 0; i < networkVariable.localPlayerSession.Length; i++)
            {
                if (maxVal < networkVariable.localPlayerSession[i].playerScore)
                {
                    maxVal   = networkVariable.localPlayerSession[i].playerScore;
                    noPlayer = (i + 1);
                }
            }

            screenManager.mainGameSession.text = "THE WINNER IS PLAYER " + noPlayer.ToString() + " SCORE:" + maxVal.ToString();


            MyNetworkMessageClass myMess = new MyNetworkMessageClass();

            for (i = 0; i < networkVariable.localPlayerSession.Length; i++)
            {
                networkVariable.localPlayerSession[i].playerGameSession = NetworkVariable.playerSession.IDLE;
                networkVariable.localPlayerSession[i].isHost            = false;
            }

            myMess.syncPlayerSession = networkVariable.localPlayerSession;
            myMess.fromPlayer        = networkVariable.thisPlayer;

            myClient.Send(MyNetworkMessageClass.BACK_TO_IDLE, myMess);
        }

        screenManager.SetAllText();
    }
    void blastToAll(NetworkMessage message)
    {
        MyNetworkMessageClass myMess = message.ReadMessage <MyNetworkMessageClass>();

        if (message.msgType == MyNetworkMessageClass.CREATE_GAME)
        {
            networkVariable.gameSession = NetworkVariable.GAME_WAITING;
        }
        else if (message.msgType == MyNetworkMessageClass.WAIT_FOR_READY)
        {
            networkVariable.gameSession = NetworkVariable.GAME_WAIT_FOR_READY;
        }

        NetworkServer.SendToAll(message.msgType, myMess);
    }
    void WaitForReady()
    {
        MyNetworkMessageClass myMess = new MyNetworkMessageClass();

        for (int i = 0; i < networkVariable.localPlayerSession.Length; i++)
        {
            if (networkVariable.localPlayerSession[i].playerGameSession == NetworkVariable.playerSession.IDLE)
            {
                continue;
            }

            networkVariable.localPlayerSession[i].playerGameSession = NetworkVariable.playerSession.WAIT_FOR_READY;
        }
        myMess.syncPlayerSession = networkVariable.localPlayerSession;

        screenManager.SetAllText();
        myClient.Send(MyNetworkMessageClass.WAIT_FOR_READY, myMess);
    }
    void SyncSession(NetworkMessage message, bool forceAll = false)
    {
        MyNetworkMessageClass myMess = message.ReadMessage <MyNetworkMessageClass>();

        networkVariable.totalPlayer = 0;

        for (int i = 0; i < 3; i++)
        {
            if (networkVariable.localPlayerSession[i].playerGameSession != NetworkVariable.playerSession.IDLE)
            {
                networkVariable.totalPlayer++;
            }

            if (i == networkVariable.thisPlayer - 1 && !forceAll)
            {
                continue;
            }

            networkVariable.localPlayerSession[i] = myMess.syncPlayerSession[i];
        }
    }