<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace VanillaBeans\hangit;

if (!function_exists('\VanillaBeans\hangit\hangit_showroom')) {

    function hangit_showroom($attributes) {
            global $wp_query;
            global $post;
            \add_thickbox();

          

        $s='';
    $roomids = \VanillaBeans\vbean_setting('vbean_hangit_rooms','');
    $roomids = str_replace('undefined,', '', $roomids);
    $roomarray = explode(",", $roomids);
    $genericrooms = \maybe_unserialize( \VanillaBeans\vbean_setting('vbean_hangit_genericrooms',''));

//        if($room!=''&&$room!='undefined'){
//            $roomurl = wp_get_attachment_url($room).'';
//            $rooms = $rooms.'rooms[rooms.length]=['.$room.',"'.$roomurl.'"];';
//        }
//    }
    
        $linktext = \VanillaBeans\vbean_setting('vbean_hangit_linktext', __('Hang it!', 'hangit'));
        $instructions = \VanillaBeans\vbean_setting('vbean_hangit_instructions', __('Move the artwork anywhere in the room. Change background room by clicking on above images. Resize by grabbing the bottom right of the artwork.', 'hangit'));
        $instructions = nl2br($instructions);
        if ($attributes && $attributes["linktext"] != "") {
            $linktext = $attributes["linktext"];
        }       

        $hangingid = \get_post_thumbnail_id($wp_query->post->ID);
        $hanging = \wp_get_attachment_url($hangingid).'';
         ob_start();
        if($hanging==''){
           echo('');
        }
        else{
        $s= ob_get_contents();
        ob_end_clean();

            $firstroom = '';
            if ($roomarray||$genericrooms) {
                ob_start();
                ?>
                <div class="hangit-container">
                    <a href="#TB_inline?width=90%&height=90%&inlineId=showroombox" class="thickbox" id="hangerlink"><?php echo \esc_html($linktext); ?></a>	
                    <div id="showroombox" class="thickbox" style="display:none;">
                        <div class="hangit-instructions">
                            <?php echo \wp_kses_post($instructions); ?>
                        </div>
                        <div class="hangit-backgrounds-header">
                            <h3><?php echo \esc_html__("Backgrounds", "hangit"); ?></h3>
                            <div class="hangit-local-background-control">
                                <button id="hangit-choose-local" class="button"><?php echo \esc_html__("Choose Your Own Background", "hangit"); ?></button>
                                <input type='file' accept='image/*' id="hangit-local-background" style="display:none;" />
                            </div>
                        </div>
                        <div id="showroom-rooms">
                            <?php
                            if($roomarray){
                                foreach ($roomarray as $roomid) {
                                    if($roomid!=''&&$roomid!='undefined'){
                                        $roomurl = \wp_get_attachment_url($roomid);
                                        if($firstroom==''){
                                            $firstroom=$roomurl;
                                        }
                                        echo '<div class="showroom-roomchoice" data-room-url="'.\esc_url($roomurl).'">';
                                        echo '<img src="'.\esc_url($roomurl).'" alt="' . \esc_attr__('Room background option', 'hangit') . '" />';
                                        echo '</div>';
                                    }
                                }
                            }
                            if($genericrooms){
                                $gencount=0;
                                foreach ($genericrooms as $room) {
                                    $gencount=$gencount+1;
                                    if($firstroom==''){
                                       $firstroom = VBEANHANGIT_PLUGIN_URL.'assets/img/'.$room;
                                    }
                                    $theroom = VBEANHANGIT_PLUGIN_URL.'assets/img/'.$room;
                                    ?>
                                    <div class="showroom-roomchoice" id="genroomnumber<?php echo \esc_attr($gencount); ?>">
                                        <img src="<?php echo \esc_url($theroom); ?>" class="showroom-roomchoice-img" />
                                    </div>
                                    <?php
                                }
                                
                                if($firstroom==''){
                                    $firstroom = VBEANHANGIT_PLUGIN_URL.'assets/img/blank.png';
                                }
                            }
                            ?>
                        </div>
                        
                        <div id="showroom-canvas">
                            <div id="the-showroom">
                                <img src="<?php echo \esc_url($firstroom); ?>" class="showroom-background-img" style="width:100%;height:auto;" />
                                <div id="thehanging">
                                    <img src="<?php echo \esc_url($hanging); ?>" id="theframe" alt="<?php echo \esc_attr__('Your artwork', 'hangit'); ?>" />
                                </div>
                            </div>
                            <div class="hangit-loading" style="display: none;"><?php echo \esc_html__('Loading room...', 'hangit'); ?></div>
                        </div>

                        <div class="hangit-container">
                            <div class="hangit-controls">
                                <div class="hangit-controls-row">
                                    <div class="hangit-slider-container">
                                        <label for="hangit-border-width">
                                            <?php echo \esc_html__("Frame Width", "hangit"); ?>: <span id="hangit-border-width-value">1</span>
                                        </label>
                                        <input type="range" id="hangit-border-width" min="0" max="20" value="1" step="1">
                                    </div>
                                    
                                    <div class="hangit-color-control">
                                        <label for="hangit-border-color">
                                            <?php echo \esc_html__("Frame Color", "hangit"); ?>:
                                            <input type="color" id="hangit-border-color" value="#000000">
                                        </label>
                                    </div>
                                    
                                    <div class="hangit-print-button">
                                        <button id="hangit-print" class="button button-primary">
                                            <?php echo \esc_html__("Print", "hangit"); ?>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php
                $s = ob_get_contents();
                ob_end_clean();
            }
        }
        return $s;
    }

}



if (\in_array('woocommerce/woocommerce.php', \apply_filters('active_plugins', \get_option('active_plugins')))) {
    // Put your plugin code here
}

//add_action('woocommerce_show_product_images', '\VanillaBeans\hangit\hangit_showroom', 15);

/**
 * Register the [hangit] shortcode
 *
 * @param array $atts Shortcode attributes
 * @return string The HTML output for the hangit showroom
 */
function hangit_inline($atts) {
     return \VanillaBeans\hangit\hangit_showroom($atts);
}
\add_shortcode('hangit', '\VanillaBeans\hangit\hangit_inline');


if (!function_exists('load_hangit_showroomscripts')) {

    function load_hangit_showroomscripts() {
        global $wp_scripts;
        
        $min = VBEANHANGIT_DBUG?"": ".min";
        
        // Use plugin version for cache busting instead of time()
        \wp_enqueue_script(
                'hangit_dbug_vanillabean', 
                VBEANHANGIT_PLUGIN_URL . 'assets/clog'.$min.'.js', 
                array('jquery'), 
                VBEANHANGIT_PLUGIN_VERSION
        );
        \wp_enqueue_script(
                'hangit_showroom_vanillabean', 
                VBEANHANGIT_PLUGIN_URL . 'assets/vanillabean'.$min.'.js', 
                array('jquery'), 
                VBEANHANGIT_PLUGIN_VERSION
        );
        \wp_enqueue_script('jquery-ui-core', array('jquery'));
        \wp_enqueue_script('jquery-ui-draggable', array('jquery'));
        \wp_enqueue_script('jquery-ui-resizable', array('jquery'));
        \wp_enqueue_script('jquery-ui-droppable', array('jquery'));
        
        // Enqueue vanilla drag script
        \wp_enqueue_script(
                'hangit_vanilla_drag',
                VBEANHANGIT_PLUGIN_URL . 'assets/vanilla-drag.js',
                array('jquery'),
                VBEANHANGIT_PLUGIN_VERSION
        );
        
        // Enqueue print script
        \wp_enqueue_script(
                'hangit_print',
                VBEANHANGIT_PLUGIN_URL . 'assets/print.js',
                array('jquery'),
                VBEANHANGIT_PLUGIN_VERSION
        );

        \wp_enqueue_style('thickbox');
        \wp_enqueue_style("jquery-ui-css", VBEANHANGIT_PLUGIN_URL . 'assets/css/jquery-ui.min.css');
        \wp_enqueue_style("jquery-ui2-css", VBEANHANGIT_PLUGIN_URL . 'assets/css/jquery-ui.structure.min.css');
        \wp_enqueue_style("jquery-ui3-css", VBEANHANGIT_PLUGIN_URL . 'assets/css/jquery-ui.theme.min.css');
        \wp_enqueue_style(
                'hangit_showroom_styles', VBEANHANGIT_PLUGIN_URL . 'assets/showroom'.$min.'.css'
        );
        
        // Enqueue print styles
        \wp_enqueue_style(
                'hangit_print_styles', VBEANHANGIT_PLUGIN_URL . 'assets/print-styles.css',
                array(),
                VBEANHANGIT_PLUGIN_VERSION
        );
    }

}

\add_action('wp_enqueue_scripts', '\VanillaBeans\hangit\load_hangit_showroomscripts');
