using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class SpectatorController : MonoBehaviour {

    BasicCameraBehaviour basicCameraBehaviour;
    GameObject player;
    bool localPlayerFound = false;
    GameObject[] playerObjs;
    int currentSpectaingPlayerViewID;

    // Use this for initialization
    void Start () {
        Transform camPivot = GameObject.FindGameObjectWithTag("Camera Pivot").transform;
        basicCameraBehaviour = camPivot.GetChild(0).GetComponent<BasicCameraBehaviour>();
    }
	
	// Update is called once per frame
	void Update () {
        if(IsCurrentlySpectatingPlayer()) {
            playerObjs = GameObject.FindGameObjectsWithTag("Player");
            if (Input.GetKeyDown(KeyCode.A)) {
                SwitchSpectate(true);
            }
            if (Input.GetKeyDown(KeyCode.D)) {
                SwitchSpectate(false);
            }
            if (Input.GetKeyDown(KeyCode.W)) {

            }
            if (Input.GetKeyDown(KeyCode.S)) {

            }
        }
    }

    public void SwitchSpectate(bool left) {
        PlayerBase lastPlayer = null;
        for(int i = 0; i < playerObjs.Length; i++) {
            PlayerBase player;
            if (player = playerObjs[i].GetComponent<PlayerBase>()){
                if (player.photonView && player.photonView.ViewID == currentSpectaingPlayerViewID) {
                    if (left) {
                        if(lastPlayer)
                            SpectatePlayer(lastPlayer.transform);
                        else {
                            SpectatePlayer(playerObjs[playerObjs.Length - 1].transform);
                        }
                    }
                    else {
                        if (playerObjs.Length - 1 != i)
                            SpectatePlayer(playerObjs[i + 1].transform);
                        else
                            SpectatePlayer(playerObjs[0].transform);
                    }
                    return;
                }
                lastPlayer = player;
            }
        }
    }

    public void SpectateAnyPlayer() {
        Transform t = PlayerBase.GetNewCameraTargetTransform();
        SpectatePlayer(t);
    }

    public void SpectatePlayer(Transform t) {
        if (t) {
            if(player) {
                PlayerBase oldPlayerBase = player.GetComponent<PlayerBase>();
                oldPlayerBase.SetSpectating(false);
            }
            player = t.gameObject;
            PlayerBase playerBase = t.GetComponent<PlayerBase>();
            playerBase.SetSpectating(true);
            currentSpectaingPlayerViewID = playerBase.photonView.ViewID;
            basicCameraBehaviour.ChangeCameraTarget(t);
        }
    }

    public bool IsCurrentlyPlaying() {
        if (localPlayerFound && player)
            return true;
        if (!PhotonNetwork.IsConnected) {
            player = GameObject.FindGameObjectWithTag("Player");
            return player != null;
        }

        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
        for (int i = 0; i < players.Length; i++) {
            PhotonView view = players[i].GetPhotonView();
            if (view && view.IsMine) {
                player = players[i];
                localPlayerFound = true;
                return true;
            }
        }
        return false;
    }

    public bool IsCurrentlySpectatingPlayer() {
        if (!PhotonNetwork.IsConnected) {
            return false;
        }

        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
        for (int i = 0; i < players.Length; i++) {
            PhotonView view = players[i].GetPhotonView();
            if (view && !view.IsMine && players[i].GetComponent<PlayerBase>().IsSpectating())
                return true;
        }
        return false;
    }
}
