﻿// ----------------------------------------------------------------------------
// <copyright file="Room.cs" company="Exit Games GmbH">
//   PhotonNetwork Framework for Unity - Copyright (C) 2011 Exit Games GmbH
// </copyright>
// <summary>
//   Represents a room/game on the server and caches the properties of that.
// </summary>
// <author>developer@exitgames.com</author>
// ----------------------------------------------------------------------------
using System;
using System.Collections;
using ExitGames.Client.Photon;

/// <summary>
/// This class is used for room listings mostly. It is able to:
/// - cache "standard" properties (byte keys)
/// - cache custom properties (string keys)
/// </summary>
public class Room : RoomInfo
{
    /// <summary>Count of players in this room.</summary>
    public new int playerCount
    {
        get
        {
            if (PhotonNetwork.playerList != null)
            {
                return PhotonNetwork.playerList.Count;
            }
            else
            {
                return 0;
            }
        }
    }


    /// <summary>The name of a room. Unique identifier (per Loadbalancing group) for a room/match.</summary>
    public new string name
    {
        get
        {
            return this.nameField;
        }

        internal set
        {
            this.nameField = value;
        }
    }
    
    /// <summary>
    /// Sets a limit of players to this room. This property is shown in lobby, too.
    /// If the room is full (players count == maxplayers), joining this room will fail.
    /// </summary>
    public new int maxPlayers
    {
        get
        {
            return (int)this.maxPlayersField;
        }

        set
        {
            if (!this.Equals(PhotonNetwork.room))
            {
                PhotonNetwork.networkingPeer.DebugReturn(DebugLevel.WARNING, "Can't set room properties when not in that room.");
            }

            if (value > 255)
            {
                UnityEngine.Debug.LogError("Error: room.maxPlayers called with value " + value + ". This has been reverted to the max of 255 players, because internally a 'byte' is used.");
                value = 255;
            }

            if (value != this.maxPlayersField && !PhotonNetwork.offlineMode)
            {
                PhotonNetwork.networkingPeer.OpSetPropertiesOfGame(new Hashtable() { { GameProperties.MaxPlayers, (byte)value } }, true, (byte)0);
            }

            this.maxPlayersField = (byte)value;
        }
    }

    /// <summary>
    /// Defines if the room can be joined.
    /// This does not affect listing in a lobby but joining the room will fail if not open.
    /// If not open, the room is excluded from random matchmaking. 
    /// Due to racing conditions, found matches might become closed before they are joined. 
    /// Simply re-connect to master and find another.
    /// Use property "visible" to not list the room.
    /// </summary>
    public new bool open
    {
        get
        {
            return this.openField;
        }

        set
        {
            if (!this.Equals(PhotonNetwork.room))
            {
                PhotonNetwork.networkingPeer.DebugReturn(DebugLevel.WARNING, "Can't set room properties when not in that room.");
            }

            if (value != this.openField && !PhotonNetwork.offlineMode)
            {
                PhotonNetwork.networkingPeer.OpSetPropertiesOfGame(new Hashtable() { { GameProperties.IsOpen, value } }, true, (byte)0);
            }

            this.openField = value;
        }
    }

    /// <summary>
    /// Defines if the room is listed in its lobby.
    /// Rooms can be created invisible, or changed to invisible.
    /// To change if a room can be joined, use property: open.
    /// </summary>
    public new bool visible
    {
        get
        {
            return this.visibleField;
        }

        set
        {
            if (!this.Equals(PhotonNetwork.room))
            {
                PhotonNetwork.networkingPeer.DebugReturn(DebugLevel.WARNING, "Can't set room properties when not in that room.");
            }

            if (value != this.visibleField && !PhotonNetwork.offlineMode)
            {
                PhotonNetwork.networkingPeer.OpSetPropertiesOfGame(new Hashtable() { { GameProperties.IsVisible, value } }, true, (byte)0);
            }

            this.visibleField = value;
        }
    }

    /// <summary>
    /// A list of custom properties that should be forwarded to the lobby and listed there.
    /// </summary>
    public string[] propertiesListedInLobby { get; private set; }

    /// <summary>
    /// Gets if this room uses autoCleanUp to remove all (buffered) RPCs and instantiated GameObjects when a player leaves.
    /// </summary>
    public bool autoCleanUp
    {
        get
        {
            return this.autoCleanUpField;
        }
    }

    internal Room(string roomName, Hashtable properties) : base(roomName, properties)
    {
        this.propertiesListedInLobby = new string[0];
    }

    internal Room(string roomName, Hashtable properties, bool isVisible, bool isOpen, int maxPlayers, bool autoCleanUp, string[] propsListedInLobby) : base(roomName, properties)
    {
        this.visibleField = isVisible;
        this.openField = isOpen;
        this.autoCleanUpField = autoCleanUp;

        if (maxPlayers > 255)
        {
            UnityEngine.Debug.LogError("Error: Room() called with " + maxPlayers + " maxplayers. This has been reverted to the max of 255 players, because internally a 'byte' is used.");
            maxPlayers = 255;
        }

        this.maxPlayersField = (byte)maxPlayers;

        if (propsListedInLobby != null)
        {
            this.propertiesListedInLobby = propsListedInLobby;
        }
        else
        {
            this.propertiesListedInLobby = new string[0];
        }
    }

    /// <summary>
    /// Updates the custom properties of this Room with propertiesToSet.
    /// Only string-typed keys are applied, new properties (string keys) are added, existing are updated
    /// and if a value is set to null, this will remove the custom property.
    /// </summary>
    /// <remarks>
    /// Local cache is updated immediately, other players are updated through Photon with a fitting operation.
    /// </remarks>
    /// <param name="propertiesToSet"></param>
    public void SetCustomProperties(Hashtable propertiesToSet)
    {
        Hashtable customProps = propertiesToSet.StripToStringKeys() as Hashtable;

        // merge (delete null-values)
        this.customProperties.Merge(customProps);
        this.customProperties.StripKeysWithNullValues();

        // send (sync) these new values
        PhotonNetwork.networkingPeer.OpSetCustomPropertiesOfGame(customProps, true, 0);
    }
}