﻿using System.Collections.Generic;
using AgoraChat.SimpleJSON;
namespace AgoraChat
{
    public class Group : BaseModel
    {
        /**
         * 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#GetGroupSpecificationFromServer(String, ValueCallBack)} 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.
         * 
         */
        internal GroupOptions Options;

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

        /**
         * Fetches the group property: whether users can auto join the group VS need requesting or invitation from a group member to join the group.
         * There are four types of group properties used to define the style of a group, and `IsMemberOnly` contains three types including: PrivateOnlyOwnerInvite,  PrivateMemberCanInvite, PublicJoinNeedApproval. And do not include {@link GroupStyle#PublicOpenJoin}.
         *
         * - `true`: Users can not join the group freely. Needs the invitation from the group owner or members, or the application been approved by the group owner or admins.
         * - `false`: Users can join freely without the group owner or member‘s invitation or the new joiner’s application been approved.
         */
        public bool IsMemberOnly { get; internal set; }

        /**
         * - `true`：群成员可以邀请其他用户加入； - `false`：不允许群成员邀请其他用户加入。 ~english Gets whether the group member is allowed to invite other users to join the group.
         */
        public bool IsMemberAllowToInvite { get; internal set; }

        /**
         * The max number of group members allowed in a group. The param is set when the group is created. Be sure to fetch the detail specification of the group from the server first, see IGroupManager#GetGroupSpecificationFromServer(String, ValueCallBack). If not, the SDK returns 0.
         */
        public int MaxUserCount { get; internal set; }


        /**
        *  Whether the group is disabled. The default value for reading or pulling roaming messages from the database is NO
         */
        public bool IsDisabled { get; internal set; }

        /**
         * Gets the customized extension of the group.
         * @return  The customized extension of the group.
         */
        public string Ext { get; internal set; }

        internal Group() { }

        internal Group(string jsonString) : base(jsonString) { }

        internal Group(JSONObject jsonObject) : base(jsonObject) { }

        internal override void FromJsonObject(JSONObject jsonObject)
        {
            GroupId = jsonObject["groupId"];
            Name = jsonObject["name"];
            Description = jsonObject["desc"];
            Owner = jsonObject["owner"];
            Announcement = jsonObject["announcement"];
            MemberCount = jsonObject["memberCount"];
            MemberList = List.StringListFromJsonArray(jsonObject["memberList"]);
            AdminList = List.StringListFromJsonArray(jsonObject["adminList"]);
            BlockList = List.StringListFromJsonArray(jsonObject["blockList"]);
            MuteList = List.StringListFromJsonArray(jsonObject["muteList"]);
            MessageBlocked = jsonObject["block"];
            IsAllMemberMuted = jsonObject["isMuteAll"];
            //Options = ModelHelper.CreateWithJsonObject<GroupOptions>(jsonObject["options"]);
            MaxUserCount = jsonObject["maxUserCount"].AsInt;
            IsMemberOnly = jsonObject["isMemberOnly"].AsBool;
            IsMemberAllowToInvite = jsonObject["isMemberAllowToInvite"].AsBool;
            Ext = jsonObject["ext"];
            PermissionType = (GroupPermissionType)jsonObject["permissionType"].AsInt;
            IsDisabled = jsonObject["isDisabled"].AsBool;
        }

        internal override JSONObject ToJsonObject()
        {
            JSONObject jo = new JSONObject();
            jo.AddWithoutNull("groupId", GroupId);
            jo.AddWithoutNull("name", Name);
            jo.AddWithoutNull("desc", Description);
            jo.AddWithoutNull("owner", Owner);
            jo.AddWithoutNull("announcement", Announcement);
            jo.AddWithoutNull("memberCount", MemberCount);
            jo.AddWithoutNull("memberList", JsonObject.JsonArrayFromStringList(MemberList));
            jo.AddWithoutNull("adminList", JsonObject.JsonArrayFromStringList(AdminList));
            jo.AddWithoutNull("blockList", JsonObject.JsonArrayFromStringList(BlockList));
            jo.AddWithoutNull("muteList", JsonObject.JsonArrayFromStringList(MuteList));
            jo.AddWithoutNull("block", MessageBlocked);
            jo.AddWithoutNull("isMuteAll", IsAllMemberMuted);
            jo.AddWithoutNull("permissionType", PermissionType.ToInt());
            // jo.AddWithoutNull("options", Options.ToJsonObject());
            // jo.AddWithoutNull("isDisabled", IsDisabled);
            return jo;
        }
    }
}
