<?php

 /*
 * WooCommerce.php
 * 
 * This file is part of the "gotoviar" project.
 * See "LICENSE" for license information.
 */

/**
 * @package gotoviarplugin
 */
namespace GTVR\Handlers;

use \GTVR\Base\GTVR_BaseController;

// https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
// https://stackoverflow.com/questions/50135867/wp-rest-response-vs-wp-error
// https://stackoverflow.com/questions/42866573/get-the-number-of-items-in-cart-in-wordpress-using-woocommerce
// https://rudrastyh.com/woocommerce/get-number-of-items-in-cart.html

class GTVR_WooCommerce extends GTVR_BaseController
{

	public function register() 
	{
        add_action( 'rest_api_init', array ($this, 'register_handlers' ) );
	}

    public function register_handlers()
    {
        register_rest_route( 'gotoviar', '/system_name', array(
            'methods' => 'GET',
            'callback' => array ($this, 'system_name' ),
          ) );

        register_rest_route( 'gotoviar/v1', '/items_count_in_cart', array(
            'methods' => 'GET',
            'callback' => array ($this, 'items_count_in_cart' ),
          ) );

        register_rest_route( 'gotoviar/v1', '/products', array(
            'methods' => 'GET',
            'callback' => array ($this, 'products' ),
          ) );
    }

    public function system_name($data)
    {       
        $system_details['system_name'] = 'woocommerce';
        return $system_details;
    }

    public function items_count_in_cart($data)
    {       
        /**
         * Check if WooCommerce is activated
         */
        if ( ! function_exists( 'is_woocommerce_activated' ) )
        {
            if ( class_exists( 'woocommerce' ) )
            {
                return WC()->cart->get_cart_contents_count();
            }
            else
            {
                http_response_code(404);
                exit;
            }
        }
    }

    function get_woocommerce_currency_symbol( $currency = '' )
    { 
        if ( ! $currency ) { 
            $currency = get_woocommerce_currency(); 
        } 

        $symbols = apply_filters( 'woocommerce_currency_symbols', array( 
            'AED' => 'د.إ',  
            'AFN' => '؋',  
            'ALL' => 'L',  
            'AMD' => 'AMD',  
            'ANG' => 'ƒ',  
            'AOA' => 'Kz',  
            'ARS' => '$',  
            'AUD' => '$',  
            'AWG' => 'ƒ',  
            'AZN' => 'AZN',  
            'BAM' => 'KM',  
            'BBD' => '$',  
            'BDT' => '৳ ',  
            'BGN' => 'лв.',  
            'BHD' => '.د.ب',  
            'BIF' => 'Fr',  
            'BMD' => '$',  
            'BND' => '$',  
            'BOB' => 'Bs.',  
            'BRL' => 'R$',  
            'BSD' => '$',  
            'BTC' => '฿',  
            'BTN' => 'Nu.',  
            'BWP' => 'P',  
            'BYR' => 'Br',  
            'BZD' => '$',  
            'CAD' => '$',  
            'CDF' => 'Fr',  
            'CHF' => 'CHF',  
            'CLP' => '$',  
            'CNY' => '¥',  
            'COP' => '$',  
            'CRC' => '₡',  
            'CUC' => '$',  
            'CUP' => '$',  
            'CVE' => '$',  
            'CZK' => 'Kč',  
            'DJF' => 'Fr',  
            'DKK' => 'DKK',  
            'DOP' => 'RD$',  
            'DZD' => 'د.ج',  
            'EGP' => 'EGP',  
            'ERN' => 'Nfk',  
            'ETB' => 'Br',  
            'EUR' => '€',  
            'FJD' => '$',  
            'FKP' => '£',  
            'GBP' => '£',  
            'GEL' => 'ლ',  
            'GGP' => '£',  
            'GHS' => '₵',  
            'GIP' => '£',  
            'GMD' => 'D',  
            'GNF' => 'Fr',  
            'GTQ' => 'Q',  
            'GYD' => '$',  
            'HKD' => '$',  
            'HNL' => 'L',  
            'HRK' => 'Kn',  
            'HTG' => 'G',  
            'HUF' => 'Ft',  
            'IDR' => 'Rp',  
            'ILS' => '₪',  
            'IMP' => '£',  
            'INR' => '₹',  
            'IQD' => 'ع.د',  
            'IRR' => '﷼',  
            'IRT' => 'تومان',  
            'ISK' => 'kr.',  
            'JEP' => '£',  
            'JMD' => '$',  
            'JOD' => 'د.ا',  
            'JPY' => '¥',  
            'KES' => 'KSh',  
            'KGS' => 'сом',  
            'KHR' => '៛',  
            'KMF' => 'Fr',  
            'KPW' => '₩',  
            'KRW' => '₩',  
            'KWD' => 'د.ك',  
            'KYD' => '$',  
            'KZT' => 'KZT',  
            'LAK' => '₭',  
            'LBP' => 'ل.ل',  
            'LKR' => 'රු',  
            'LRD' => '$',  
            'LSL' => 'L',  
            'LYD' => 'ل.د',  
            'MAD' => 'د.م.',  
            'MDL' => 'MDL',  
            'MGA' => 'Ar',  
            'MKD' => 'ден',  
            'MMK' => 'Ks',  
            'MNT' => '₮',  
            'MOP' => 'P',  
            'MRO' => 'UM',  
            'MUR' => '₨',  
            'MVR' => '.ރ',  
            'MWK' => 'MK',  
            'MXN' => '$',  
            'MYR' => 'RM',  
            'MZN' => 'MT',  
            'NAD' => '$',  
            'NGN' => '₦',  
            'NIO' => 'C$',  
            'NOK' => 'kr',  
            'NPR' => '₨',  
            'NZD' => '$',  
            'OMR' => 'ر.ع.',  
            'PAB' => 'B/.',  
            'PEN' => 'S/.',  
            'PGK' => 'K',  
            'PHP' => '₱',  
            'PKR' => '₨',  
            'PLN' => 'zł',  
            'PRB' => 'р.',  
            'PYG' => '₲',  
            'QAR' => 'ر.ق',  
            'RMB' => '¥',  
            'RON' => 'lei',  
            'RSD' => 'дин.',  
            'RUB' => '₽',  
            'RWF' => 'Fr',  
            'SAR' => 'ر.س',  
            'SBD' => '$',  
            'SCR' => '₨',  
            'SDG' => 'ج.س.',  
            'SEK' => 'kr',  
            'SGD' => '$',  
            'SHP' => '£',  
            'SLL' => 'Le',  
            'SOS' => 'Sh',  
            'SRD' => '$',  
            'SSP' => '£',  
            'STD' => 'Db',  
            'SYP' => 'ل.س',  
            'SZL' => 'L',  
            'THB' => '฿',  
            'TJS' => 'ЅМ',  
            'TMT' => 'm',  
            'TND' => 'د.ت',  
            'TOP' => 'T$',  
            'TRY' => '₺',  
            'TTD' => '$',  
            'TWD' => 'NT$',  
            'TZS' => 'Sh',  
            'UAH' => '₴',  
            'UGX' => 'UGX',  
            'USD' => '$',  
            'UYU' => '$',  
            'UZS' => 'UZS',  
            'VEF' => 'Bs F',  
            'VND' => '₫',  
            'VUV' => 'Vt',  
            'WST' => 'T',  
            'XAF' => 'Fr',  
            'XCD' => '$',  
            'XOF' => 'Fr',  
            'XPF' => 'Fr',  
            'YER' => '﷼',  
            'ZAR' => 'R',  
            'ZMW' => 'ZK',  
        ) ); 

        $currency_symbol = isset( $symbols[ $currency ] ) ? $symbols[ $currency ] : ''; 

        return apply_filters( 'woocommerce_currency_symbol', $currency_symbol, $currency ); 
    } 

    function get_products()
    {
        // The query
        $products = new \WP_Query( array(
        'post_type'      => array('product'),
        'post_status'    => 'publish',
        'posts_per_page' => -1,
        'meta_query' => array( 
            array( 
             'value' => 'gtvr_resource_name', 
             'compare' => 'like'
            ), 
          ),
        ) );
        

        // The Loop
        # https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
        if ( $products->have_posts() ): while ( $products->have_posts() ):
            $products->the_post();
            $product = wc_get_product($products->post->ID);

            $prod["id"] = $product->get_id();
            $prod["price"] = $product->get_price() . ' ' . get_woocommerce_currency();
            $prod["name"] = $product->get_name();

            //get variations
            $variations = $product->get_children()[0];
            $prod['variations'] = $variations ? $variations : [];

            //get attributes
            $attributes = $product->get_attributes();
            $prod['attributes']=[];
            foreach ( $attributes as $attribute ):
                $attribute_data = $attribute->get_data();
                if ($attribute_data['name'] == 'gtvr_resource_name')
                    continue;
                $attr['name'] = $attribute_data['name'];
                $attr['value'] = $attribute_data['value'];
                $prod['attributes'][]= $attr;
            endforeach;

            $product_ids[$product->get_attribute('gtvr_resource_name')] = $prod;
        endwhile;
            wp_reset_postdata();
        endif;
        
        return $product_ids;
    }

    public function products($data)
    {       
        /**
         * Check if WooCommerce is activated
         */
        if ( ! function_exists( 'is_woocommerce_activated' ) )
        {
            if ( class_exists( 'woocommerce' ) )
            {
                return $this->get_products();
            }
            else
            {
                http_response_code(404);
                exit;
            }
        }
    }    
}