<?php
namespace WpBookingEvent\Wordpress;

use Exception;
use WpBookingEvent\Factory;
use WpBookingEvent\Html\WpBookingEventHtmlFrontend;
use WpBookingEvent\Log\Log;
use WpBookingEvent\Mvc\Controller\Controller;
use WpBookingEvent\Mvc\Model\Model;
use WpBookingEvent\Mvc\View\View;
use WpBookingEvent\Utilities\Utility;
use WpBookingEvent\Language\Text;
/**
 * Class Wordpress
 * @package WpBookingEvent\Wordpress
 */
class WordpressSite extends Wordpress
{

    /**
     * @var
     */
    private static $instance;
    public static $key_woo_booking = "woobooking";

    /**
     * @var string
     */
    public static $namespace = "wpbookingevent_api/1.0";
    /**
     * @var int
     */
    private $clientId=0;
    private $init_GutembergBlock=0;
    public function init(){

        if(!$this->is_admin()) {
            if(session_id() == '')
                session_start();
            add_action( 'wp_enqueue_scripts', array($this,'wp_enqueue_scripts') );
            $input=Factory::getInput();
            $data=$input->getData();
            if(isset($data['_locale'])){
                return false;
            }
            $task=$input->getString('task',"");
            if ($task) {
                list($controller, $task) = explode(".", $task);
                $class_name ="WpBookingEventFrontend\app\\controllers\\". ucfirst($controller) . "Controller";
                if (class_exists($class_name)) {
                    $class_controller = new $class_name();
                    if (method_exists($class_controller, $task)) {
                        //not return
                        call_user_func(array($class_controller, $task));

                    }
                }
            }

            add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
            WpBookingEventHtmlFrontend::load_jquery_library('less');
            WpBookingEventHtmlFrontend::load_jquery_library('main_app');
            WpBookingEventHtmlFrontend::load_jquery_library('lazy_load');
            WpBookingEventHtmlFrontend::load_jquery_library('bootstrap');

            $path = "wp-content/plugins/wpbookingevent/frontend/app/views.xml";
            add_shortcode('wpbookingevent', array($this, "front_end_render_view"));
            add_action('rest_api_init', array($this, 'wp_register_rest_route'));
            $list_view = $this->get_list_layout_block_frontend();

            foreach ($list_view as $key => $view) {
                $a_key = self::$key_wpbookingevent . "-block-" . $key;
                add_shortcode($a_key, array($this, 'woo_booking_render_block_by_tag_func'));
            }


            $gutembergBlock = GutembergBlock::getInstance();
            $gutembergBlock->init();
            $widgets = Widgets::getInstance();
            $widgets->init();
            add_action('wp_footer', array($this, 'wp_hook_add_script_footer'));
        }



    }
    public static function wp_enqueue_scripts(){
        $config=Factory::getConfig();
        $environment=$config->get('environment',"development");
        if($environment=="production") {
            wp_enqueue_style( 'wpbookingevent-main-frontend-style', WPBOOKINGEVENT_PLUGIN_DIR_URL . '/frontend/app/assets/less/style.css', array());
        }else{

        }


    }
    public function wp_register_rest_route(){
        register_rest_route(
            self::$namespace,
            "wpbookingevent_frontend/task",
            array(
                'methods' => 'POST',
                'callback' => array($this, 'ajax_action_task'),
            )
        );
        register_rest_route(
            self::$namespace,
            "wpbookingevent_frontend/task",
            array(
                'methods' => 'GET',
                'callback' => array($this, 'ajax_action_task'),
            )
        );
        register_rest_route(
            self::$namespace,
            "wpbookingevent_backend/task",
            array(
                'methods' => 'POST',
                'callback' => array($this, 'ajax_action_task'),
            )
        );
        register_rest_route(
            self::$namespace,
            "wpbookingevent_backend/task",
            array(
                'methods' => 'GET',
                'callback' => array($this, 'ajax_action_task'),
            )
        );
    }

    public  function load_plugin_textdomain(){
        load_plugin_textdomain( 'wpbookingevent', false, WPBOOKINGEVENT_PATH_ROOT . '/languages/' );
    }

    function woo_booking_render_block_by_tag_func($atts, $content, $a_view)
    {


        $input = Factory::getInput();
        if (is_array($atts) && $id = reset($atts)) {

            list($package, $block, $block_name) = explode("-", $a_view);

            wp_kses(Controller::view_block_module($id, $block_name),true);
        }
        return false;
    }
    public function getKeyWooBooking()
    {
        return self::$key_woo_booking;
    }

    public static function get_api_task()
    {
        return "wpbookingevent_frontend/task";

    }
    public function front_end_render_view($params,$option,$view){
        $input=Factory::getInput();
        WpBookingEventHtmlFrontend::load_jquery_library('bootstrap');
        $view=isset($params['view'])?$params['view']:"services-list";
        $a_view=$input->getString('view',"");
        if($a_view){
            $view=$a_view;
        }
        list($view, $layout) = explode("-", $view);

        echo (Controller::view("$view.$layout"));
        add_action('wp_footer', array($this, 'wp_hook_add_script_footer'));

    }
    public function wp_hook_add_script_footer(){
        global $wp;

        wp_enqueue_script("jquery");

        $doc = Factory::getDocument();

        $lessStyleSheets = $doc->getLessStyleSheets();
        $config=Factory::getConfig();

        $environment=$config->get('environment',"development");
        if($environment=="development") {
            foreach ($lessStyleSheets as $src) {
                ob_start();
                ?>
                <link rel="stylesheet/less" type="text/css"
                      href="<?php  Text::_echo(WPBOOKINGEVENT_PLUGIN_DIR_URL . "/" . $src) ?>"/>
                <?php
                echo (ob_get_clean());
            }
        }else{
            //$doc->addStyleSheet('frontend/app/assets/less/style.css');
        }
        $styleSheets = $doc->getStyleSheets();


        foreach ($styleSheets as $src) {
            $random = random_int(100000, 900000);
            if (strpos($src, 'http') !== false) {
                wp_enqueue_style('wpbookingevent-css-' . $random, $src);
            } else {
                wp_enqueue_style('wpbookingevent-css-' . $random, WPBOOKINGEVENT_PLUGIN_DIR_URL . '/' . $src);
            }
        }

        ob_start();
        ?>
        <script type="text/javascript">
            var wpbookingevent_root_url = "<?php  echo (WPBOOKINGEVENT_SITE_URL) ?>";
            var wpbookingevent_current_url = "<?php  echo (WPBOOKINGEVENT_SITE_URL) ?>";
            var wpbookingevent_root_url_plugin = "<?php echo (WPBOOKINGEVENT_PLUGIN_DIR_URL); ?>/";
            var wpbookingevent_api_task = "/wp-json/<?php echo (self::$namespace.'/'.$this->get_api_task()) ?>";
        </script>
        <?php
        $content=ob_get_clean();
        $content= Utility::remove_string_javascript($content);
        wp_enqueue_script('wpbookingevent-js-init', WPBOOKINGEVENT_PLUGIN_DIR_URL .'frontend/resources/init.js' );
        wp_add_inline_script('wpbookingevent-js-init', $content);

        $scripts = $doc->getScripts();
        foreach ($scripts as $src){
            try {
                $random = random_int(100000, 900000);
                if (strpos($src, 'http') !== false || strpos($src, 'https') !== false) {
                    wp_enqueue_script('wpbookingevent-js-out-site'.$random,$src,array('jquery') );
                } else {
                    $src=WPBOOKINGEVENT_PLUGIN_DIR_URL . $src;
                    wp_enqueue_script('wpbookingevent-js-in-site-'.$random, $src,array('jquery') );
                }
            } catch (Exception $e) {
            }

        }
        wp_enqueue_script('wpbookingevent-js-defined', WPBOOKINGEVENT_PLUGIN_DIR_URL .'frontend/resources/define.js' );
        $doc=Factory::getDocument();
        $script = $doc->getScript();
        foreach ($script as $content) {
            wp_add_inline_script('wpbookingevent-js-defined', $content);
        }
    }



}