<?php

/**
* Get all radar
*
* @param $args array
*
* @return array
*/
function esi_get_all_radar( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'number'     => 20,
        'offset'     => 0,
        'orderby'    => 'id',
        'order'      => 'ASC',
    );

    $args      = wp_parse_args( $args, $defaults );
    $cache_key = 'radar-all';
    $items     = wp_cache_get( $cache_key, 'esi' );

    if ( false === $items ) {
        $items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'esi_radar ORDER BY ' . $args['orderby'] .' ' . $args['order'] .' LIMIT ' . $args['offset'] . ', ' . $args['number'] );

        wp_cache_set( $cache_key, $items, 'esi' );
    }

    return $items;
}

/**
* Fetch all radar from database
*
* @return array
*/
function esi_get_radar_count() {
    global $wpdb;

    return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'esi_radar' );
}

/**
* Fetch a single radar from database
*
* @param int   $id
*
* @return array
*/
function esi_get_radar( $id = 0 ) {
    global $wpdb;

    return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'esi_radar WHERE id = %d', $id ) );
}

/**
* Fetch industry radar array
*
* @param int   $id
*
* @return array
*/
function esi_get_radar_industry( $id = 0 ) {
    global $wpdb;

    // Get Selected industry mapping
    $arrSelectedIndustry = $wpdb->get_results("SELECT industry_id FROM " .$wpdb->prefix . "esi_radar_industry WHERE radar_id=".$id);
    if($arrSelectedIndustry){
        $arrSelectedIndustry = array_map(function($val) {
            return intval($val->industry_id);
        }, $arrSelectedIndustry);

        $arrIndustry = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_industry WHERE id IN (".join(',',$arrSelectedIndustry).")");
        return $arrIndustry;
    }else{
        return array();
    }
}

/**
* Fetch industry radar array
*
* @param int   $id
*
* @return array
*/
function esi_get_radar_industry_array( $id = 0 ) {
    global $wpdb;

    // Get Selected industry mapping
    $arrSelectedIndustry = $wpdb->get_results("SELECT industry_id FROM " .$wpdb->prefix . "esi_radar_industry WHERE radar_id=".$id);
    if($arrSelectedIndustry){
        return array_map(function($val) {
            return intval($val->industry_id);
        }, $arrSelectedIndustry);
    }else{
        return array();
    }
}

/**
* Fetch function radar array
*
* @param int   $id
*
* @return array
*/
function esi_get_radar_function( $id = 0 ) {
    global $wpdb;

    $arrSelectedFunction = $wpdb->get_results("SELECT function_id FROM " .$wpdb->prefix . "esi_radar_function WHERE radar_id=".$id);
    if($arrSelectedFunction){
        $arrSelectedFunction = array_map(function($val) {
            return intval($val->function_id);
        }, $arrSelectedFunction);

        $arrFunction = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_function WHERE id IN (".join(',', $arrSelectedFunction).")");
        return $arrFunction;
    }else {
        return array();
    }
}

/**
* Fetch function radar array
*
* @param int   $id
*
* @return array
*/
function esi_get_radar_function_array( $id = 0 ) {
    global $wpdb;

    $arrSelectedFunction = $wpdb->get_results("SELECT function_id FROM " .$wpdb->prefix . "esi_radar_function WHERE radar_id=".$id);
    if($arrSelectedFunction){
        return array_map(function($val) {
            return intval($val->function_id);
        }, $arrSelectedFunction);
    }else {
        return array();
    }
}

/**
* Insert a new radar
*
* @param array $args
*/
function esi_insert_radar( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'id' => null,
        'name' => '',
        'esi_industry_table' => array(),
        'esi_function_table' => array(),

    );

    $args       = wp_parse_args( $args, $defaults );
    $table_name = $wpdb->prefix . 'esi_radar';

    // some basic validation
    if ( empty( $args['name'] ) ) {
        return new WP_Error( 'no-name', __( 'No name provided.', 'esi' ) );
    }

    // remove row id to determine if new or update
    $row_id = (int) $args['id'];
    unset( $args['id'] );
    $esi_industry_table = $args['esi_industry_table'];
    unset($args['esi_industry_table']);
    $esi_function_table = $args['esi_function_table'];
    unset($args['esi_function_table']);

    if ( ! $row_id ) {

        // insert a new
        if ( $wpdb->insert( $table_name, $args ) ) {
            $row_id = $wpdb->insert_id;
            esi_save_radar_industry($row_id, $esi_industry_table);
            esi_save_radar_function($row_id, $esi_function_table);
            return $row_id;
        }

    } else {

        esi_save_radar_industry($row_id, $esi_industry_table);
        esi_save_radar_function($row_id, $esi_function_table);
        // do update method here
        if ( $wpdb->update( $table_name, $args, array( 'id' => $row_id ) ) ) {
            return $row_id;
        }
    }

    return false;
}


/**
* Save radar industry
*
* @param int   $id
* @param array   $industries
*/
function esi_save_radar_industry( $id = 0, $industries = array() ) {
    global $wpdb;

    $wpdb->delete( $wpdb->prefix . "esi_radar_industry", array( 'radar_id' => $id ) );
    foreach ($industries as $industry) {
        $wpdb->insert(
        $wpdb->prefix . "esi_radar_industry",
        array(
            'radar_id' => $id,
            'industry_id' => intval($industry)
        ));
    }
}

/**
* Save radar function
*
* @param int   $id
* @param array   $functions
*/
function esi_save_radar_function( $id = 0, $functions = array() ) {
    global $wpdb;

    $wpdb->delete( $wpdb->prefix . "esi_radar_function", array( 'radar_id' => $id ) );
    foreach ($functions as $function) {
        $wpdb->insert(
        $wpdb->prefix . "esi_radar_function",
        array(
            'radar_id' => $id,
            'function_id' => intval($function)
        ));
    }
}

/**
* Deletitem
*
* @param int   $id
*
*/
function esi_delete_radar( $id = 0 ) {
    global $wpdb;
    $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'esi_radar WHERE id = %d', $id ) );
}
