<?php

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

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

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

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

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

    return $items;
}

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

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

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

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

/**
* Fetch a single entity from database
*
* @param int   $id
*
* @return array
*/
function esi_get_all_countries() {
    global $wpdb;

    $defaults = array(
        'orderby'    => 'country_name',
        'order'      => 'ASC',
    );

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

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

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

    return $items;
}

function esi_get_all_entity_type_id() {
    global $wpdb;

    $defaults = array(
        'orderby'    => 'id',
        'order'      => 'ASC',
    );

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

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

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

    return $items;

}

/**
* Insert a entity entity
*
* @param array $args
*/
function esi_insert_entity( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'id'         => null,
        'name' => '',
        'description' => '',
        'url' => '',
        'is_investor' => '',
    );

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

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

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

    if ( ! $row_id ) {
        // insert a entity
        if ( $wpdb->insert( $table_name, $args ) ) {
            return $wpdb->insert_id;
        }

    } else {

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

    return false;
}

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

function sample_admin_notice_success() {
    ?>
    <div class="notice notice-success is-dismissible">
        <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
    </div>
    <?php
}

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


    // Get Selected industry mapping
    $arrSelectedIndustry = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_industry WHERE parent = " . $id . " ORDER BY name ASC");

    return $arrSelectedIndustry;
}


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


    // Get Selected industry mapping
    $item = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_industry WHERE id = " . $id . " ORDER BY name ASC");

    return $item;
}


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

    // Get Selected technology mapping
    $arrSelectedTechnology = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_technology ORDER BY name ASC");

    return $arrSelectedTechnology;
}

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

    // Get Selected technology mapping
    $arrSelectedTechnology = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_technology_entity  ORDER BY name ASC");

    return $arrSelectedTechnology;
}

/**
 * Fetch technologies array
 *
 * @param int   $id
 *
 * @return array
 */
function esi_get_list_selected_entity_technology( $id = 0, $output = 'object' ) {
    global $wpdb;

    // Get Selected technology mapping
    $arrSelectedTechnology = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_technology_entity WHERE entity_id = " . $id, OBJECT_K);

    if ($output == 'array') {

        $array = array();
        foreach ($arrSelectedTechnology as $selected) {
            $array [] = $selected->technology_id;
        }

        $arrSelectedTechnology = $array;
    }

    return $arrSelectedTechnology;
}

/**
 * Fetch technologies array
 *
 * @param int   $entityid
 * @param array $technologyids
 *
 * @return array
 */
function esi_save_list_entity_technology( $entityid = 0, $technologyids = array() ) {
    global $wpdb;

    // Delete all current join from liaison table
    $wpdb->query($wpdb->prepare('DELETE FROM ' . $wpdb->prefix . 'esi_technology_entity WHERE entity_id = %d', $entityid));

    // Insert new news_id technology_id relation
    foreach ($technologyids as $technology_id) {
        $wpdb->insert(
            $wpdb->prefix . "esi_technology_entity",
            array(
                'technology_id' => $technology_id,
                'entity_id' => intval($entityid)
            ));
    }
}

/**
 * Fetch technologies array
 *
 * @param int   $entity_id
 * @param array $tag_ids
 *
 * @return array
 */
function esi_save_list_entity_tag( $entity_id = 0, $tag_ids = array() ) {
    global $wpdb;

    // Delete all current join from liaison table
    $wpdb->query($wpdb->prepare('DELETE FROM ' . $wpdb->prefix . 'esi_tag_entity WHERE entity_id = %d', $entityid));

    // Insert new news_id tag_id relation
    foreach ($tag_ids as $tag_id) {
        $wpdb->insert(
            $wpdb->prefix . "esi_tag_entity",
            array(
                'tag_id' => $tag_id,
                'entity_id' => intval($entity_id)
            ));
    }
}

/**
 * Fetch technologies array
 *
 * @param int   $id
 *
 * @return array
 */
function esi_get_list_selected_entity_tag( $id = 0, $output = 'object' ) {
    global $wpdb;

    // Get Selected technology mapping
    $arrSelectedTechnology = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_tag_entity WHERE entity_id = " . $id, OBJECT_K);

    if ($output == 'array') {

        $array = array();
        foreach ($arrSelectedTechnology as $selected) {
            $array [] = $selected->tag_id;
        }

        $arrSelectedTechnology = $array;
    }

    return $arrSelectedTechnology;
}

/**
 * Fetch technologies array
 *
 * @param int   $id
 *
 * @return array
 */
function esi_get_list_selected_entity_industry( $id = 0, $output = 'object' ) {
    global $wpdb;

    // Get Selected technology mapping

    $arrSelectedIndustry = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix . "esi_entity_industry WHERE entity_id = " . $id, ARRAY_A);

    if ($output == 'array') {

        $array = array();
        foreach ($arrSelectedIndustry as $selected) {
            $array [] = $selected['industry_id'];
        }

        $arrSelectedIndustry = $array;
    }

    return $arrSelectedIndustry;
}

/**
 * Save industries array
 *
 * @param int   $entity_id
 * @param array $industry_ids
 *
 * @return array
 */
function esi_save_list_entity_industry( $entity_id = 0, $industry_ids = array()) {
    global $wpdb;

    // Delete all current join from liaison table
    $wpdb->query($wpdb->prepare('DELETE FROM ' . $wpdb->prefix . 'esi_entity_industry WHERE entity_id = %d', $entity_id));

    // Insert new news_id tag_id relation
    foreach ($industry_ids as $industry_id) {
        $wpdb->insert(
            $wpdb->prefix . "esi_entity_industry",
            array(
                'industry_id' => $industry_id,
                'entity_id' => intval($entity_id)
            ));
    }
}
