<?php
/**
 * Class Sparta_Template_Helper.
 *
 * @since 0.9.0
 *
 */

if (!class_exists('Sparta_Template_Helper')) {
    class Sparta_Template_Helper
    {
        /**
         * Variable to hold the initialization state.
         *
         * @var  boolean
         */
        protected static $initialized = false;

        /**
         * Initialize functions.
         *
         * @return  void
         */
        public static function initialize()
        {
            // Do nothing if start functions already initialized.
            if (self::$initialized) {
                return;
            }

            remove_action( 'mphb_render_single_room_type_metas', array( '\MPHB\Views\SingleRoomTypeView','renderReservationForm' ), 50 );
            add_action('sparta_single_room_type_content_right',array( '\MPHB\Views\SingleRoomTypeView', 'renderReservationForm' ));
            
            remove_action( 'mphb_render_single_room_type_metas', array( '\MPHB\Views\SingleRoomTypeView', 'renderDefaultOrForDatesPrice' ), 30 );
            add_action( 'sparta_single_room_type_price', array( '\MPHB\Views\SingleRoomTypeView', 'renderDefaultOrForDatesPrice' ) );

            // State that initialization completed.
            self::$initialized = true;
        }
      

        /**
         * Get template path.
         *
         * @since 1.3.0
         */
        private static function get_template_path()
        {
            return apply_filters('sparta_core_template_path', Sparta_Core::get_core_templates_path());
        }

        /**
         * Get template part.
         *
         * @since 1.3.0
         *
         * @param $path
         * @param $args
         * @param $render
         *
         * @return bool|string
         */
        public static function template($path, $args = array(), $render = false)
        {
            $template = locate_template(self::get_template_path() . $path);

            if (!$template) :
                $template = Sparta_Core::get_core_templates_path() . $path;
            endif;

            if ($render) :
                return self::render_template($template, $args);
            endif;

            return self::get_template($template, $args);
        }

        /**
         * Render template.
         *
         * @since 0.9.0
         *
         * @param $file
         * @param $args
         *
         * @return bool
         */
        private static function render_template($file, $args = array())
        {
            if (!is_file($file) || !is_readable($file)) :
                return false;
            endif;

            include $file;

            return true;
        }

        /**
         * Get template.
         *
         * @since 0.9.0
         *
         * @param $file
         * @param $args
         *
         * @return bool|string
         */
        private static function get_template($file, $args = array())
        {
            if (!is_file($file) || !is_readable($file)) :
                return false;
            endif;

            ob_start();
            include $file;

            return ob_get_clean();
        }
    }
}