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

namespace ChatSDK
{
    /**
     * The enumeration of user attributes.  
     */
    public enum UserInfoType
    {
        /**
         * The nickname of the user.
         */
        NICKNAME = 0,

        /**
         * The avatar URL of the user.
         */
        AVATAR_URL = 1,

        /**
         * The email address of the user.
         */
        EMAIL = 2,

        /**
         * The phone number of the user.
         */
        PHONE = 3,

        /**
         * The gender of the user.
         * - (Default) `0`: Unknown.
         * - `1`: Male.
         * - `2`: Female.
         */
        GENDER = 4,

        /**
         * The signature of the user.
         */
        SIGN = 5,

        /**
         * The birthday of the user.
         */
        BIRTH = 6,

        /**
         * The extension information of the user. 
         * 
         * You can specify either an empty string or the custom information encapsulated as the JSON string.
         */
        EXT = 100
    };

    /**
    * The user information class.
    */
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class UserInfo
    {
        /**
         * The nickname of the user.
         */
		 [MarshalAs(UnmanagedType.LPTStr)]
        public string nickName = "";
        /**
         * The avatar URL of the user.
         */
        public string avatarUrl = "";
        /**
         * The email address of the user.
         */
        public string email = "";
         /**
         * The phone number of the user.
         */
        public string phoneNumber = "";
        /**
         * The signature of the user.
         */
        public string signature = "";
         /**
         * The birthday of the user.
         */
        public string birth = "";
        /**
         * The user ID.
         */
        public string userId = "";
         /**
         * The extension information of the user. 
         * 
         * You can specify either an empty string or the custom information encapsulated as the JSON string.
         */
        public string ext = "";
        /**
         * The gender of the user.
         * - (Default) `0`: Unknown.
         * - `1`: Male.
         * - `2`: Female.
         */
        public int gender = 0;

        public UserInfo()
        {
            //default constructor
        }

        internal void Unmarshall()
        {
            nickName = TransformTool.GetUnicodeStringFromUTF8(nickName);
        }

        //public UserInfo(string jsonString)
        internal UserInfo(string jsonString)
        {
            if (jsonString != null)
            {
                JSONNode jn = JSON.Parse(jsonString);
                if (!jn.IsNull && jn.IsObject)
                {
                    JSONObject jo = jn.AsObject;
                    if(!jo["nickName"].IsNull) {
                        this.nickName = jo["nickName"].Value;
                    }

                    if (!jo["avatarUrl"].IsNull) {
                        this.avatarUrl = jo["avatarUrl"].Value;
                    }

                    if (!jo["mail"].IsNull) {
                        this.email = jo["mail"].Value;
                    }

                    if (!jo["phone"].IsNull) {
                        this.phoneNumber = jo["phone"].Value;
                    }

                    if (!jo["sign"].IsNull)
                    {
                        this.signature = jo["sign"].Value;
                    }

                    if (!jo["birth"].IsNull)
                    {
                        this.birth = jo["birth"].Value;
                    }

                    if (!jo["userId"].IsNull)
                    {
                        this.userId = jo["userId"].Value;
                    }

                    if (!jo["gender"].IsNull)
                    {
                        this.gender = jo["gender"].AsInt;
                    }
                    
                    if (!jo["ext"].IsNull) {
                        this.ext = jo["ext"].Value;
                    }
                    
                }
            }
        }

        //public JSONObject ToJson()
        internal JSONObject ToJson()
        {
            JSONObject jo = new JSONObject();
            jo.Add("userId", userId);
            if (nickName != null) {
                jo.Add("nickName", nickName);
            }

            if (avatarUrl != null) {
                jo.Add("avatarUrl", avatarUrl);
            }

            if (email != null) {
                jo.Add("mail", email);
            }

            if (phoneNumber != null) {
                jo.Add("phone", phoneNumber);
            }

            if (signature != null) {
                jo.Add("sign", signature);
            }

            if (birth != null) {
                jo.Add("birth", birth);
            }

            if(ext != null)
            {
                jo.Add("ext", ext);
            }
            
           
            jo.Add("gender", gender);
            return jo;
        }
    };
}
