<?php
namespace Wpupgrader\WordPress\Plugins\GdprConsentLite\Hooks\Actions;

use Wpupgrader\Libraries\WordPress\Plugins\Core\Hooks\AbstractAction;
use Wpupgrader\Libraries\WordPress\Plugins\Core\Notices;
use Wpupgrader\WordPress\Plugins\GdprConsentPlugin\GdprConsentPlugin;

class init extends AbstractAction
{
    /**
     * This method is invoked by Wordpress
     */
    static public function invoke()
    {
        static::initDefaults();
        static::registerCptConsent();
        static::upgradeProLink();
    }

    static protected function initDefaults()
    {
        /**
         * INSERT DEFAULT OPTIONS (ONLY ONCE, EVER)
         */
        if (empty(\get_option('gdpr-consent-settings'))) {
            $defaultSettings = [
                // Default Theme settings
                "gdprc_consent_theme_text"                    => "#222222",
                "gdprc_consent_theme_background"              => "#f5f5f5",
                "gdprc_consent_theme_border"                  => "#eeeeee",
                "gdprc_consent_theme_consent_checker"         => "#3a9e69",
                "gdprc_consent_theme_consent"                 => "#333333",
                "gdprc_consent_theme_consent_text"            => "#333333",
                "gdprc_consent_theme_button"                  => "#222222",
                "gdprc_consent_theme_button_text"             => "#ffffff",
                "gdprc_consent_theme_button_active"           => "#767676",
                "gdprc_consent_theme_button_text_active"      => "#ffffff",
                "gdprc_consent_theme_link"                    => "#222222",
                "gdprc_consent_theme_link_hover"              => "#767676",

                // Default Interface settings
                "gdprc_consent_interface_show_accept_all"     => "0",
                "gdprc_consent_interface_use_shadows"         => "0",
                "gdprc_consent_interface_use_rounded_corners" => \gdprConsentPluginIsProEdition() ? "1" : "0",
                "gdprc_consent_interface_use_toggles"         => \gdprConsentPluginIsProEdition() ? "1" : "0",
                "gdprc_consent_interface_use_animations"      => \gdprConsentPluginIsProEdition() ? "1" : "0",
                "gdprc_consent_interface_title"               => "GDPR Consent Settings",
                "gdprc_consent_interface_description"         => "Edit your consent settings below.",
                "gdprc_consent_interface_embed_disabled"      => '<p><i class="gdpri gdpri-ban"></i> No consent. This feature is disabled by GDPR</p>',
                "gdprc_consent_interface_accept_all"          => "Accept all",
                "gdprc_consent_interface_view_details"        => "View details",
                "gdprc_consent_interface_apply_button"        => "Change my permissions",
                "gdprc_consent_interface_apply_embed"         => "Accept and show content",
                "gdprc_consent_interface_apply_confirmation"  => "All done!",
                "gdprc_consent_interface_reset_button"        => "Forget my settings",
                "gdprc_consent_interface_reset_confirmation"  => "Deleted!",

                // Default Permission Bar settings
                "gdprc_permission_bar_enable"                 => "1",
                "gdprc_permission_bar_consents_enable"        => "0",
                "gdprc_permission_bar_title"                  => "GDPR Consent",
                "gdprc_permission_bar_desc"                   => "Edit your consent settings or view privacy policy.",
                "gdprc_permission_bar_accept_label"           => "Accept",
                "gdprc_permission_bar_close_label"            => "Close",
                "gdprc_permission_bar_settings_label"         => "Settings",
                "gdprc_permission_bar_privacy_label"          => "Privacy policy",
            ];

            /**
             * INSERT GDPR-SETTINGS-PAGE (ONLY ONCE, EVER)
             */
            if (empty(\get_option('gdprc_firsttime_setup')) && empty(\get_option('wp_gdprcspage'))) {
                // Ad Settings page
                $wp_gdprcspage_id = \wp_insert_post([
                    'post_title'     => "GDPR Consent Settings",
                    'post_content'   => "[gdpr_consent_settings]",
                    'post_status'    => 'publish',
                    'post_type'      => 'page',
                    'post_name'      => "gdpr-settings",
                    'comment_status' => 'closed',
                    'ping_status'    => 'closed',
                ]);
                $defaultSettings["gdprc_consent_interface_page"] = (string)$wp_gdprcspage_id;
                \delete_option('wp_gdprcspage'); // backward compatibility
                \update_option('gdprc_firsttime_setup', 1);

                // Add demo Consent
                $demo_consent_id = \wp_insert_post([
                    'post_title'     => "Demo Consent",
                    'post_content'   => "This consent may be removed or used for testing. It is here for demonstration purposes.",
                    'post_status'    => 'publish',
                    'post_type'      => 'gdprc_consent',
                    'post_name'      => "test-consent",
                    'comment_status' => 'closed',
                    'ping_status'    => 'closed',
                ]);
                \add_post_meta($demo_consent_id, "gdprc_selected_icon", "gdpri-users");
            }

            \update_option("gdpr-consent-settings", $defaultSettings);
        }

        if (empty(\get_option('gdpr-consent-version')) || \version_compare(\get_option('gdpr-consent-version'), GdprConsentPlugin::VERSION)) {
            Notices::get_instance()->register_notice('<img src="'.\get_gdprc_admin_icon().'" style="vertical-align: bottom;" width="18" height="18" /> <b>GDPR Consent!</b> The plugin was newly installed or updated to version '
                                                     .GdprConsentPlugin::VERSION.'. <b>Please save/update the <a href="edit.php?post_type=gdprc_consent&page=options-general.php%3Fpage%3Dgdpr-consent-settings">Settings</a>!</b> ')
            ;
        }
    }

    static protected function registerCptConsent()
    {
        $labels = [
            'name'                  => \_x('GDPR Consent', 'Post Type General Name', 'gdprc_consent'),
            'singular_name'         => \_x('GDPR Consent', 'Post Type Singular Name', 'gdprc_consent'),
            'menu_name'             => \__('GDPR Consent', 'gdprc_consent'),
            'name_admin_bar'        => \__('GDPR Consent', 'gdprc_consent'),
            'archives'              => \__('Consent Archives', 'gdprc_consent'),
            'attributes'            => \__('Consent Attributes', 'gdprc_consent'),
            'parent_item_colon'     => \__('Parent Consent:', 'gdprc_consent'),
            'all_items'             => \__('All Consents', 'gdprc_consents'),
            'add_new_item'          => \__('Add New Consent', 'gdprc_consents'),
            'add_new'               => \__('Add New Consent', 'gdprc_consents'),
            'new_item'              => \__('New Consent', 'gdprc_consent'),
            'edit_item'             => \__('Edit Consent', 'gdprc_consent'),
            'update_item'           => \__('Update Consent', 'gdprc_consent'),
            'view_item'             => \__('View Consent', 'gdprc_consent'),
            'view_items'            => \__('View Consents', 'gdprc_consent'),
            'search_items'          => \__('Search Consent', 'gdprc_consent'),
            'not_found'             => \__('Not found', 'gdprc_consent'),
            'not_found_in_trash'    => \__('Not found in Trash', 'gdprc_consent'),
            'featured_image'        => \__('Featured Image', 'gdprc_consent'),
            'set_featured_image'    => \__('Set featured image', 'gdprc_consent'),
            'remove_featured_image' => \__('Remove featured image', 'gdprc_consent'),
            'use_featured_image'    => \__('Use as featured image', 'gdprc_consent'),
            'insert_into_item'      => \__('Insert into Consent', 'gdprc_consent'),
            'uploaded_to_this_item' => \__('Uploaded to this Consent', 'gdprc_consent'),
            'items_list'            => \__('Consents list', 'gdprc_consent'),
            'items_list_navigation' => \__('Consents list navigation', 'gdprc_consent'),
            'filter_items_list'     => \__('Filter Consents list', 'gdprc_consent'),
        ];

        $args = [
            'label'               => \__('GDPR Consent', 'gdprc_consent'),
            'description'         => \__('Allows to set up varying choices of consents that a visitor may give to be of use in the site', 'gdprc_consent'),
            'labels'              => $labels,
            'supports'            => ['title', 'editor'],
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'menu_position'       => 80,
            'menu_icon'           => \get_gdprc_admin_icon(),
            'show_in_admin_bar'   => false,
            'show_in_nav_menus'   => false,
            'can_export'          => true,
            'has_archive'         => false,
            'exclude_from_search' => true,
            'publicly_queryable'  => false,
            'capability_type'     => 'page',
        ];

        \register_post_type('gdprc_consent', $args);
    }

    protected static function upgradeProLink()
    {
        \add_action('admin_menu', function() {
            \add_submenu_page('edit.php?post_type=gdprc_consent', 'gdpr-consent', '<b><u>Upgrade to Pro!</u></b>', 'publish_posts', 'https://www.wpupgrader.com/wordpress-plugins/gdpr-consent/#pricing');
        });
    }
}
