# dependencies
library(CSIndicators)

# Load functions
source("modules/Indicators/R/data_format_csindicators.R")
source("modules/Indicators/R/spei_spi.R")

Indicators <- function(recipe, data){
    
    var.list <- strsplit(recipe$Analysis$Variables$name, ', ')[[1]]
    ncores <- recipe$Analysis$ncores

    # SPEI
    if (!is.null(recipe$Analysis$Workflow$Indicators$SPEI$return_spei)){
        if (recipe$Analysis$Workflow$Indicators$SPEI$return_spei){
        
            # read recipe parameters
            pet_method <- recipe$Analysis$Workflow$Indicators$SPEI$PET_method
            if (pet_method == 'none'){pet_method <- NULL}
            spei_accum <- recipe$Analysis$Workflow$Indicators$SPEI$Nmonths_accum
            spei_standardization <- recipe$Analysis$Workflow$Indicators$SPEI$standardization
            spei_stand_refperiod <- recipe$Analysis$Workflow$Indicators$SPEI$standardization_ref_period
            if (is.null(spei_stand_refperiod)){
                spei_stand_refperiod <- c(recipe$Analysis$Time$hcst_start, recipe$Analysis$Time$hcst_end)
            }
            spei_stand_handleinf <- recipe$Analysis$Workflow$Indicators$SPEI$standardization_handle_infinity
            
            # call spei_spi function from modules/Indicators/R
            result_spei <- spei_spi(data = data, indicator = 'spei',
                                    var.list = var.list, 
                                    pet_method = pet_method, 
                                    accum = spei_accum, 
                                    standardization = spei_standardization,
                                    stand_refperiod = spei_stand_refperiod,
                                    stand_handleinf = spei_stand_handleinf,
                                    ncores = ncores)
                
        } else {
            result_spei <- NULL
        }
    } else {
        result_spei <- NULL
    }

    # SPI
    if (!is.null(recipe$Analysis$Workflow$Indicators$SPI$return_spi)){
        if (recipe$Analysis$Workflow$Indicators$SPI$return_spi){
        
            # read recipe parameters
            spi_accum <- recipe$Analysis$Workflow$Indicators$SPI$Nmonths_accum
            spi_standardization <- recipe$Analysis$Workflow$Indicators$SPI$standardization
            spi_stand_refperiod <- recipe$Analysis$Workflow$Indicators$SPI$standardization_ref_period
            if (is.null(spi_stand_refperiod)){
                spi_stand_refperiod <- c(recipe$Analysis$Time$hcst_start, recipe$Analysis$Time$hcst_end)
            }
            spi_stand_handleinf <- recipe$Analysis$Workflow$Indicators$SPI$standardization_handle_infinity
            
            # call spei_spi function from modules/Indicators/R
            result_spi <- spei_spi(data = data, indicator = 'spi',
                                    var.list = var.list, 
                                    pet_method = NULL, 
                                    accum = spi_accum, 
                                    standardization = spi_standardization,
                                    stand_refperiod = spi_stand_refperiod,
                                    stand_handleinf = spi_stand_handleinf,
                                    ncores = ncores)
                
        } else {
            result_spi <- NULL
        }
    } else {
        result_spi <- NULL
    }

    if (!is.null(result_spei) & !is.null(result_spi)){
        result <- list(result_spei, result_spi)
        names(result) <- c('SPEI', 'SPI')
    } else if (!is.null(result_spei)){
        result <- list(result_spei)
        names(result) <- 'SPEI'
    } else if (!is.null(result_spi)){
        result <- list(result_spi)
        names(result) <- 'SPI'
    } else {
        result <- NULL
    }

    return(result)
}
