<?php

/**
 * The admin-facing functionality of the plugin.
 *
 * @link       https://themeatelier.net
 * @since      1.0.0
 *
 * @package greet-bubble
 * @subpackage greet-bubble/Admin
 * @author     ThemeAtelier<themeatelierbd@gmail.com>
 */

namespace ThemeAtelier\GreetBubble\Admin;

use ThemeAtelier\GreetBubble\Admin\DBUpdates;
use ThemeAtelier\GreetBubble\Admin\Helpers\ReviewNotice;
use ThemeAtelier\GreetBubble\Admin\Helpers\ThemeAtelier_Offer_Banner;
use ThemeAtelier\GreetBubble\Admin\Views\AssignBubble;
use ThemeAtelier\GreetBubble\Admin\Views\BubbleMetaboxOptions;
use ThemeAtelier\GreetBubble\Admin\Views\GetHelp;
use ThemeAtelier\GreetBubble\Admin\Views\GreetBubbleOptions;
use ThemeAtelier\GreetBubble\Includes\Helpers;

/**
 * The admin class
 */
class Admin
{
    /**
     * The slug of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $plugin_slug   The slug of this plugin.
     */
    private $plugin_slug;

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

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

    /**
     * The class constructor.
     *
     * @param string $plugin_slug The slug of the plugin.
     * @param string $version Current version of the plugin.
     */
    function __construct($plugin_slug, $version)
    {
        $this->plugin_slug = $plugin_slug;
        $this->version     = $version;
        $this->min         = defined('WP_DEBUG') && WP_DEBUG ? '' : '.min';
        new DBUpdates();
        new ReviewNotice();
        // Move initialization to after_setup_theme hook
        add_action('after_setup_theme', array($this, 'init_components'));
        add_action('after_setup_theme', array($this, 'assign_bubble_configs'));
        add_action('admin_menu', array($this, 'add_plugin_page'));
        add_action('activated_plugin', array($this, 'greet_bubble_redirect_to'));
        add_filter('admin_footer_text', array($this, 'admin_footer'), 1, 2);
        $active_plugins = get_option('active_plugins');
        foreach ($active_plugins as $active_plugin) {
            $_temp = strpos($active_plugin, 'greet-bubble.php');
            if (false != $_temp) {
                add_filter('plugin_action_links_' . $active_plugin, array($this, 'greet_bubble_plugin_action_links'));
                add_filter('plugin_row_meta', array($this, 'after_greet_bubble_row_meta'), 10, 4);
            }
        }

        if (! defined('THEMEATELIER_OFFER_BANNER_LOADED')) {
            define('THEMEATELIER_OFFER_BANNER_LOADED', true);
            ThemeAtelier_Offer_Banner::instance();
        }
        add_action('after_setup_theme', array($this, 'manage_bubble_metabox_configs'));
        add_action('init', array($this, 'register_greet_bubble_post_type'));
        add_filter('parent_file', array($this, 'greet_bubble_highlight_menu'));
        add_filter('submenu_file', array($this, 'greet_bubble_submenu_highlight'));
    }

    public function register_greet_bubble_post_type()
    {
        if (post_type_exists('manage-bubble')) {
            return;
        }
        $capability = Helpers::greet_bubble_dashboard_capability();
        $show_ui =  current_user_can($capability) ? true : false;
        // Set the greet bubble post type labels.
        $labels = apply_filters(
            'greet_bubble_post_type_labels',
            array(
                'name'               => esc_html__('Manage Bubble', 'greet-bubble'),
                'singular_name'      => esc_html__('Manage Bubble', 'greet-bubble'),
                'menu_name'          => esc_html__('Manage Bubble', 'greet-bubble'),
                'all_items'          => esc_html__('All Bubbles', 'greet-bubble'),
                'add_new'            => esc_html__('Add Bubble', 'greet-bubble'),
                'add_new_item'       => esc_html__('Add New Bubble', 'greet-bubble'),
                'new_item'           => esc_html__('Add New Bubble', 'greet-bubble'),
                'edit_item'          => esc_html__('Edit Generated Bubble', 'greet-bubble'),
                'view_item'          => esc_html__('View Generated Bubble', 'greet-bubble'),
                'name_admin_bar'     => esc_html__('Bubble Generator', 'greet-bubble'),
                'search_items'       => esc_html__('Search Generated Bubble', 'greet-bubble'),
                'parent_item_colon'  => esc_html__('Parent Generated Bubble:', 'greet-bubble'),
                'not_found'          => esc_html__('No Bubble found.', 'greet-bubble'),
                'not_found_in_trash' => esc_html__('No Bubble found in Trash.', 'greet-bubble')
            )
        );

        $args      = apply_filters(
            'greet_bubble_post_type_args',
            array(
                'label'           => esc_html__('Manage Bubble', 'greet-bubble'),
                'description'     => esc_html__('Manage Bubble', 'greet-bubble'),
                'public'          => false,
                'show_ui'         => $show_ui,
                'show_in_nav_menus'  => false,
                'show_in_menu'    => false,
                'menu_icon'       => 'dashicons-calendar',
                'hierarchical'    => false,
                'query_var'       => false,
                'menu_position'   => 7,
                'supports'        => array('title'),
                'capabilities'    => array(
                    'publish_posts'       => $capability,
                    'edit_posts'          => $capability,
                    'edit_others_posts'   => $capability,
                    'delete_posts'        => $capability,
                    'delete_others_posts' => $capability,
                    'read_private_posts'  => $capability,
                    'edit_post'           => $capability,
                    'delete_post'         => $capability,
                    'read_post'           => $capability,
                ),
                'capability_type' => 'post',
                'rewrite'         => array('slug' => 'manage-bubble'),
                'labels'          => $labels,
            )
        );

        register_post_type('manage_bubble', $args);
        flush_rewrite_rules();
    }

    /**
     * Initialize plugin components
     *
     * @since    1.0.0
     */
    public function manage_bubble_metabox_configs()
    {
        BubbleMetaboxOptions::section('greet_meta');
    }
    public function init_components()
    {
        GreetBubbleOptions::options('_greet');
        GetHelp::options('greet_bubble_help');
    }

    public function assign_bubble_configs()
    {
        AssignBubble::settings('assign_bubble');
    }

    /**
     * Register the stylesheets for the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public static function enqueue_scripts($hook)
    {
        if ('toplevel_page_greet-bubble' == $hook || 'toplevel_page_greet-bubble-help') {

            wp_enqueue_style('greet-help');
        }
        wp_enqueue_style('greet-global-admin');
    }


    public function add_plugin_page()
    {
        add_menu_page(
            esc_html__('Greet Bubble', 'greet-bubble'),
            esc_html__('Greet Bubble', 'greet-bubble'),
            'manage_options',
            'greet-bubble',
            array($this, 'greet_bubble_setting'),
            'dashicons-format-video',
            30
        );
        add_submenu_page(
            'greet-bubble',
            esc_html__('Global Settings', 'greet-bubble'),
            esc_html__('Global Settings', 'greet-bubble'),
            'manage_options',
            'greet-bubble',
        );

        add_submenu_page(
            'greet-bubble',
            __('Manage Bubbles', 'greet-bubble'),
            __('Manage Bubble', 'greet-bubble'),
            'manage_options',
            'edit.php?post_type=manage_bubble'
        );

        add_submenu_page(
            'greet-bubble',
            __('Assign Bubbles', 'greet-bubble'),
            __('Assign Bubble', 'greet-bubble'),
            'manage_options',
            'assign-bubble',
            array($this, 'greet_bubble'),
        );
        add_submenu_page(
            'greet-bubble',
            __('Help', 'greet-bubble'),
            __('Help', 'greet-bubble'),
            'manage_options',
            'greet-bubble-help',
            array($this, 'greet_bubble'),
        );

        do_action('greet_bubble_before_upgrade_pro_menu');
        add_submenu_page('greet-bubble', __('Upgrade To Premium', 'greet-bubble'), sprintf('<span class="greet-bubble-pro-text">%s</span>', __('Upgrade To Pro! 👑', 'greet-bubble')), 'manage_options', 'https://themeatelier.net/downloads/greet-bubble#pricing?utm_source=greet_plugin&utm_medium=submenu_page&utm_campaign=regular');
        do_action('greet_bubble_after_upgrade_pro_menu');
    }

    public function greet_bubble_highlight_menu($parent_file)
    {
        global $current_screen;
        if ($current_screen->post_type === 'manage_bubble') {
            $parent_file = 'greet-bubble';
        }
        return $parent_file;
    }
    public function greet_bubble_submenu_highlight($submenu_file)
    {
        global $current_screen;
        if ($current_screen->post_type === 'manage_bubble') {
            $submenu_file = 'edit.php?post_type=manage_bubble';
        }
        return $submenu_file;
    }

    /**
     * Options page callback
     */
    public function greet_bubble_setting() {}
    public function greet_bubble() {}

    public function greet_bubble_redirect_to($plugin)
    {
        if (GREET_BUBBLE_BASENAME === $plugin) {
            $redirect_url = esc_url(admin_url('admin.php?page=greet-bubble-help'));
            exit(wp_kses_post(wp_safe_redirect($redirect_url)));
        }
    }

    // Plugin settings in plugin list
    public function greet_bubble_plugin_action_links(array $links)
    {
        $new_links = array(
            sprintf('<a href="' . esc_url(admin_url('admin.php?page=greet-bubble#tab=general')) . '">' . esc_html__('Settings', 'greet-bubble') . '</a>'),
            // sprintf('<a target="_blank" href="https://themeatelier.net/contact">' . esc_html__('Support', 'greet-bubble') . '</a>'),
            // sprintf('<a style="font-weight: bold;color:#7432ff" target="_blank" href="https://themeatelier.net/downloads/greet-bubble">' . esc_html__('Go Pro!', 'greet-bubble') . '</a>'),
        );
        $links[] = sprintf('<a style="font-weight: bold;color:#35b747" target="_blank" href="https://themeatelier.net/downloads/greet-bubble?utm_source=greet_plugin&utm_medium=action_link&utm_campaign=regular">%s</a>', esc_html__('Go Pro!', 'greet-bubble'));
        return array_merge($new_links, $links);
    }


    /**
     * Add plugin row meta link.
     *
     * @since 2.0
     *
     * @param array  $plugin_meta .
     * @param string $file .
     *
     * @return array
     */
    public function after_greet_bubble_row_meta($plugin_meta, $file)
    {

        if (GREET_BUBBLE_BASENAME === $file) {
            $plugin_meta[] = '<a href="' . GREET_BUBBLE_DEMO_URL . '" target="_blank">' . __('Live Demo', 'greet-bubble') . '</a>';
        }

        return $plugin_meta;
    }

    /**
     * Review Text.
     *
     * @param string $text text.
     *
     * @return string
     */
    public function admin_footer($text)
    {

        $screen = get_current_screen();

        if ('toplevel_page_greet-bubble' === $screen->id) {
            $text = sprintf(
                /* translators: 1: start strong tag, 2: close strong tag. 3: start link 4: close link */
                __('<i>Enjoying %1$sGreet Bubble?%2$s Please rate us %3$sWordPress.org%4$s. Your positive feedback will help us grow more. Thank you! 😊</i>', 'greet-bubble'),
                '<strong>',
                '</strong>',
                '<span class="greet-footer-text-star">★★★★★</span> <a href="https://wordpress.org/support/plugin/greet-bubble/reviews/#new-post" target="_blank">',
                '</a>'
            );
        }

        return $text;
    }
}
