<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       
 * @since      1.0.0
 *
 * @package    Tarif_Klik_Logistics
 * @subpackage Tarif_Klik_Logistics/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Tarif_Klik_Logistics
 * @subpackage Tarif_Klik_Logistics/public
 * @author     
 */
class Tarif_Klik_Logistics_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	private $base_url;
	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of the plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;
		$this->$base_url = 'https://spreadsheets.google.com/feeds/list/15WWKrdvcfuvAMRe9MfSgyvqgvhV6qdaPIO90zgzRbyo/od6/public/values?alt=json';
		
	}
	public function endpoint_register()
    {
        // read all settings
        register_rest_route('tarif_klik/v1', '/tariff', array(
            'methods' => "GET",
            'callback' => array( $this, 'tariff' ),
            'permission_callback' => '__return_true'
        ));
    }

    /**
     * Handle get tariff
     * 
     * @param object $request Request object.
     */
    public function tariff( $request ) {
    	$content = wp_remote_get( $this->$base_url );
		if ( ! is_wp_error( $content ) ) {
			$body = json_decode( $content['body'], true );
			if ( isset( $body['feed']['entry'] ) ) {
				if(is_array( $body['feed']['entry'] )){
					$origin = array_keys(array_column($body['feed']['entry'], 'gsx$asal'), array('$t' => $request['origin']));
					$result_origin = [];
					foreach ($origin as $value) {
						$result_origin[] = $body['feed']['entry'][$value];
					}
					$destination = array_keys(array_column($result_origin, 'gsx$tujuan'), array('$t' => $request['destination']));
					$result_destination = [];
					foreach ($destination as $value) {
						$result_destination[] = $result_origin[$value];
					}
					$result = $result_destination;
					if(!empty($request['weight'])){
						$res = [];
						foreach ($result as $key) {
							if( intval($key['gsx$minimumcharge']['$t']) <= $request['weight']){
								$res[] = $key;
							}
						}
						$result = $res;
					}
					return array(
						'status'    => true,
						'data'      => $result,
					);
				}
			}
		}
    }
	public function tarif_klik_shortcode( $atts, $content ){
		$content = wp_remote_get( $this->$base_url );
		if ( ! is_wp_error( $content ) ) {
			$body = json_decode( $content['body'] );
			if ( isset( $body->feed->entry ) ) {
				if(is_array( $body->feed->entry )){
					$origin = [];
					$destination = [];
					$weight = [];
					foreach ($body->feed->entry as $item) {
						$item = (array) $item;
						$title = (array) $item['title'];
						if($title['$t'] != 'Row: 2'){
							$the_origin = (array) $item['gsx$asal'];
							$the_detination = (array) $item['gsx$tujuan'];
							$the_weight = (array) $item['gsx$minimumcharge'];
							if(array_search($the_origin['$t'], array_column($origin, 'value')) === false  && !empty($the_origin['$t'])){
								$obj_origin = (object) ['value' => $the_origin['$t'], 'data' => $the_origin['$t']];
								$origin[] = $obj_origin;
							}
							if(array_search($the_detination['$t'], array_column($destination, 'value')) === false  && !empty($the_detination['$t'])){
								$obj_dest = (object) ['value' => $the_detination['$t'], 'data' => $the_detination['$t']];
								$destination[] = $obj_dest;
							}
							if(array_search( (string)intval( $the_weight['$t'] ), array_column($weight, 'value')) === false  && !empty($the_weight['$t'])){
								$obj_dest = (object) ['value' => (string)intval( $the_weight['$t'] ), 'data' => (string)intval( $the_weight['$t'] )];
								$weight[] = $obj_dest;
							}
						}
					}
					wp_localize_script( $this->plugin_name, 'tarif_klik_origin', $origin );
					wp_localize_script( $this->plugin_name, 'tarif_klik_destination', $destination );
					wp_localize_script( $this->plugin_name, 'tarif_klik_weight', $weight );
					ob_start();
					include plugin_dir_path(__FILE__) . '../public/partials/tarif-klik-logistics-public-display.php';
			        $return .= ob_get_clean();
			        return $return;
				}
				return false;
			}
		}
		return false;
	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Tarif_Klik_Logistics_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Tarif_Klik_Logistics_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/tarif-klik-logistics-public.css', array(), $this->version, 'all' );

	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Tarif_Klik_Logistics_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Tarif_Klik_Logistics_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_script( 'autocomplet', plugin_dir_url( __FILE__ ) . 'js/jquery.autocomplete.min.js', array( 'jquery' ), $this->version, false );
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/tarif-klik-logistics-public.js', array( 'jquery' ), $this->version, true );
		$api = get_site_url(null, '/wp-json/tarif_klik/v1/tariff');
		wp_localize_script( $this->plugin_name, 'tarif_klik_api', $api );
	}

}
