using System;
using System.Runtime.InteropServices;

namespace Fivestops.Oxide
{
    public class CausalityClient
    {
        const string LIBRARY_LOCATION = "/home/eric/src/depot/src/main/causality-client/";

        // [DllImport (LIBRARY_LOCATION + "libcausality-client.so", CallingConvention=CallingConvention.Cdecl, EntryPoint="client_init")]
        [DllImport (LIBRARY_LOCATION + "libcausality-client.so",  CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, EntryPoint="client_init")]
        public static extern void Init (int key);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_get_request")]
        public static extern IntPtr GetRequest();

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_write_string")]
        public static extern bool WriteString (IntPtr request, string data);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_write_data")]
        public static extern bool WriteData (IntPtr request, [MarshalAs(UnmanagedType.LPArray)] byte[] data, int dataLen);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_get_uri")]
        public static extern string GetUri (IntPtr request);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_get_query")]
        public static extern string GetQuery (IntPtr request);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_finish")]
        public static extern bool Finish (IntPtr request);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_get_entity")]
        public static extern int ReadEntityBody (IntPtr request, [Out] [MarshalAs(UnmanagedType.LPArray)] byte[] bufOut, int bufLen);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_is_entire_entity_loaded")]
        public static extern bool IsEntireEntityLoaded (IntPtr request);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_get_method")]
        public static extern string GetMethod (IntPtr request);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_get_request_header")]
        public static extern string GetRequestHeader (IntPtr request, string key);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_set_response_header")]
        public static extern void SetResponseHeader (IntPtr request, string key, string value);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_set_status")]
        public static extern void SetStatus (IntPtr request, int status, string description);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_headers_sent")]
        public static extern bool HeadersSent (IntPtr request);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_get_variable")]
        public static extern string GetTransactionVariable (IntPtr request, string key);

        [DllImport (LIBRARY_LOCATION + "libcausality-client.so", EntryPoint="client_get_remote_ip")]
        public static extern string GetRemoteIP (IntPtr request);
    }
}
