﻿using System.Collections.Generic;
using System.Runtime.InteropServices;
using SimpleJSON;

namespace ChatSDK
{
    /**
     *  The group class, which defines group information.
     */
    public class Group
    {

        internal Group(string jsonString)
        {
            if (jsonString != null)
            {
                JSONNode jn = JSON.Parse(jsonString);
                if (!jn.IsNull && jn.IsObject)
                {
                    JSONObject jo = jn.AsObject;
                    this.GroupId = jo["groupId"].Value;
                    this.Name = jo["name"].Value;
                    this.Description = jo["desc"].Value;
                    this.Owner = jo["owner"].Value;
                    this.Announcement = jo["announcement"].Value;
                    this.MemberCount = jo["memberCount"].AsInt;
                    this.MemberList = TransformTool.JsonStringToStringList(jo["memberList"].Value);
                    this.AdminList = TransformTool.JsonStringToStringList(jo["adminList"].Value);
                    this.BlockList = TransformTool.JsonStringToStringList(jo["blockList"].Value);
                    this.MuteList = TransformTool.JsonStringToStringList(jo["muteList"].Value);
                    this.NoticeEnabled = jo["noticeEnable"].AsBool;
                    this.MessageBlocked = jo["messageBlocked"].AsBool;
                    this.IsAllMemberMuted = jo["isAllMemberMuted"].AsBool;
                    string optionsString = jo["options"].Value;
                    if (optionsString != null)
                    {
                        this.Options = new GroupOptions(optionsString);
                    }

                    if (jo["permissionType"].AsInt == -1)
                    {
                        this.PermissionType = GroupPermissionType.None;
                    }
                    else if (jo["permissionType"].AsInt == 0)
                    {
                        this.PermissionType = GroupPermissionType.Member;
                    }
                    else if (jo["permissionType"].AsInt == 1)
                    {
                        this.PermissionType = GroupPermissionType.Admin;
                    }
                    else if (jo["permissionType"].AsInt == 2)
                    {
                        this.PermissionType = GroupPermissionType.Owner;
                    }
                }
            }
        }

        /**
         * The group ID.
         */
        public string GroupId { get; internal set; }

        /**
         * The group name.
         */
        public string Name { get; internal set; }

        /**
         * The group description.
         */
        public string Description { get; internal set; }

        /**
         * The group owner.
         * 
         */
        public string Owner { get; internal set; }

        /**
         * The group announcement.
         * 
         * To get the group announcement from the server, you can call {@link IGroupManager#GetGroupAnnouncementFromServer(String, ValueCallBack)}.
         */
        public string Announcement { get; internal set; }

        /**
         * The number of members in the group.
         * 
         */
        public int MemberCount { get; internal set; }

        /**
         * The member list of the group.
         * 
         */
        public List<string> MemberList { get; internal set; }

        /**
         * The admin list of the group.
         * 
         * To get the admin list of the group from the server, you can call {@link IGroupManager#GetGroupWithId(String)} to get group details.
         * 
         */
        public List<string> AdminList { get; internal set; }

        /**
         * Gets the block list of the group.
         * 
         * To get the block list of the group from the server, you can call {@link IGroupManager#GetGroupBlockListFromServer(String, int, int, ValueCallBack)}.
         * 
         */
        public List<string> BlockList { get; internal set; }

        /**
         * Gets the mute list of the group.
         * 
         * To get the mute list of the group from the server, you can call {@link IGroupManager#GetGroupMuteListFromServer(String, int, int, ValueCallBack)}.
         *
         */
        public List<string> MuteList { get; internal set; }

        /**
         * Whether push notifications are enabled.
         * - `true`: Yes.
         * - `false`: No.
         * 
         */
        public bool NoticeEnabled { get; internal set; }

        /**
         * Whether group messages are blocked.
         * - `true`: Yes.
         * - `false`: No.
         
         * To block group messages, you can call {@link IGroupManager#BlockGroup(String，CallBack}.
         * 
         * To unblock group messages, you can call {@link IGroupManager#UnBlockGroup(String, CallBack)}.
         * 
         */
        public bool MessageBlocked { get; internal set; }

        /**
         * Whether all members are muted.
         * - `true`: Yes.
         * - `false`: No.
         * 
         * The mute state of the in-memory object is updated when the mute callback {@link OnMuteListAddedFromGroup} or unmuting callback {@link OnMuteListRemovedFromGroup} is received.
         *
         *  After the in-memory object is collected and pulled again from the database or server, the state becomes untrustworthy.
         */
        public bool IsAllMemberMuted { get; internal set; }

        /**
         * The group options.
         * 
         */
        public GroupOptions Options { get; internal set; }

        /**
         * The role of the current user in the group.
         * 
         */
        public GroupPermissionType PermissionType { get; internal set; }

        internal Group()
        {
            //default constructor
        }

    }
}
