<?php namespace Tussendoor\Reloadify\Controllers;

use ReloadifyScoped\Illuminate\Database\Eloquent\ModelNotFoundException;
use Tussendoor\Reloadify\Concerns\HasActions;
use Tussendoor\Reloadify\Models\AbandonedCart\AbandonedCart;
use Tussendoor\Reloadify\Plugin;

class AdminBarController extends Controller
{
    use HasActions;

    public function register()
    {
        add_action('wp_enqueue_scripts', [$this, 'loadAssets']);
        add_action('admin_bar_menu', [$this, 'addAdminBarRecoveryInfo'], 999);
    }

    /**
     * Load assets for this WooCommerce cart
     *
     * @see PublicController for the ajax handler
     *
     * @return void
     */
    public function loadAssets()
    {
        // Only for admin recovery
        if (empty(WC()->session) || empty(WC()->session->get('reloadify_admin_recovery'))) {
            return;
        }

        wp_enqueue_script(
            Plugin::config('plugin.tag').'_public_script',
            Plugin::config('plugin.assets').'public/js/public.js',
            ['jquery'],
            Plugin::config('plugin.version')
        );

        wp_localize_script(
            Plugin::config('plugin.tag').'_public_script',
            'AdminController',
            array(
                'ajaxurl'                   => admin_url('admin-ajax.php'),
                'forget_recovery_action'    => self::$forgetAdminRecoveryAction,
            ),
        );
    }

    /**
     * Add Reloadify information to the WordPress admin bar
     *
     * @param \WP_Admin_Bar $wpAdminBar
     * @return void
     */
    public function addAdminBarRecoveryInfo($wpAdminBar)
    {
        if (!is_super_admin() || !is_admin_bar_showing()) return;

        if (!function_exists('WC') || is_admin() || empty(WC()->session) || empty(WC()->session->get('reloadify_admin_recovery'))) return;

        $wpAdminBar->add_node([
            'id'    => 'reloadify_admin_bar_text',
            'title' => __('Cart recovery', 'reloadify'),
            'parent' => false,
            'group' => false,
            'meta'  => [
                'class' => 'reloadify-admin-bar-text',
            ],
        ]);

        // add node with an id so when the node is clicked we can start an ajax action
        $wpAdminBar->add_node([
            'id'    => 'reloadify_admin_bar_forget_recovery',
            'title' => __('Forget recovery', 'reloadify'),
            'href' => '#',
            'parent' => 'reloadify_admin_bar_text',
            'group' => false,
            'meta'  => [
                'class' => 'reloadify-admin-bar-forget-recovery',
            ],
        ]);

        try {
            $hash = sanitize_key(WC()->session->get('reloadify_admin_recovery'));
            $cart = AbandonedCart::where('hash', $hash)->firstOrFail();
        } catch (ModelNotFoundException $e) {
            return $wpAdminBar->add_node([
                'id' => 'reloadify_admin_bar_cart_hash',
                'parent' => 'reloadify_admin_bar_text',
                'title' => 'Hash: ' . $hash,
                'group' => false,
                'meta' => [
                    'class' => 'reloadify-admin-bar-text-cart-hash',
                ],
            ]);
        }

        $nodeBases = [
            'id',
            'currency',
            'created_at',
            'created_at_gmt',
            'updated_at',
            'hash',
        ];

        foreach ($nodeBases as $property) {
            $wpAdminBar->add_node([
                'id' => 'reloadify_admin_bar_cart_' . $property,
                'title' =>  $cart->{$property} . ' [' . $property . ']',
                'parent' => 'reloadify_admin_bar_text',
                'group' => false,
                'meta' => [
                    'class' => 'reloadify-admin-bar-text-cart-' . str_replace('_', '-', $property),
                ],
            ]);
        }

    }
}
