<?php
/**
 * 2007-2015 Apollotheme
 *
 * NOTICE OF LICENSE
 *
 * ApPageBuilder is module help you can build content for your shop
 *
 * DISCLAIMER
 *
 *  @Module Name: AP Page Builder
 *  @author    Apollotheme <apollotheme@gmail.com>
 *  @copyright 2007-2015 Apollotheme
 *  @license   http://apollotheme.com - prestashop template provider
 */

if (!defined('_PS_VERSION_')) {
    # module validation
    exit;
}

class ApImage360 extends ApShortCodeBase
{
    public $name = 'ApImage360';
    public $for_module = 'manage';

    public function getInfo()
    {
        return array('label' => $this->l('Image 360'), 'position' => 20, 'desc' => $this->l('You can put image here'),
            'icon_class' => 'icon-html5', 'tag' => 'content structure');
    }

    public function getConfigList()
    {
        $href = Context::getContext()->link->getAdminLink('AdminApPageBuilderImages').'&imgDir=images&is_ajax=true&widget=ApImage360';
        $inputs = array(
            array(
                'type' => 'text',
                'name' => 'title',
                'label' => $this->l('Title'),
                'lang' => 'true',
                'default' => '',
            ),
//            array(
//                'type' => 'switch',
//                'label' => $this->l('Magnifier enable'),
//                'name' => 'magnify',
//                'is_bool' => true,
//                'values' => ApPageSetting::returnYesNo(),
//                'default' => 1,
//            ),
//            array(
//                'type' => 'text',
//                'name' => 'magnifier_width',
//                'label' => $this->l("Magnifier's width"),
//                'default' => '80%',
//            ),
//            array(
//                'type' => 'select',
//                'label' => $this->l("Magnifier's shape"),
//                'name' => 'magnifier_shape',
//                'options' => array(
//                    'query' => array(
//                        array('id' => 'inner', 'name' => $this->l('Inner')),
//                        array('id' => 'circle', 'name' => $this->l('Circle')),
//                        array('id' => 'square', 'name' => $this->l('Square')),
//                    ),
//                    'id' => 'id',
//                    'name' => 'name'
//                ),
//            ),
//            array(
//                'type' => 'switch',
//                'label' => $this->l('Fullscreen enable'),
//                'name' => 'fullscreen',
//                'is_bool' => true,
//                'values' => ApPageSetting::returnYesNo(),
//                'default' => 1,
//            ),
            array(
                'type' => 'select',
                'label' => $this->l("Spin"),
                'name' => 'spin',
                'options' => array(
                    'query' => array(
                        array('id' => 'drag', 'name' => $this->l('Drag')),
                        array('id' => 'hover', 'name' => $this->l('Hover')),
                    ),
                    'id' => 'id',
                    'name' => 'name'
                ),
                'desc' => $this->l('Method for spinning the image'),
            ),
            array(
                'type' => 'select',
                'label' => $this->l("Direction of auto-spin"),
                'name' => 'autospin_direction',
                'options' => array(
                    'query' => array(
                        array('id' => 'clockwise', 'name' => $this->l('Clockwise')),
                        array('id' => 'anticlockwise', 'name' => $this->l('Anticlockwise')),
                        array('id' => 'alternate-clockwise', 'name' => $this->l('Alternate Clockwise')),
                        array('id' => 'alternate-anticlockwise', 'name' => $this->l('Alternate Anticlockwise')),
                    ),
                    'id' => 'id',
                    'name' => 'name'
                ),
                'desc' => $this->l('Method for spinning the image'),
            ),
            array(
                'type' => 'text',
                'name' => 'speed',
                'label' => $this->l('speed'),
//                'lang' => 'true',
                'default' => '50',
                'desc' => $this->l('Speed of spin (1 - 100)'),
            ),
            array(
                'type' => 'text',
                'name' => 'mousewheel_step',
                'label' => $this->l('Mousewheel Step'),
//                'lang' => 'true',
                'default' => '1',
                'desc' => $this->l('Number of frames to spin on mousewheel'),
            ),
            array(
                'type' => 'text',
                'name' => 'autospin_speed',
                'label' => $this->l('Speed of auto-spin'),
//                'lang' => 'true',
                'default' => '3600',
                'desc' => $this->l('Choose speed of auto-spin'),
            ),
            array(
                'type' => 'switch',
                'label' => $this->l('Smoothing'),
                'name' => 'smoothing',
                'is_bool' => true,
                'values' => ApPageSetting::returnYesNo(),
                'default' => 1,
                'desc' => $this->l('Smoothly stop the image spinning'),
            ),
            array(
                'type' => 'select',
                'label' => $this->l("Duration of automatic spin"),
                'name' => 'autospin',
                'options' => array(
                    'query' => array(
                        array('id' => 'once', 'name' => $this->l('Once')),
                        array('id' => 'twice', 'name' => $this->l('Twice')),
                        array('id' => 'infinite', 'name' => $this->l('Infinite')),
                        array('id' => 'off', 'name' => $this->l('Off')),
                    ),
                    'id' => 'id',
                    'name' => 'name'
                ),
//                'desc' => $this->l('Method for spinning the image'),
            ),
            array(
                'type' => 'select',
                'label' => $this->l("Autospin starts on"),
                'name' => 'autospin_start',
                'options' => array(
                    'query' => array(
                        array('id' => 'load', 'name' => $this->l('load')),
                        array('id' => 'hover', 'name' => $this->l('hover')),
                        array('id' => 'click', 'name' => $this->l('click')),
                        array('id' => 'load,hover', 'name' => $this->l('load,hover')),
                        array('id' => 'load,click', 'name' => $this->l('load,click')),
                    ),
                    'id' => 'id',
                    'name' => 'name'
                ),
                'desc' => $this->l('Start automatic spin on page load, click or hover'),
            ),
            array(
                'type' => 'select',
                'label' => $this->l("Autospin stops on"),
                'name' => 'autospin_stop',
                'options' => array(
                    'query' => array(
                        array('id' => 'hover', 'name' => $this->l('hover')),
                        array('id' => 'click', 'name' => $this->l('click')),
                        array('id' => 'never', 'name' => $this->l('never')),
                    ),
                    'id' => 'id',
                    'name' => 'name'
                ),
                'desc' => $this->l('Start automatic spin on page load, click or hover'),
            ),
            array(
                'type' => 'select',
                'label' => $this->l("Initialization"),
                'name' => 'initialize_on',
                'options' => array(
                    'query' => array(
                        array('id' => 'hover', 'name' => $this->l('hover')),
                        array('id' => 'click', 'name' => $this->l('click')),
                        array('id' => 'load', 'name' => $this->l('load')),
                    ),
                    'id' => 'id',
                    'name' => 'name'
                ),
                'default' => 'load',
                'desc' => $this->l('Start automatic spin on page load, click or hover'),
            ),
            array(
                'type' => 'text',
                'name' => 'start_column',
                'label' => $this->l('Start Column'),
                'default' => '1',
                'desc' => $this->l('Column from which to start spin. auto means to start from the middle'),
            ),
            array(
                'type' => 'text',
                'name' => 'start_row',
                'label' => $this->l('Start Row'),
                'default' => 'auto',
                'desc' => $this->l('Row from which to start spin. auto means to start from the middle'),
            ),
            array(
                'type' => 'switch',
                'label' => $this->l('Loop Column'),
                'name' => 'loop_column',
                'is_bool' => true,
                'values' => ApPageSetting::returnYesNo(),
                'default' => 1,
                'desc' => $this->l('Continue spin after the last image on X-axis'),
            ),
            array(
                'type' => 'switch',
                'label' => $this->l('Loop Row'),
                'name' => 'loop_row',
                'is_bool' => true,
                'values' => ApPageSetting::returnYesNo(),
                'default' => 1,
                'desc' => $this->l('Continue spin after the last image on Y-axis'),
            ),
            array(
                'type' => 'switch',
                'label' => $this->l('Reverse rotation on X-axis'),
                'name' => 'reverse_column',
                'is_bool' => true,
                'values' => ApPageSetting::returnYesNo(),
                'default' => 1,
//                'desc' => $this->l('Continue spin after the last image on Y-axis'),
            ),
            array(
                'type' => 'switch',
                'label' => $this->l('Reverse rotation on Y-axis'),
                'name' => 'reverse_row',
                'is_bool' => true,
                'values' => ApPageSetting::returnYesNo(),
                'default' => 1,
//                'desc' => $this->l('Continue spin after the last image on Y-axis'),
            ),
            array(
                'type' => 'text',
                'name' => 'column_increment',
                'label' => $this->l('Column increment'),
                'default' => '1',
                'desc' => $this->l('Load only every second (2) or third (3) column so that spins load faster'),
            ),
            array(
                'type' => 'text',
                'name' => 'row_increment',
                'label' => $this->l('Row increment'),
                'default' => '1',
                'desc' => $this->l('Load only every second (2) or third (3) row so that spins load faster'),
            ),


//            array(
//                'type' => 'switch',
//                'label' => $this->l('Show hint message'),
//                'name' => 'enable_message_',
//                'is_bool' => true,
//                'values' => ApPageSetting::returnYesNo(),
//                'default' => '1',
//            ),
            array(
                'type' => 'text',
                'name' => 'message',
                'label' => $this->l('Message under image'),
                'default' => 'Drag image to spin',
                'lang' => true,
            ),
            array(
                'type' => 'text',
                'name' => 'message_loading',
                'label' => $this->l('Message Loading'),
                'default' => 'Text displayed while images are loading.',
                'lang' => true,
            ),
//            array(
//                'type' => 'text',
//                'name' => 'message_loading_fullscreen',
//                'label' => $this->l('Message Fullscren Loading'),
//                'default' => 'Loading...',
//                'lang' => true,
//            ),
            array(
                'type' => 'switch',
                'label' => $this->l('Show hint message'),
                'name' => 'hint',
                'is_bool' => true,
                'values' => ApPageSetting::returnYesNo(),
                'default' => 1,
            ),
            array(
                'type' => 'text',
                'name' => 'message_desktop_hint',
                'label' => $this->l('Message Desktop Hint'),
                'default' => 'Drag to spin',
                'lang' => true,
                'desc' => $this->l('Text of the hint on Desktop'),
            ),
//            array(
//                'type' => 'text',
//                'name' => 'message_mobile_hint',
//                'label' => $this->l('Message Mobile Hint'),
//                'default' => 'Swipe to spin',
//                'lang' => true,
//                'desc' => $this->l('Text of the hint on iOS/Android devices'),
//            ),
            array(
                'type' => 'hidden',
                'name' => 'total_slider',
                'default' => '',
            ),
            array(
                'label' => $this->l('Image'),
                'type' => 'selectImg',
                'href' => $href,
                'name' => 'image_360',
                'lang' => false,
                'class' => 'item-add-slide ignore-lang',
                'form_group_class' => 'apfullslider-row select-img',
                'show_image' => false,
            )
        );
        return $inputs;
    }

    /**
     * Widget can override this method and add more config at here
     */
    public function addConfigList($values)
    {
        // Get value with keys special
        $config_val = array();
        $total = isset($values['total_slider']) ? $values['total_slider'] : '';

        $arr = explode('|', $total);
        $inputs = array('image360');
        $languages = Language::getLanguages(false);
        foreach ($arr as $i) {
            foreach ($inputs as $config) {
                $config_val[$config][$i] = str_replace($this->str_search, $this->str_relace_html_admin, Tools::getValue($config.'_'.$i, ''));
            }
        }

        $list_slider = '<ul id="list-slider" class="ul-apimage360">';
        $html = '';
        $image_folder = __PS_BASE_URI__.'themes/'.Context::getContext()->shop->getTheme().'/img/modules/appagebuilder/images/';
        foreach ($arr as $i)
        {
            if ($i && $config_val['image360'][$i])
            {
                    $image_name = $config_val['image360'][$i];
                    $input_name = 'image360_'.$i;

                    $html .= '<li id="'.$i.'" name="' .$image_name. '">';
                    $html .= '<div class="col-lg-9">';

                    $html .= '    <div class="col-lg-5"><img data-position="" data-name="' .$image_name. '" class="img-thumbnail" src="' .$image_folder.$image_name. '">';
                    $html .= '    <input type="hidden" value="' .$image_name. '" class="ApImage360" id="'.$input_name.'" name="'.$input_name.'"></div>';
                    $html .= '    <div class="col-lg-4">'. $image_name . '</div>';
                    $html .= '</div>';
                    $html .= '<div class="col-lg-3" style="text-align: right;">';
                    $html .= '    <button type="button" class="btn-delete-fullslider btn btn-danger"><i class="icon-trash"></i> Delete</button>';
                    $html .= '</div>';
                    $html .= '</li>';
            }
        }
        $list_slider .= $html . '</ul>';
        $input = array(
            'type' => 'html',
            'name' => 'default_html',
            'html_content' => $list_slider
        );
        $this->config_list[] = $input;
    }

    public function prepareFontContent($assign, $module = null)
    {

        $total_slider = isset($assign['formAtts']['total_slider']) ? $assign['formAtts']['total_slider'] : '';
        $list = explode('|', $total_slider);
        

        $image_list = array();
        $image_path = _THEME_IMG_DIR_.'modules/'.$this->module_name.'/images/';

        foreach ($list as $item) {
            if(isset($assign['formAtts']['image360_'.$item]))
            $image_list[] = $assign['formAtts']['image360_'.$item];
        }

        $assign['formAtts']['image_path'] = $image_path;
        $assign['formAtts']['columns'] = count($list);
        $assign['formAtts']['row'] = 1;
        $assign['formAtts']['image_list'] = $image_list;

        // IMAGE DEFAULT
        $min_key = min(array_keys($image_list));
        $assign['formAtts']['image_default'] = $image_list[$min_key];

        return $assign;
    }
}
