<?php
namespace WOO_PRODUCT_TABLE\Admin;

use WOO_PRODUCT_TABLE\Core\Base;
use WOO_PRODUCT_TABLE\Admin\Handle\Notice_Framework;

class Page_Loader extends Base
{
    public $main_slug = 'edit.php?post_type=wpt_product_table';
    public $page_folder_dir;
    public $topbar_file;
    public $topbar_sub_title;

    public $is_pro; //I tried to load inside hook, then make it public
    protected $pro_version;
    protected $license;
    protected $direct;
    public $notice_framework;

    public function __construct()
    {

        $this->is_pro = defined( 'WPT_PRO_DEV_VERSION' );
        
        $this->notice_framework = new Notice_Framework();
        $this->notice_framework->plugins_recommendation();

        if($this->is_pro && class_exists( '\WOO_Product_Table' ) && ! defined('WPT_NEW_PREMIUM') ){
            $this->pro_version = WPT_PRO_DEV_VERSION;
            $this->handle_license_n_update();
            // $this->notice_framework->offer_in_premium();
        }else{
            // $this->notice_framework->offer_4_premium_in_free();
        }
        $this->page_folder_dir = $this->base_dir . 'admin/page/';
        $this->topbar_file = $this->page_folder_dir . 'topbar.php';
        $this->topbar_sub_title = __("Manage and Settings", "woo-product-table");
    }

    public function run()
    {
        
        //has come from admin/menu_plugin_settings_link.php file
        add_action( 'admin_menu', [$this, 'admin_menu'], 2 );
        add_filter('admin_body_class', [$this, 'body_class']);
        add_action( 'admin_enqueue_scripts', [$this, 'admin_enqueue_scripts'] );

        if ( class_exists( '\WOO_PRODUCT_TABLE\Admin\Appseros\Src\Client' ) ) {
            $client = new \WOO_PRODUCT_TABLE\Admin\Appseros\Src\Client( 'e8d12f8a-54f6-488e-9437-480fe6b3bc27', 'Woo Product Table', WPT_FILE );
            $client->insights()->init();
        }

    }

    public function configure_page_render()
    {
        include $this->topbar_file;
        if( ! $this->is_pro ){
            include $this->page_folder_dir . 'premium-link-header.php';
        }
        include $this->page_folder_dir . 'configure.php';
    }
    public function admin_menu()
    {
        $proString = $this->is_pro ? esc_html__( ' Pro', 'woo-product-table' ) : '';
        add_submenu_page( $this->main_slug, esc_html__( 'Table Settings ', 'woo-product-table' ) . $proString,  esc_html__( 'Table Settings', 'woo-product-table' ), WPT_CAPABILITY, 'woo-product-table-config', [$this, 'configure_page_render'] );
        

        if (class_exists('\PSSG_Sync_Sheet\App\Handle\Quick_Table')) {
            add_submenu_page( $this->main_slug, esc_html__( 'Product Bulk Edit', 'woo-product-table' ) . $proString,  __( 'Bulk Edit', 'woo-product-table' ), WPT_CAPABILITY, 'wpt-product-quick-edit', [$this, 'product_quick_edit'] );
        }
        
        
        add_submenu_page( $this->main_slug, esc_html__( 'Browse Plugins', 'woo-product-table' ),  esc_html__( 'Browse Plugins', 'woo-product-table' ), WPT_CAPABILITY, 'wpt-browse-plugins', [$this, 'browse_plugins_html'] );
        if( ! $this->is_pro ){
            //Get Premium link
            add_submenu_page( $this->main_slug, esc_html__( 'Get Premium', 'woo-product-table' ),  esc_html__( 'Get Premium', 'woo-product-table' ), 'read', 'https://wooproducttable.com/pricing/' );
            // add_submenu_page( $this->main_slug, esc_html__( 'Addons', 'woo-product-table' ),  esc_html__( 'Addons', 'woo-product-table' ), WPT_CAPABILITY, 'woo-product-table-addons-list', [$this, 'addons_list_html'] );
            // add_submenu_page( $this->main_slug, esc_html__( 'Tutorials', 'woo-product-table' ),  esc_html__( 'Tutorials', 'woo-product-table' ), WPT_CAPABILITY, 'woo-product-table-tutorial-page', [$this, 'html_tutorial_page'] );
        }
    }

    public function body_class( $classes )
    {
        global $current_screen;

        
        $s_id = isset( $current_screen->id ) ? $current_screen->id : '';
        
        if( $s_id == 'edit-wpt_product_table' || $s_id == 'wpt_product_table' ){
            // $classes .= ' wp-default-content-wrapper ';
        }else if( strpos( $s_id, $this->plugin_prefix ) !== false ){
            $classes .= ' wpt-zero-body ';
        }
        
        
        return $classes;

    }
    public function admin_enqueue_scripts()
    {
        global $current_screen;

        $s_id = isset( $current_screen->id ) ? $current_screen->id : '';
        if( strpos( $s_id, $this->plugin_prefix ) === false ) return;

        add_filter('admin_footer_text',[$this, 'admin_footer_text']);


        $this->live_chat_script();

        wp_register_style( $this->plugin_prefix . '-new-admin', $this->base_url . 'assets/css/new-admin.css', false, $this->dev_version );
        wp_enqueue_style( $this->plugin_prefix . '-new-admin' );
    }


    /**
     * Undocumented function
     *
     * @return void
     */
    protected function live_chat_script()
    {
        /**
         * how to disable live chat
         * add_filter('wpt_live_chat_bool','__return_false');
         */
        $live_chat_bool = apply_filters( 'wpt_live_chat_bool', true );
        if( ! $live_chat_bool ) return;
        ?>
        <!--Start of Tawk.to Script-->
        <script type="text/javascript">
        var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
        (function(){
        var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
        s1.async=true;
        s1.src='https://embed.tawk.to/628f5d4f7b967b1179915ad7/1g4009033';
        s1.charset='UTF-8';
        s1.setAttribute('crossorigin','*');
        s0.parentNode.insertBefore(s1,s0);
        })();
        </script>
        <!--End of Tawk.to Script-->
        <?php
    }
    public function admin_footer_text($text)
    {
        $rev_link = 'https://wordpress.org/support/plugin/woo-product-table/reviews/#new-post';
        
        $text = sprintf(
            /* translators: 1: Plugin review submit link 2: star rating */
			__( 'Thank you for using Woo Product Table. <a href="%1$s" target="_blank">%2$sPlease review us</a>.', 'woo-product-table' ),
			$rev_link,
            '<i class="wpt-star-filled"></i><i class="wpt-star-filled"></i><i class="wpt-star-filled"></i><i class="wpt-star-filled"></i><i class="wpt-star-filled"></i>'
		);
        return '<span id="footer-thankyou" class="wpt-footer-thankyou">' . wp_kses_post( $text ) . '</span>';
    }
    public function browse_plugins_html()
    {
        //In future, I will make it like min max plugin - which I already did
        // add_filter( 'plugins_api_result', [$this, 'plugins_api_result'], 1, 3 );
        $this->topbar_sub_title = __( 'Browse our Plugins','woo-product-table' );
        include $this->topbar_file;
        if( ! $this->is_pro ){
            include $this->page_folder_dir . 'premium-link-header.php';
        }
        include $this->page_folder_dir . 'browse-plugins.php';
    }

    //Browse plugin only user codersaiful
    public function plugins_api_result( $res, $action, $args ) {
        if ( 'plugin_information' !== $action ) {
            return $res;
        }

        // Check for our plugin slug
        if ( 'woo-product-table' === $args->slug ) {
            // Modify the plugin information as needed
            $res->download_link = 'https://downloads.wordpress.org/plugin/woo-product-table.zip';
        }
        return $res;
    }

    public function addons_list_html()
    {
        $this->topbar_sub_title = __( 'Addons','woo-product-table' );
        include $this->topbar_file;
        if( ! $this->is_pro ){
            include $this->page_folder_dir . 'premium-link-header.php';
        }
        include $this->page_folder_dir . 'addons-list.php';
    }
    public function html_tutorial_page()
    {
        //In future, I will make it like min max plugin - which I already did
        // add_filter( 'plugins_api_result', [$this, 'plugins_api_result'], 1, 3 );
        $this->topbar_sub_title = __( 'Tutorial','woo-product-table' );
        include $this->topbar_file;
        if( ! $this->is_pro ){
            include $this->page_folder_dir . 'premium-link-header.php';
        }
        include $this->page_folder_dir . 'tutorials.php';
    }

    public function product_quick_edit()
    {
        $this->topbar_sub_title = __( 'Product Bulk Edit','woo-product-table' );
        include $this->topbar_file;
        include $this->page_folder_dir . 'product-bulk-edit.php';
        
    }

    /**
     * If will work, when only found pro version
     * 
     * @since 3.4.3.0
     * @author Saiful Islam <codersaiful@gmail.com>
     *
     * @return void
     */
    public function handle_license_n_update()
    {
        $this->license_key = get_option( 'wpt_pro_license_key' );
        if(empty($this->license_key)) return;
        $this->license_data_key = 'wpt_license_data';
        $this->license_status_key = 'wpt_pro_license_status';
        $this->license_status = get_option( $this->license_status_key );
        $this->license_data = get_option($this->license_data_key);

        
        add_action( 'admin_notices', [$this, 'license_activation_message'] );

        /**
         * Actually if not found lisen data, we will return null here
         * 
         * @since 3.4.3.0
         * @author Saiful Islam <codersaiful@gmail.com>
         */
        if( empty( $this->license_status ) || empty( $this->license_data ) ) return;
        
        $expires = isset($this->license_data->expires) ? $this->license_data->expires : '';
        $this->item_id = isset($this->license_data->item_id) ? $this->license_data->item_id : '';
        if('lifetime' == $expires) return;
        $exp_timestamp = strtotime($expires);
        /**
         * keno ami ei timestamp niyechi.
         * asole expire a zodi faka ase, tahole ta 1 jan, 1970 as strtotime er output.
         * 
         * ar jehetu amora 2010 er por kaj suru korechi. tai sei expire date ba ager date asar kOnO karonoi nai.
         * tai zodi 2012 er kom timestamp ase amora return null kore debo.
         * za already diyechi: if( $exp_timestamp < $year2010_timestamp ) return; by this line. niche follow korun.
         */
        $year2010_timestamp = strtotime('2023-09-08 23:59:59');
        if( $exp_timestamp < $year2010_timestamp ) return;

        //ekhon amora bortoman date er sathe tulona korbo
        if($exp_timestamp < time()){

            $this->exp_timestamp = $exp_timestamp;

            if($this->license_status == 'valid'){
                $this->invalid_status = 'invalid';
                $this->license_data->license = $this->invalid_status;
                update_option( $this->license_status_key, $this->invalid_status );
                update_option( $this->license_data_key, $this->license_data );

                
            }
            add_action( 'admin_notices', [$this, 'renew_license_notice'] );
        }
        
    }

    public function license_activation_message()
    {

        global $current_screen;
        if(strpos($current_screen->id, 'woo-product-table-license') !== false) return;

        if(empty($this->item_id)) return;
        $wpt_logo = WPT_ASSETS_URL . 'images/logo.png';

        if( empty( $this->license_status ) || $this->license_status === 'invalid' || $this->license_status === 'site_inactive' || $this->license_status === 'inactive' ){
            $wpt_logo = WPT_ASSETS_URL . 'images/logo.png';
            
            $link_label = __( 'Activate License', 'woo-product-table' );
            $link = admin_url('edit.php?post_type=wpt_product_table&page=woo-product-table-license');
            $message = esc_html__( ' Please Activate License to get Pro Feature', 'woo-product-table' );
            ob_start();
            ?>
            <div class="error wpt-renew-license-notice">
                <div class="wpt-license-notice-inside">
                <img src="<?php echo esc_url( $wpt_logo ); ?>" class="wpt-license-brand-logo">
                    Your License of <strong>Woo Product Table pro</strong> is <span style="color: #d00;font-weight:bold;">Invalid/Inactive</span>
                    %1$s <a href="%2$s">%3$s</a>
                </div>
            </div>
            <?php
            $full_message = ob_get_clean();
            printf( wp_kses_post( $full_message ), esc_html( $message ), esc_url( $link ), esc_html( $link_label ) );        
        }
    }
    public function renew_license_notice()
    {

        if(empty($this->item_id)) return;
        $wpt_logo = WPT_ASSETS_URL . 'images/logo.png';
        $expired_date = gmdate( 'd M, Y', $this->exp_timestamp );
        $link_label = __( 'Renew License', 'woo-product-table' );
        $link = "https://codeastrology.com/checkout/?edd_license_key={$this->license_key}&download_id={$this->item_id}";
		$message = esc_html__( ' Renew it to enable pro features.', 'woo-product-table' );
        ob_start();
        ?>
        <div class="error wpt-renew-license-notice">
            <div class="wpt-license-notice-inside">
            <img src="<?php echo esc_url( $wpt_logo ); ?>" class="wpt-license-brand-logo">
                Your License of <strong>Woo Product Table pro</strong> has been expired at <span style="color: #d00;font-weight:bold;"><?php echo esc_html( $expired_date ); ?></span>
                %1$s <a href="%2$s" target="_blank">%3$s</a>
            </div>
        </div>
        <?php
        $full_message = ob_get_clean();
        printf( wp_kses_post( $full_message ), esc_html( $message ), esc_url( $link ), esc_html( $link_label ) );    
    }

    
}