/*
    This file is based on file in the runjags package (version 2.0)
    The previous version of the file is Copyright (C) Matthew Denwood, licensed under GPL-2.
*/

#include <stdlib.h>
#include <R_ext/Rdynload.h>
#include <Rinternals.h> // for SEXP

extern void getjagsversions(int *forced, int *assumed, int *detected, int *used);
extern SEXP RoBMA_glmm_binom_marginal_loglik(SEXP ai, SEXP ci,
                                             SEXP n1i, SEXP n2i,
                                             SEXP mu_samples,
                                             SEXP tau_within,
                                             SEXP weights,
                                             SEXP theta_grid,
                                             SEXP log_theta_weights,
                                             SEXP logit_pi_grid,
                                             SEXP log_pi_weights);
extern SEXP RoBMA_glmm_pois_marginal_loglik(SEXP x1i, SEXP x2i,
                                            SEXP t1i, SEXP t2i,
                                            SEXP mu_samples,
                                            SEXP tau_within,
                                            SEXP weights,
                                            SEXP theta_grid,
                                            SEXP log_theta_weights,
                                            SEXP log_phi_grid,
                                            SEXP log_phi_weights);
extern SEXP RoBMA_glmm_binom_cluster_loglik(SEXP ai, SEXP ci,
                                            SEXP n1i, SEXP n2i,
                                            SEXP mu_samples,
                                            SEXP tau_within,
                                            SEXP tau_between,
                                            SEXP cluster_index,
                                            SEXP cluster_size,
                                            SEXP weights,
                                            SEXP theta_grid,
                                            SEXP log_theta_weights,
                                            SEXP logit_pi_grid,
                                            SEXP log_pi_weights,
                                            SEXP gamma_grid,
                                            SEXP log_gamma_weights);
extern SEXP RoBMA_glmm_pois_cluster_loglik(SEXP x1i, SEXP x2i,
                                           SEXP t1i, SEXP t2i,
                                           SEXP mu_samples,
                                           SEXP tau_within,
                                           SEXP tau_between,
                                           SEXP cluster_index,
                                           SEXP cluster_size,
                                           SEXP weights,
                                           SEXP theta_grid,
                                           SEXP log_theta_weights,
                                           SEXP log_phi_grid,
                                           SEXP log_phi_weights,
                                           SEXP gamma_grid,
                                           SEXP log_gamma_weights);
extern SEXP RoBMA_regplot_normal_mixture_interval(SEXP mean, SEXP sd,
                                                  SEXP probs);
extern SEXP RoBMA_regplot_selnorm_mixture_interval(SEXP mean, SEXP sd,
                                                   SEXP se, SEXP probs,
                                                   SEXP omega, SEXP alpha,
                                                   SEXP phack_kind,
                                                   SEXP kernel_mode,
                                                   SEXP z_lower,
                                                   SEXP z_upper,
                                                   SEXP sign, SEXP q,
                                                   SEXP phack_z_source,
                                                   SEXP phack_z_dest,
                                                   SEXP segment_bounds,
                                                   SEXP segment_step_bin,
                                                   SEXP segment_phack_region,
                                                   SEXP telescope_probabilities);
extern SEXP RoBMA_selnorm_kernel_loglik_matrix(SEXP yi, SEXP mu_num,
                                               SEXP sigma_num, SEXP mu_norm,
                                               SEXP sigma_norm, SEXP sei,
                                               SEXP weights, SEXP omega,
                                               SEXP alpha, SEXP phack_kind,
                                               SEXP kernel_mode,
                                               SEXP z_lower, SEXP z_upper,
                                               SEXP obs_bin, SEXP sign,
                                               SEXP q,
                                               SEXP phack_z_source,
                                               SEXP phack_z_dest,
                                               SEXP segment_bounds,
                                               SEXP segment_step_bin,
                                               SEXP segment_phack_region,
                                               SEXP telescope_probabilities);
extern SEXP RoBMA_selnorm_kernel_log_norm_matrix(SEXP mean, SEXP sd,
                                                 SEXP sei, SEXP omega,
                                                 SEXP alpha,
                                                 SEXP phack_kind,
                                                 SEXP kernel_mode,
                                                 SEXP z_lower,
                                                 SEXP z_upper,
                                                 SEXP sign, SEXP q,
                                                 SEXP phack_z_source,
                                                 SEXP phack_z_dest,
                                                 SEXP segment_bounds,
                                                 SEXP segment_step_bin,
                                                 SEXP segment_phack_region,
                                                 SEXP telescope_probabilities);
extern SEXP RoBMA_selnorm_kernel_cdf_matrix(SEXP x, SEXP mean, SEXP sd,
                                            SEXP sei, SEXP omega,
                                            SEXP alpha, SEXP phack_kind,
                                            SEXP kernel_mode,
                                            SEXP z_lower,
                                            SEXP z_upper, SEXP sign,
                                            SEXP q, SEXP phack_z_source,
                                            SEXP phack_z_dest,
                                            SEXP segment_bounds,
                                            SEXP segment_step_bin,
                                            SEXP segment_phack_region,
                                            SEXP lower_tail,
                                            SEXP telescope_probabilities);
extern SEXP RoBMA_selnorm_kernel_moments_matrix(SEXP mean, SEXP sd,
                                                SEXP sei, SEXP omega,
                                                SEXP alpha,
                                                SEXP phack_kind,
                                                SEXP kernel_mode,
                                                SEXP z_lower,
                                                SEXP z_upper,
                                                SEXP sign, SEXP q,
                                                SEXP phack_z_source,
                                                SEXP phack_z_dest,
                                                SEXP segment_bounds,
                                                SEXP segment_step_bin,
                                                SEXP segment_phack_region,
                                                SEXP telescope_probabilities);
extern SEXP RoBMA_selnorm_kernel_rng_matrix(SEXP mean, SEXP sd,
                                            SEXP sei, SEXP omega,
                                            SEXP alpha,
                                            SEXP phack_kind,
                                            SEXP kernel_mode,
                                            SEXP z_lower,
                                            SEXP z_upper,
                                            SEXP sign, SEXP q,
                                            SEXP phack_z_source,
                                            SEXP phack_z_dest,
                                            SEXP segment_bounds,
                                            SEXP segment_step_bin,
                                            SEXP segment_phack_region,
                                            SEXP telescope_probabilities);
extern SEXP RoBMA_selnorm_kernel_weighted_summary(SEXP yi, SEXP mean,
                                                  SEXP sd, SEXP sei,
                                                  SEXP psis_weights,
                                                  SEXP omega, SEXP alpha,
                                                  SEXP phack_kind,
                                                  SEXP kernel_mode,
                                                  SEXP z_lower,
                                                  SEXP z_upper,
                                                  SEXP sign, SEXP q,
                                                  SEXP phack_z_source,
                                                  SEXP phack_z_dest,
                                                  SEXP segment_bounds,
                                                  SEXP segment_step_bin,
                                                  SEXP segment_phack_region,
                                                  SEXP telescope_probabilities);
extern SEXP RoBMA_funnel_model_averaged_quantiles(SEXP se_sequence,
                                                  SEXP mu, SEXP tau,
                                                  SEXP PET, SEXP PEESE,
                                                  SEXP is_weightfunction,
                                                  SEXP omega, SEXP alpha,
                                                  SEXP phack_kind,
                                                  SEXP kernel_mode,
                                                  SEXP z_lower,
                                                  SEXP z_upper,
                                                  SEXP sign, SEXP q,
                                                  SEXP phack_z_source,
                                                  SEXP phack_z_dest,
                                                  SEXP segment_bounds,
                                                  SEXP segment_step_bin,
                                                  SEXP segment_phack_region,
                                                  SEXP direction,
                                                  SEXP telescope_probabilities);
extern SEXP RoBMA_zcurve_normal_density_matrix(SEXP z_sequence,
                                               SEXP mean, SEXP sd,
                                               SEXP sei);
extern SEXP RoBMA_selnorm_zcurve_threshold_summary(SEXP z_threshold,
                                                   SEXP mean, SEXP sd,
                                                   SEXP sei, SEXP omega,
                                                   SEXP alpha,
                                                   SEXP phack_kind,
                                                   SEXP kernel_mode,
                                                   SEXP z_lower,
                                                   SEXP z_upper,
                                                   SEXP sign, SEXP q,
                                                   SEXP phack_z_source,
                                                   SEXP phack_z_dest,
                                                   SEXP segment_bounds,
                                                   SEXP segment_step_bin,
                                                   SEXP segment_phack_region,
                                                   SEXP extrapolate,
                                                   SEXP telescope_probabilities);
extern SEXP RoBMA_selnorm_zcurve_density_matrix(SEXP z_sequence,
                                                SEXP mean, SEXP sd,
                                                SEXP sei, SEXP omega,
                                                SEXP alpha,
                                                SEXP phack_kind,
                                                SEXP kernel_mode,
                                                SEXP z_lower,
                                                SEXP z_upper,
                                                SEXP sign, SEXP q,
                                                SEXP phack_z_source,
                                                SEXP phack_z_dest,
                                                SEXP segment_bounds,
                                                SEXP segment_step_bin,
                                                SEXP segment_phack_region,
                                                SEXP extrapolate,
                                                SEXP telescope_probabilities);

static R_NativePrimitiveArgType getjagsversions_t[] = {
    INTSXP, INTSXP, INTSXP, INTSXP
};

static const R_CMethodDef cMethods[] = {
    {"getjagsversions", (DL_FUNC) &getjagsversions, 4, getjagsversions_t},
    {NULL, NULL, 0, NULL}
};

static const R_CallMethodDef callMethods[] = {
    {"RoBMA_glmm_binom_marginal_loglik", (DL_FUNC) &RoBMA_glmm_binom_marginal_loglik, 11},
    {"RoBMA_glmm_pois_marginal_loglik",  (DL_FUNC) &RoBMA_glmm_pois_marginal_loglik,  11},
    {"RoBMA_glmm_binom_cluster_loglik", (DL_FUNC) &RoBMA_glmm_binom_cluster_loglik, 16},
    {"RoBMA_glmm_pois_cluster_loglik",  (DL_FUNC) &RoBMA_glmm_pois_cluster_loglik,  16},
    {"RoBMA_regplot_normal_mixture_interval", (DL_FUNC) &RoBMA_regplot_normal_mixture_interval, 3},
    {"RoBMA_regplot_selnorm_mixture_interval", (DL_FUNC) &RoBMA_regplot_selnorm_mixture_interval, 18},
    {"RoBMA_selnorm_kernel_loglik_matrix", (DL_FUNC) &RoBMA_selnorm_kernel_loglik_matrix, 22},
    {"RoBMA_selnorm_kernel_log_norm_matrix", (DL_FUNC) &RoBMA_selnorm_kernel_log_norm_matrix, 17},
    {"RoBMA_selnorm_kernel_cdf_matrix", (DL_FUNC) &RoBMA_selnorm_kernel_cdf_matrix, 19},
    {"RoBMA_selnorm_kernel_moments_matrix", (DL_FUNC) &RoBMA_selnorm_kernel_moments_matrix, 17},
    {"RoBMA_selnorm_kernel_rng_matrix", (DL_FUNC) &RoBMA_selnorm_kernel_rng_matrix, 17},
    {"RoBMA_selnorm_kernel_weighted_summary", (DL_FUNC) &RoBMA_selnorm_kernel_weighted_summary, 19},
    {"RoBMA_funnel_model_averaged_quantiles", (DL_FUNC) &RoBMA_funnel_model_averaged_quantiles, 21},
    {"RoBMA_zcurve_normal_density_matrix", (DL_FUNC) &RoBMA_zcurve_normal_density_matrix, 4},
    {"RoBMA_selnorm_zcurve_threshold_summary", (DL_FUNC) &RoBMA_selnorm_zcurve_threshold_summary, 19},
    {"RoBMA_selnorm_zcurve_density_matrix", (DL_FUNC) &RoBMA_selnorm_zcurve_density_matrix, 19},
    {NULL, NULL, 0}
};

void
R_init_RoBMA(DllInfo *dll)
{
    R_registerRoutines(dll, cMethods, callMethods, NULL, NULL);
    R_useDynamicSymbols(dll, FALSE);
}
