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

#if UNITY_ANDROID || UNITY_IOS || UNITY_STANDALONE || UNITY_EDITOR
using UnityEngine;
#endif

namespace ChatSDK
{
    /**
     *  The message thread object class.
     */
    public class ChatThread
    {
        /**
         * Gets the message thread ID.
         *
         * @return The message thread ID.
         */
        public string Tid;

        /**
         * Gets the ID of the parent message.
         *
         * @return The ID of the parent message.
         */
        public string MessageId;

        /**
         * Gets the group ID to which the message thread belongs.
         *
         * @return The group ID.
         */
        public string ParentId;

        /**
         * Gets the message thread creator.
         *
         * This message thread creator is returned when you get the message thread details and message thread list.
         *
         * @return The user ID of the message thread creator.
         */

        public string Owner;

        /**
         * Gets the message thread name.
         *
         * @return The message thread name.
         */
        public string Name;

        /**
         * Get the number of messages in a message thread.
         *
         * To get the number of messages in a message thread, you need to first call {@link IChatThreadManager#GetThreadDetail} to get details of the message thread.
         *
         * @return The message count.
         */
        public int MessageCount;

        /**
         * Gets the number of members in the message thread.
         *
         * To get the member count, you need to first call {@link IChatThreadManager#GetThreadDetail} to get details of the message thread.
         *
         * @return  The number of members in the message thread.
         */
        public int MembersCount;

         /**
         * Gets the Unix timestamp when the message thread is created. The unit is millisecond.
         *
         * @return The Unix timestamp when the message thread is created.
         */
        public long CreateAt;

        /**
         * Get the last reply in the message thread.
         *
         * To get the last reply in the message thread, you need to first call {@link IChatThreadManager#GetThreadDetail} to get details of the message thread.
         *
         * @return The last reply in the message thread.
         */
        public Message LastMessage;

        static internal ChatThread FromJsonObject(JSONNode jn)
        {
            if (null == jn) return null;
            if (!jn.IsNull && jn.IsObject)
            {
                ChatThread thread = new ChatThread();
                JSONObject jo = jn.AsObject;
                thread.Tid = jo["tId"].Value;
                thread.MessageId = jo["messageId"].Value;
                thread.ParentId = jo["parentId"].Value;
                thread.Owner = jo["owner"].Value;
                thread.Name = jo["name"].Value;
                thread.MessageCount = jo["messageCount"].AsInt;
                thread.MembersCount = jo["membersCount"].AsInt;
                thread.CreateAt = long.Parse(jo["createTimestamp"].Value);
                if (!jo["lastMessage"].IsNull && jo["lastMessage"].IsString)
                    thread.LastMessage = new Message(jo["lastMessage"].Value);
                return thread;
            }
            else
                return null;
        }

        static internal ChatThread FromJson(string json)
        {
            Debug.Log($"FromJson json : {json}");
            if (null != json && json.Length > 0)
            {
                JSONNode jn = JSON.Parse(json);
                return FromJsonObject(jn);
            }
            else
                return null;
        }

        static internal CursorResult<ChatThread> CursorThreadFromJson(string json)
        {
            Debug.Log($"CursorThreadFromJson json : {json}");

            CursorResult<ChatThread> cursorResult = new CursorResult<ChatThread>();
            cursorResult.Data = new List<ChatThread>();

            if (null != json && json.Length > 0)
            {
                JSONNode jn = JSON.Parse(json);
                if (null == jn) return cursorResult;

                JSONObject jo = jn.AsObject;

                cursorResult.Cursor = jo["cursor"].Value;

                JSONNode jn_list = JSON.Parse(jo["list"].Value);
                if (null != jn_list)
                {
                    JSONArray jsonArray = jn_list.AsArray;
                    foreach (JSONNode obj in jsonArray)
                    {
                        if (obj.IsObject)
                        {
                            cursorResult.Data.Add(ChatThread.FromJsonObject(obj));
                        }
                    }
                }
            }
            return cursorResult;
        }

        static internal Dictionary<string, Message> DictFromJson(string json)
        {
            Debug.Log($"DictFromJson json : {json}");
            Dictionary<string, Message> dict = new Dictionary<string, Message>();

            if (null != json && json.Length > 0)
            {
                JSONNode jn = JSON.Parse(json);
                if (null == jn) return dict;

                JSONObject jo = jn.AsObject;

                foreach (string s in jo.Keys)
                {
                    Message msg = new Message(jo[s].Value);
                    dict.Add(s, msg);
                }
            }
            return dict;
        }

    }
}
