/*
 *
 * HL demo implementation
 *
 * */

#include <random>
#include <atomic>
#include <iostream>
#include <sstream>

#include <hl/include/sfinae.hpp>
#include <hl/include/hl_ipc/conversion.hpp>
#include <hl/include/mpl_foreach.hpp>
#include <hl/include/hl_ipc/hl_ipc_specific.hpp>

#ifdef __J_ACTIVE
    #include <hl/include/hl_ipc/hl_json_backend.hpp>
#endif

#include <hl/include/hl_ipc/Interface.hpp>
#include <hl/include/linux/environment.hpp>
#include <hl/include/atomic_st_value.hpp>

namespace impl
{

template <typename Class>
struct invoke_backend
{
    // template
    template <typename Backend>
    inline void
    invoke(Backend& backend)
    {
        std::cout << ((std::string)backend).c_str() << std::endl;
    }
};

template <typename StringType
        , typename Invoker>
struct archive_backend : Invoker
{
    // TODO we should implement any logic of you backend serialization
};

// Service base functionality
// implementation placed below
struct base_interface
{
    HL_META_CLASS_DECLARATION(base_interface);

    HL_RPC_TYPE(((std::string, first_filed), (std::string, second_field)), ReflType);
    HL_RPC_FUNCTION(
                ((bool, result))
                , run
                , ((std::string, first_field), (ReflType, refl_demo))
                );

    HL_RPC_FUNCTION(((bool, result)), exit, ((bool, mustexit)));

};

#ifdef __J_ACTIVE
    struct implementation
            : hl::Interface<
                          hl::Impl<implementation, base_interface>
                        , std::string
                        , hl::JsonBackend<std::string, invoke_backend<implementation> > >
    {
        HL_RPC_SERVICE_CLIENT_DECLARATION(implementation, "I_am", "I_am", "");
    };
#else
    struct implementation
            : hl::Interface<
                          hl::Impl<implementation, base_interface>
                        , std::string
                        , impl::archive_backend<std::string, invoke_backend<implementation> > >
    {
        HL_RPC_SERVICE_CLIENT_DECLARATION(implementation, "I_am", "I_am", "");
    };
#endif

}

template <typename Type>
inline void
print_type()
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

//  The global test runner
// we should use this only as global
int main()
{
#ifdef __J_ACTIVE
    impl::implementation::run_result r
        = impl::implementation().invoke<impl::run_selector>("hell world!"
                    , impl::base_interface::ReflType("1-1-1", "2-2-2"));
#endif

    typedef hl::refl_extract<impl::base_interface::ftbl >::type functions;

    // but I can do anythink here with methods
    print_type<functions>();

    return 0;
}
