﻿using System.Collections.Generic;
using SimpleJSON;

namespace ChatSDK
{
    /**
     *  The chat room class, which defines chat room information.
     */
    public class Room
    {

        /**
         * The chat room ID.
         *
         */
        public string RoomId { get; internal set; }

        /**
         * The chat room name.
         */
        public string Name { get; internal set; }
        /**
         * The chat room description.
         */
        public string Description { get; internal set; }

        /**
         * The chat room announcement.
         */
        public string Announcement { get; internal set; }

         /**
         * The number of online members.
         */
        public int MemberCount { get; internal set; }
		
        /**
         * The admin list of the chat room.
         */
        public List<string> AdminList { get; internal set; }

        /**
         * The member list of the chat room.
         * 
         * To get the member list of the chat room from the server, you can call `{@link IRoomManager#FetchRoomMembers(String, String, int, ValueCallBack)}`.
         */
        public List<string> MemberList { get; internal set; }

        /**
         * The block list of the chat room.
         * 
         * To get the block list of the chat room from the server, you can call `{@link IRoomManager#FetchRoomBlockList(String, int, int, ValueCallBack)}`.         
         */
        public List<string> BlockList { get; internal set; }

        /**
         * The mute list of the chat room.
         * 
         * To get the mute list of the chat room from the server, you can call `{@link IRoomManager#FetchRoomMuteList(String, int, int, ValueCallBack)}`.
         *
         */
        public List<string> MuteList { get; internal set; }

        /**
         * The maximum number of members allowed in the chat room, which is determined during chat room creation.
         * 
         * To get the latest data, you can call `{@link IRoomManager#FetchRoomInfoFromServer(String,ValueCallBack)}` to get details of a chat room from the server. 
         */
        public int MaxUsers { get; internal set; }

        /**
         * The chat room owner.
         * 
         * To get the latest data, you can call `{@link IRoomManager#FetchRoomInfoFromServer(String,ValueCallBack)}` to get details of a chat room from the server.
         */
        public string Owner { get; internal set; }

        /**
         * Whether all members are muted.
         * - `true`: Yes.  
         * - `false`: No.
         * 
         * **Note**
         * - Once all members are muted or unmuted, the callback is triggered to notify and update the mute or unmute status. You can call the method to get the current status.
         * - If you leave the chat room and rejoin it, the status is not reliable.
         * 
         */
        public bool IsAllMemberMuted { get; internal set; }

        /**
         * The role of the current user in the chat room.
         */
        public RoomPermissionType PermissionType { get; internal set; }



        internal Room(string jsonString)
        {
            if (jsonString != null)
            {
                JSONNode jn = JSON.Parse(jsonString);

                if (!jn.IsNull && jn.IsObject)
                {
                    JSONObject jo = jn.AsObject;
                    RoomId = jo["roomId"].Value;
                    Name = jo["name"].Value;
                    Description = jo["desc"].Value;
                    Announcement = jo["announcement"].Value;
                    MemberCount = jo["memberCount"].AsInt;
                    AdminList = TransformTool.JsonStringToStringList(jo["adminList"].Value);
                    MemberList = TransformTool.JsonStringToStringList(jo["memberList"].Value);
                    BlockList = TransformTool.JsonStringToStringList(jo["blockList"].Value);
                    MuteList = TransformTool.JsonStringToStringList(jo["muteList"].Value);
                    MaxUsers = jo["maxUsers"].AsInt;
                    Owner = jo["owner"].Value;
                    IsAllMemberMuted = jo["isAllMemberMuted"].AsBool;
                    if (jo["permissionType"].AsInt == -1)
                    {
                        PermissionType = RoomPermissionType.None;
                    }
                    else if (jo["permissionType"].AsInt == 0)
                    {
                        PermissionType = RoomPermissionType.Member;
                    }
                    else if (jo["permissionType"].AsInt == 1)
                    {
                        PermissionType = RoomPermissionType.Admin;
                    }
                    else if (jo["permissionType"].AsInt == 2)
                    {
                        PermissionType = RoomPermissionType.Owner;
                    }
                }
            }
        }


        internal Room()
        {
            //default constructor
        }

    }
}
