<?php
/**
 * Initialization Action.
 * @package FASTX
 * @since v.1.0.0
 */
namespace FASTX;

defined('ABSPATH') || exit;

/**
 * FASTX_Initialization class.
 */
class FASTX_Initialization {

    /**
	 * Setup class.
	 * @since v.1.0.0
	 */
    public function __construct() {
        $this->add_block_styles();
        add_action( 'after_setup_theme', array( $this, 'setup_func' ) );
        add_action( 'admin_menu',              [ $this, 'add_theme_menu_page_callback' ] );         //  add theme menu page
        add_action( 'enqueue_block_assets',    [ $this, 'fastx_theme_enqueue_style_sheet' ] );     //enqueue styles in backend
        add_action( 'wp_ajax_ultp_install',    [ $this, 'ultp_install_callback' ] );               // install PostX plugin
        add_action( 'wp_ajax_ultp_activate',   [ $this, 'ultp_activate_callback' ] );              // activate PostX plugin
        add_action( 'wp_ajax_disbale_postx_notice',   [ $this, 'disbale_postx_notice_func' ] );              // activate PostX plugin
    }

    /**
	 * Setup function to handle theme notice
     * 
     * @since v1.0.0
	 * @return styles
	*/
    public function disbale_postx_notice_func() {
		update_option('fastx_postx_install_notice', 'no');
        return [ 'success' => true ];
	}
    /**
	 * Setup function to load language
     * 
     * @since v1.0.0
	 * @return styles
	*/
    public function setup_func() {
        $this->register_pattern_categories();
        $this->register_patterns();
		load_theme_textdomain( 'fastx', FASTX_PATH . '/languages' );
	}
    /**
	 * register_pattern_categories
     * 
     * @since v1.0.0
	 * @return styles
	*/
    
    public function register_pattern_categories() {
        $categories = [
            'fastx' => [
                'label'          => __('FastX Patterns', 'fastx'),
                'description'    => __('Fastx patterns', 'fastx')
            ]
        ];
        foreach( $categories as $slug => $val ) {
            if ( \WP_Block_Pattern_Categories_Registry::get_instance()->is_registered($slug) ) {
                continue;
            }
            register_block_pattern_category( $slug, $val );
        }
    }
    /**
	 * register_patterns
     * 
     * @since v1.0.0
	 * @return styles
	*/
    
    public function register_patterns() {

        $patterns = [
            'header-v1',
            'header-v2',
            'header-v3',
            'header-v4',
            'footer-v1',
            'footer-v2',
            'footer-v3',
        ];
        foreach( $patterns as $pattern ) {
            $file = FASTX_PATH.'/patterns/more_patterns/'.$pattern.'.php';
            if ( is_file($file) ) {
                register_block_pattern('fastx/'.$pattern, require $file );
            }
        }
    }

    /**
	 * Add global css for blocks, elements
     * 
     * @since v1.0.0
	 * @return styles
	*/
    public function fastx_theme_enqueue_style_sheet() {
        wp_register_style( 'fastx-style', FASTX_DIR . '/assets/styles.css', array(), FASTX_VER );
        wp_enqueue_style( 'fastx-style' );
    }
    /**
     * Add theme menu page
     * @since v1.0.0
	 * @return menu page
	*/
    public function add_theme_menu_page_callback() {
        add_theme_page(
            __( 'FastX Theme', 'fastx' ),
            __( 'FastX Theme', 'fastx' ),
            'edit_theme_options',
            'fastx-settings',
            array( $this, 'fastx_theme_page' )
        );
    }

    /**
     * Add fastx_theme_page
     * @since v1.0.0
	 * @return page_content
	*/
    public function fastx_theme_page() {
        wp_enqueue_style( "fastx-themepage", FASTX_DIR . '/assets/css/themepage.css', array(), FASTX_VER, 'all' );
        wp_enqueue_script( "fastx-themepage", FASTX_DIR . '/assets/js/themepage.js', array('jquery'), FASTX_VER, 'all' );
        ?>
        <div class="fsx_theme_page_wrapper fsx_theme_submenu">
            <div class="fsx_theme_page_content">
                <!-- welcome section -->
                <div class="fsx_theme_welcome">
                    <div class="fsx_theme_welcome_left">
                        <div class="fsx_wc_title_sm">Hello, admin</div>
                        <div class="fsx_wc_title_wrap">
                            <div class="fsx_wc_title"><?php echo esc_html__( 'Welcome to', 'fastx' ) ?> FastX <?php echo esc_html__( 'Theme', 'fastx' ) ?></div>
                            <div class="fsx_wc_tag"><?php echo esc_html__( 'FREE', 'fastx' ) ?></div>
                        </div>
                        <div class="fsx_theme_descp"><?php echo esc_html__( 'FastX allows you to build a fast-loading website effortlessly. You can get started by choosing from 10 different styles of color and typography. The full site editor lets you create templates for all essential pages. FastX is also integrated with PostX, which allows you to build complete sites in three simple steps.', 'fastx' ) ?></div>
                        <div class="fsx_wc_btns">
                            <div class="fsx_wc_imports">
                                <?php
                                    if( !file_exists(WP_PLUGIN_DIR.'/ultimate-post/ultimate-post.php') ) {
                                        ?> 
                                            <a id="fsx_install_btn" href="<?php echo esc_url(add_query_arg(array('action' => 'ultp_install'), admin_url())); ?>"><span class="dashicons dashicons-image-rotate"></span><?php echo esc_html__('Install PostX', 'fastx'); ?></a>
                                        <?php 
                                    } else {
                                        if( ( file_exists(WP_PLUGIN_DIR.'/ultimate-post/ultimate-post.php') && ! is_plugin_active('ultimate-post/ultimate-post.php') ) ) {
                                            ?>
                                                <a id="fsx_activate_btn" href="<?php echo esc_url(add_query_arg(array('action' => 'ultp_activate'), admin_url())); ?>"><span class="dashicons dashicons-image-rotate"></span><?php echo esc_html__('Activate PostX', 'fastx'); ?></a>
                                            <?php 
                                        } else {
                                            ?> 
                                                <a class="fsx_starter_btn" href="<?php echo esc_url(admin_url('admin.php?page=ultp-settings#startersites')) ?>"><?php echo esc_html__( 'Import Starter Packs', 'fastx' ) ?></a>
                                            <?php 
                                        }
                                    }
                                ?>
                            </div>
                            <div class="fsx_wc_demos">
                                <a href=<?php echo esc_url("https://www.wpxpo.com/postx/starter-sites/?utm_source=Fastx-Getting-Starterd&utm_medium=Explore-Templates&utm_campaign=FastX"); ?> target="_blank"><?php echo esc_html__( 'Explore Templates', 'fastx' ) ?><span class="dashicons dashicons-arrow-right-alt"></span></a>
                            </div>
                        </div>
                    </div>
                    <div class="fsx_theme_welcome_right">
                        <img class="thumbnail" src=" <?php echo FASTX_DIR.'/assets/img/fastx_banner.jpg'; ?>" alt="PostX Intro Video">
                    </div>
                </div>

                <!-- feature section  -->
                <div class="fsx_features_wrapper">
                    <div class="fsx_feature_left">
                        <div class="fsx_feature_custimizer">
                            <div class="fsx_custimizer_label"><?php echo esc_html__( 'FSE Support (Edit Templates & Template Parts)', 'fastx' ) ?></div>
                            <div class="fsx_custimizer_items">
                                <div class="fsx_custimizer_item">
                                    <div class="fsx_custimizer_title"><?php echo esc_html__( 'Front Page', 'fastx' ) ?></div>
                                    <a class="fsx_custimizer_btn" href="<?php echo esc_url(admin_url('site-editor.php'));?>?postType=wp_template&postId=fastx%2F%2Fhome&canvas=edit"><?php echo esc_html__( 'Customize', 'fastx' ) ?><span class="dashicons dashicons-arrow-right-alt"></span></a>
                                </div>
                                <div class="fsx_custimizer_item">
                                    <div class="fsx_custimizer_title"><?php echo esc_html__( 'Blog Page', 'fastx' ) ?></div>
                                    <a class="fsx_custimizer_btn" href="<?php echo esc_url(admin_url('site-editor.php'));?>?postType=wp_template&postId=fastx%2F%2Fhome&canvas=edit"><?php echo esc_html__( 'Customize', 'fastx' ) ?><span class="dashicons dashicons-arrow-right-alt"></span></a>
                                </div>
                                <div class="fsx_custimizer_item">
                                    <div class="fsx_custimizer_title"><?php echo esc_html__( 'Archive Pages', 'fastx' ) ?></div>
                                    <a class="fsx_custimizer_btn" href="<?php echo esc_url(admin_url('site-editor.php'));?>?postType=wp_template&postId=fastx%2F%2Farchive&canvas=edit"><?php echo esc_html__( 'Customize', 'fastx' ) ?><span class="dashicons dashicons-arrow-right-alt"></span></a>
                                </div>
                                <div class="fsx_custimizer_item">
                                    <div class="fsx_custimizer_title"><?php echo esc_html__( 'Single Posts', 'fastx' ) ?></div>
                                    <a class="fsx_custimizer_btn" href="<?php echo esc_url(admin_url('site-editor.php'));?>?postType=wp_template&postId=fastx%2F%2Fsingle&canvas=edit"><?php echo esc_html__( 'Customize', 'fastx' ) ?><span class="dashicons dashicons-arrow-right-alt"></span></a>
                                </div>
                                <div class="fsx_custimizer_item">
                                    <div class="fsx_custimizer_title"><?php echo esc_html__( 'Header Part', 'fastx' ) ?></div>
                                    <a class="fsx_custimizer_btn" href="<?php echo esc_url(admin_url('site-editor.php'));?>?postType=wp_template_part&postId=fastx%2F%2Fheader&canvas=edit"><?php echo esc_html__( 'Customize', 'fastx' ) ?><span class="dashicons dashicons-arrow-right-alt"></span></a>
                                </div>
                                <div class="fsx_custimizer_item">
                                    <div class="fsx_custimizer_title"><?php echo esc_html__( 'Footer Part', 'fastx' ) ?></div>
                                    <a class="fsx_custimizer_btn" href="<?php echo esc_url(admin_url('site-editor.php'));?>?postType=wp_template_part&postId=fastx%2F%2Ffooter&canvas=edit"><?php echo esc_html__( 'Customize', 'fastx' ) ?><span class="dashicons dashicons-arrow-right-alt"></span></a>
                                </div>
                            </div>
                        </div>
                        <div class="fsx_feature_benefits">
                            <div class="fsx_benefits_label"><?php echo esc_html__( 'More Reasons to Love FastX!', 'fastx' ) ?></div>
                            <div class="fsx_benefits">
                                <div class="fsx_theme_descp"><?php echo esc_html__( 'The FastX FSE theme is finely crafted for creating fast-loading websites without depending on any third-party builders. It is suitable for website owners, freelancers, agencies, and marketers.', 'fastx' ) ?></div>
                                <div class="fsx_benefits_label"><?php echo esc_html__( 'Key Features', 'fastx' ) ?></div>
                                <div class="fsx_benefits_items">
                                    <div>
                                        <div class="fsx_benefits_item"><span class="dashicons dashicons-yes"></span><?php echo esc_html__( 'Full Site Editing Support', 'fastx' ) ?></div>
                                        <div class="fsx_benefits_item"><span class="dashicons dashicons-yes"></span><?php echo esc_html__( 'Block-Based Desing', 'fastx' ) ?></div>
                                        <div class="fsx_benefits_item"><span class="dashicons dashicons-yes"></span><?php echo esc_html__( 'Customization Freedom', 'fastx' ) ?></div>
                                    </div>
                                    <div>
                                        <div class="fsx_benefits_item"><span class="dashicons dashicons-yes"></span><?php echo esc_html__( 'Simple Coding Structures', 'fastx' ) ?></div>
                                        <div class="fsx_benefits_item"><span class="dashicons dashicons-yes"></span><?php echo esc_html__( 'Personalized Templates', 'fastx' ) ?></div>
                                        <div class="fsx_benefits_item"><span class="dashicons dashicons-yes"></span><?php echo esc_html__( 'Global Styles', 'fastx' ) ?></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- right  -->
                    <div class="fsx_feature_right">
                        <div class="fsx_feature_right_item">
                            <div class="fsx_title"><?php echo esc_html__( 'Join FastX Community', 'fastx' ) ?></div>
                            <div class="fsx_feature_descp"><?php echo esc_html__( 'Join our community to stay up to date, share your feedback, and get fast support.', 'fastx' ) ?></div>
                            <a class="fsx_feature_btn" href=<?php echo esc_url("https://www.facebook.com/groups/gutenbergpostx"); ?> target="_blank"><?php echo esc_html__( 'Join Now', 'fastx' ) ?></a>
                        </div>
                        <div class="fsx_feature_right_item">
                            <div class="fsx_title"><?php echo esc_html__( 'Show Your Love', 'fastx' ) ?></div>
                            <div class="fsx_feature_descp"><?php echo esc_html__( 'Enjoying FastX? Support our endless work by giving a 5 start review.', 'fastx' ) ?></div>
                            <a class="fsx_feature_btn" href=<?php echo esc_url("https://wordpress.org/support/theme/fastx/reviews/"); ?> target="_blank"><?php echo esc_html__( 'Rate it Now', 'fastx' ) ?></a>
                        </div>
                    </div>
                </div>

            </div>
        </div>
        <?php
    }

    /**
     * Activate callback of PostX plugin
     * @since v1.0.0
	*/
    public function ultp_activate_callback( $func ) {
        if( file_exists(WP_PLUGIN_DIR.'/ultimate-post/ultimate-post.php') && ! is_plugin_active('ultimate-post/ultimate-post.php') ) {
            activate_plugin('ultimate-post/ultimate-post.php');
            if( $func != 'func' ) {
                return wp_send_json_success ([
                    'redirect' => admin_url('admin.php?page=ultp-settings#startersites')
                ]);
            }
        }
	}

    /**
     * Activate callback of PostX plugin
     * @since v1.0.0
	*/
    public function ultp_install_callback() {
        include ABSPATH . 'wp-admin/includes/plugin-install.php';
        include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

        if ( ! class_exists( 'Plugin_Upgrader' ) ) {
            include ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
        }
        if ( ! class_exists( 'Plugin_Installer_Skin' ) ) {
            include ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php';
        }

        $plugin = 'ultimate-post';
        $api = plugins_api(
            'plugin_information',
            array(
                'slug'   => $plugin,
                'fields' => array(
                    'sections' => false,
                ),
            )
        );

        if ( is_wp_error( $api ) ) {
            return $api->get_error_message();
        }

        $skin     = new \WP_Ajax_Upgrader_Skin();
        $upgrader = new \Plugin_Upgrader( $skin );
        $result   = $upgrader->install( $api->download_link );
        
        $redirect = admin_url('themes.php?page=fastx-settings');
        if ( $result && ! is_wp_error( $result ) ) {
            $this->ultp_activate_callback('func');
            $redirect = admin_url('admin.php?page=ultp-settings#startersites');
        }
        return wp_send_json_success ([
            'redirect' => $redirect,
        ]);
	}

    /**
	 * Core block css
     * @since v1.1.0
	 * @return styles
	*/
    public function add_block_styles( ) {
        register_block_style(
            'core/archives',
            array(
                'name'      => '-fsx-archives',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/categories',
            array(
                'name'      => '-fsx-categories',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/search',
            array(
                'name'      => '-fsx-search',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/tag-cloud',
            array(
                'name'      => '-fsx-tag-cloud',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/post-author',
            array(
                'name'      => '-fsx-post-author',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/avatar',
            array(
                'name'      => '-fsx-avatar',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/post-terms',
            array(
                'name'      => '-fsx-post-terms-bg',
                'label'     => __( 'Background', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/post-terms',
            array(
                'name'      => '-fsx-post-terms-border',
                'label'     => __( 'Bordered', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/query',
            array(
                'name'      => '-fsx-query',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/post-comments-form',
            array(
                'name'      => '-fsx-comments-form',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/comments-pagination',
            array(
                'name'      => '-fsx-comments-pagination',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
        register_block_style(
            'core/query-pagination',
            array(
                'name'      => '-fsx-query-pagination',
                'label'     => __( 'FastX design', 'fastx' ),
                'inline_style' => ''
            )
        );
    }
}