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

namespace ChatSDK
{
    /**
     * The chat setting class that defines parameters and options of the SDK, including whether to automatically accept friend invitations and whether to automatically download the thumbnail.
     */
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class Options
    {
        /**
	     * The App Key you get from the console when creating a chat app. It is the unique identifier of your app.
	     */
        public string AppKey = "";

        /**
	     * The URL of the DNS server.
	     */
        public string DNSURL = "";

        /**
	     * The address of the IM server.
         * 
         * This address is used when you implement data isolation and data security during private deployment.
         * 
         * If you need the address, contact our business manager.
         * 
	     */
        public string IMServer = "";

        /**
	     * The address of the REST server. 
         * 
         * This address is used when you implement data isolation and data security during private deployment.
         * 
         * If you need the address, contact our business manager.
	     */
        public string RestServer = "";

        /**
	     * The custom port of the IM server.
         * 
         * This port is used when you implement data isolation and data security during private deployment.
         * 
         * If you need the port, contact our business manager.
	     */
        public int IMPort = 0;

        /**
	     * Whether to enable DNS.
	     * - (Default) `true`: Yes.
	     * - `false`: No. DNS should be disabled for private deployment.
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool EnableDNSConfig = true;

        /**
         * Whether to output the debug information. 
         * - `true`: Yes. The debug information will be output as logs.
         * - (Default) `false`: No.
         */
        [MarshalAs(UnmanagedType.U1)]
        public bool DebugMode = false;

        /**
	     * Whether to enable automatic login.
	     * - (Default) `true`: Yes.
	     * - `false`: No.
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool AutoLogin = true;

        /**
	     * Whether to automatically accept friend invitations from other users. 
         * - `true`: Yes.
	     * - (Default) `false`: No.
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool AcceptInvitationAlways = false;

        /**
	     * Whether to accept group invitations automatically. 
         * - (Default) `true`: Yes.
	     * - `false`: No.
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool AutoAcceptGroupInvitation = false;

        /**
	     * Whether to require the read receipt. 
	     * - (Default) `true`: Yes; 
	     * - `false`: No.
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool RequireAck = true;

        /**
	     * Whether to require the delivery receipt.
	     * - (Default) `true`: Yes; 
	     * - `false`: No.
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool RequireDeliveryAck = false;

        /**
	     * Whether to delete the historical group messages in the memory and local database when leaving the group (either voluntarily or passively). 
	     * - (Default) `true`: Yes;
	     * - `false`: No.
         */
        [MarshalAs(UnmanagedType.U1)]
        public bool DeleteMessagesAsExitGroup = true;

        /**
	     * Whether to delete the historical messages of the chat room in the memory and local database when leaving the chat room (either voluntarily or passively).
	     * - (Default)` true`: Yes.
	     * - `false`: No.
         */
        [MarshalAs(UnmanagedType.U1)]
        public bool DeleteMessagesAsExitRoom = true;

        /**
	     * Whether to allow the chat room owner to leave the chat room.
	     * - (Default) `true`: Yes. When leaving the chat room, the chat room owner still has all privileges, except for receive messages in the chat room.
	     * - `false`: No.
         */
        [MarshalAs(UnmanagedType.U1)]
        public bool IsRoomOwnerLeaveAllowed = true;

        /**
	     * Whether to sort the messages in the reverse chronological order of the time when they are received by the server.
	     * - (Default) `true`: Yes;
	     * - `false`: No. Messages are sorted in the reverse chronological order of the time when they are created.
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool SortMessageByServerTime = true;

        /**
	     * Whether only HTTPS is used for REST operations.
	     * - (Default) `true`: Only HTTPS is supported.
	     * - `false`: Both HTTP and HTTPS are allowed. 
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool UsingHttpsOnly = false;

        /**
	     * Whether to upload the message attachments automatically to the chat server.
	     * - (Default)`true`: Yes;
	     * - `false`: No.
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool ServerTransfer = true;

        /**
	     * Whether to automatically download the thumbnail.
	     * - (Default) `true`: Yes;
	     * - `false`: No.
	     * 
	     */
        [MarshalAs(UnmanagedType.U1)]
        public bool IsAutoDownload = true;

        /**
        * The options constructor.
        * 
        * @param appKey  The App Key.
        */
        public Options(string appKey)
        {
            AppKey = appKey;
        }

        internal string ToJsonString() {

            JSONObject jo = new JSONObject();
            jo["app_key"] = AppKey;
            jo["debug_mode"] = DebugMode;
            jo["auto_login"] = AutoLogin;
            jo["accept_invitation_always"] = AcceptInvitationAlways;
            jo["auto_accept_group_invitation"] = AutoAcceptGroupInvitation;
            jo["require_ack"] = RequireAck;
            jo["require_delivery_ack"] = RequireDeliveryAck;
            jo["delete_messages_as_exit_group"] = DeleteMessagesAsExitGroup;
            jo["delete_messages_as_exit_room"] = DeleteMessagesAsExitRoom;
            jo["is_room_owner_leave_allowed"] = IsRoomOwnerLeaveAllowed;
            jo["sort_message_by_server_time"] = SortMessageByServerTime;
            jo["using_https_only"] = UsingHttpsOnly;
            //jo["server_transfer"] = ServerTransfer;
            jo["is_auto_download"] = IsAutoDownload;


            jo.Add("enable_dns_config", EnableDNSConfig);

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

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

            if (IMPort != 0) {
                jo.Add("im_port", IMPort);
            }


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

            jo.Add("mz_app_id", mZAppId);
            jo.Add("mz_app_key", mZAppKey);
            jo.Add("enable_mz_push", enableMZPush);

            jo.Add("oppo_app_key", oPPOAppKey);
            jo.Add("oppo_app_secret", oPPOAppSecret);
            jo.Add("enable_oppo_push", enableOPPOPush);

            jo.Add("mi_app_id", miAppId);
            jo.Add("mi_app_key", miAppKey);
            jo.Add("enable_mi_push", enableMiPush);

            jo.Add("fcm_id", fCMId);
            jo.Add("enable_fcm_push", enableFCMPush);

            jo.Add("apns_cer_name", aPNsCerName);
            jo.Add("enable_apns", enableAPNs);

            jo.Add("enable_hw_push", enableHWPush);

            jo.Add("enable_vivo_push", enableVivoPush);

            return jo.ToString();
        }


        // MeiZu
        private string mZAppId = "", mZAppKey = "";
        private bool enableMZPush = false;

        // OPPO
        private string oPPOAppKey = "", oPPOAppSecret = "";
        private bool enableOPPOPush = false;

        // XiaoMi
        private string miAppId = "", miAppKey = "";
        private bool enableMiPush = false;

        private bool enableVivoPush = false;

        // Google
        private string fCMId = "";
        private bool enableFCMPush = false;

        // Apple
        private string aPNsCerName = "";
        private bool enableAPNs = false;

        // HuaWei
        private bool enableHWPush = false;

    }
}
