<?php
if (!defined('ABSPATH')) exit;

class TFL_Hook
{
    function __construct()
    {
        add_action('tfl_wsc_cart_content', array($this, 'tfl_get_cart_content'));
        add_action('wp_footer', array($this, 'tfl_get_cart_markup'));
        add_action('wp_enqueue_scripts', array($this, 'sidebar_cart_enqueue_scripts'));
        add_filter('woocommerce_add_to_cart_fragments', array($this, 'tfl_set_ajax_fragments'), 10, 1);
        add_action('wc_ajax_tfl_wsc_update_cart', array($this, 'update_cart_ajax'));
        add_action('wp_enqueue_scripts', array($this, 'tfl_front_style'));
        add_action('wp_enqueue_scripts', array($this, 'tfl_plugin_script'));
    }

    //search popup
    function tfl_front_style()
    {
        wp_register_style('tfl-public-style', TFL_PLG_PATH . 'assets/css/tfl-public-style.css', array(), '1.0.0', 'all');
    }

    function tfl_plugin_script()
    {
        wp_register_script('tfl-popup-js', TFL_PLG_PATH . 'assets/js/tfl-popup.min.js', array('jquery'), '', true);
    }

    function update_cart_ajax()
    {
        //Form Input Values
        $cart_key = sanitize_text_field($_POST['cart_key']);
        //If empty return error
        if (!$cart_key) {
            wp_send_json(array('error' => __('Something went wrong', 'side-cart-woocommerce')));
        }
        $cart_success = WC()->cart->remove_cart_item($cart_key);
        if ($cart_success) {
            WC_AJAX::get_refreshed_fragments();
        } else {
            if (wc_notice_count('error') > 0) {
                echo wc_print_notices();
            }
        }
        die();
    }

    function tfl_set_ajax_fragments($fragments)
    {
        //Get User Settings
        $options = get_option('tfl-wsc-gl-options');
        $show_count = isset($options['bk-show-bkcount']) ? $options['bk-show-bkcount'] : 1;
        if ($show_count == 1) {
            $count_value = WC()->cart->get_cart_contents_count();
        } else {
            $count_value = 0;
        }
        $cart_content = $this->tfl_get_cart_content();
        //Cart content
        $fragments['div.tfl-wsc-container'] = '<div class="tfl-wsc-container">' . $cart_content . '</div>';
        //Total Count
        $fragments['span.tfl-wsc-items-count'] = '<span class="tfl-wsc-items-count">' . $count_value . '</span>';
        return $fragments;
    }

    function tfl_get_cart_content()
    {
        $args = array(
            'options' => '',
            'sy_options' => ''
        );
        ob_start();
        if (function_exists('wc_get_template')) {
            wc_get_template('tfl-wsc-content.php', $args, '', TFL_PLG_DIR . 'template/');
            wc_get_template('tfl-wsc-footer.php', $args, '', TFL_PLG_DIR . 'template/');
        }
        return ob_get_clean();
    }

    function tfl_get_cart_markup()
    {
        require_once TFL_PLG_DIR . 'template/side-cart-html.php';
    }

    function sidebar_cart_enqueue_scripts()
    {

        wp_enqueue_script('sidebar-cart', TFL_PLG_PATH . 'assets/js/sidebar-cart.js', array('jquery'), '1.0.1', true);
        if (!wp_script_is('wc-add-to-cart', 'enqueued')) {
            wp_enqueue_script('wc-add-to-cart');
        }
        wp_enqueue_script( 'wc-cart-fragments' );
        wp_enqueue_script('wsc-public', TFL_PLG_PATH . 'assets/js/tfl-wsc-public.min.js', array('jquery'), '', true);
        wp_enqueue_script('tfl-ajax', TFL_PLG_PATH . 'assets/js/tfl-main-ajax.js', array('jquery'), '', true);
        wp_localize_script('wsc-public', 'tfl_wsc_localize', array(
                'adminurl' => admin_url() . 'admin-ajax.php',
                'wc_ajax_url' => (class_exists('WC_AJAX') ? WC_AJAX::get_endpoint("%%endpoint%%") : null),
                'ajax_atc' => 0,
                'added_to_cart' => false,
                'auto_open_cart' => 0,
                'atc_icons' => 0,
                'atc_reset' => 1,
                'trigger_class' => null
            )
        );
        wp_enqueue_style('wsc-public', TFL_PLG_PATH . 'assets/css/tfl-wsc-public.css', array(), '', 'all');

    }
}

$tfl_hook = new TFL_Hook();