<?php
N2Loader::import('libraries.plugins.N2SliderWidgetAbstract', 'smartslider');
N2Loader::import('libraries.image.color');

class N2SSPluginWidgetBulletNumbers extends N2SSPluginWidgetAbstract {

    var $_name = 'numbers';

private static $key = 'widget-bullet-';

static function getDefaults() {
    return array(
        'widget-bullet-position-mode'        => 'simple',
        'widget-bullet-position-area'        => 10,
        'widget-bullet-position-offset'      => 5,
        'widget-bullet-action'               => 'click',
        'widget-bullet-style'                => 'eyJuYW1lIjoiU3RhdGljIiwiZGF0YSI6W3siYmFja2dyb3VuZGNvbG9yIjoiMDAwMDAwYWIiLCJwYWRkaW5nIjoiNXwqfDl8Knw1fCp8OXwqfHB4IiwiYm94c2hhZG93IjoiMHwqfDB8KnwwfCp8MHwqfDAwMDAwMGZmIiwiYm9yZGVyIjoiMHwqfHNvbGlkfCp8MDAwMDAwZmYiLCJib3JkZXJyYWRpdXMiOiIwIiwiZXh0cmEiOiJtaW4td2lkdGg6IDEwcHg7XG5tYXJnaW46IDRweDsifSx7ImJhY2tncm91bmRjb2xvciI6IjVjYmEzY2ZmIiwicGFkZGluZyI6IjV8Knw5fCp8NXwqfDl8KnxweCJ9XX0=',
        'widget-bullet-font'                 => 'eyJuYW1lIjoiU3RhdGljIiwiZGF0YSI6W3siY29sb3IiOiJmZmZmZmZmZiIsInNpemUiOiIxNHx8cHgiLCJ0c2hhZG93IjoiMHwqfDB8KnwwfCp8MDAwMDAwZmYiLCJhZm9udCI6Ik1vbnRzZXJyYXQiLCJsaW5laGVpZ2h0IjoiMS4yIiwiYm9sZCI6MCwiaXRhbGljIjowLCJ1bmRlcmxpbmUiOjAsImFsaWduIjoiY2VudGVyIiwiZXh0cmEiOiIifSx7ImNvbG9yIjoiZmZmZmZmZmYifV19',
        'widget-bullet-bar'                  => '',
        'widget-bullet-align'                => 'center',
        'widget-bullet-orientation'          => 'auto',
        'widget-bullet-bar-full-size'        => 0,
        'widget-bullet-overlay'              => 0,
        'widget-bullet-thumbnail-show-image' => 0,
        'widget-bullet-thumbnail-width'      => 100,
        'widget-bullet-thumbnail-width'      => 60,
        'widget-bullet-thumbnail-style'      => 'eyJuYW1lIjoiU3RhdGljIiwiZGF0YSI6W3siYmFja2dyb3VuZGNvbG9yIjoiMDAwMDAwODAiLCJwYWRkaW5nIjoiM3wqfDN8KnwzfCp8M3wqfHB4IiwiYm94c2hhZG93IjoiMHwqfDB8KnwwfCp8MHwqfDAwMDAwMGZmIiwiYm9yZGVyIjoiMHwqfHNvbGlkfCp8MDAwMDAwZmYiLCJib3JkZXJyYWRpdXMiOiIzIiwiZXh0cmEiOiJtYXJnaW46IDVweDsifV19',
        'widget-bullet-thumbnail-side'       => 'before'
    );
}

function onBulletList(&$list) {
    $list[$this->_name] = $this->getPath();
}

function getPath() {
    return dirname(__FILE__) . DIRECTORY_SEPARATOR . 'numbers' . DIRECTORY_SEPARATOR;
}

static function getPositions(&$params) {
    $positions                    = array();
    $positions['bullet-position'] = array(
        self::$key . 'position-',
        'bullet'
    );

    return $positions;
}

/**
 * @param $slider N2SmartSliderAbstract
 * @param $id
 * @param $params
 *
 * @return string
 */
static function render($slider, $id, $params) {
	
	if (count($slider->slides) <= 1) {
		return '';
	}

        N2LESS::addFile(N2Filesystem::translate(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'numbers' . DIRECTORY_SEPARATOR . 'style.n2less'), $slider->cacheId, array(
            "sliderid" => $slider->elementId
        ), NEXTEND_SMARTSLIDER_ASSETS . '/less' . NDS);
        N2JS::addFile(N2Filesystem::translate(dirname(__FILE__) . '/../transition/transition/bullet.min.js'), $id);
    

        list($displayClass, $displayAttributes) = self::getDisplayAttributes($params, self::$key);

        $bulletStyle = N2StyleRenderer::render($params->get(self::$key . 'style'), 'dot', $slider->elementId, 'div#' . $slider->elementId . ' ');
        $barStyle    = N2StyleRenderer::render($params->get(self::$key . 'bar'), 'simple', $slider->elementId, 'div#' . $slider->elementId . ' ');

    $bulletFont = N2FontRenderer::render($params->get(self::$key . 'font'), 'dot', $slider->elementId, 'div#' . $slider->elementId . ' ', $slider->fontSize);

    list($style, $attributes) = self::getPosition($params, self::$key);
    $attributes['data-offset'] = $params->get(self::$key . 'position-offset', 0);

    $dots = array();
    $i    = 1;
    foreach ($slider->slides AS $slide) {
        $label  = $i;
        $dots[] = N2Html::tag('div', array(
            'class'    => 'n2-ow ' . $bulletStyle . $bulletFont,
            'tabindex' => '0'
        ), $label);

        $i++;
    }

    $orientation = self::getOrientationByPosition($params->get(self::$key . 'position-mode'), $params->get(self::$key . 'position-area'), $params->get(self::$key . 'orientation'));
    if ($orientation == 'auto') {
        $orientation = 'horizontal';
    }
    $html = '';
    switch ($orientation) {
        case 'vertical':
            $html .= implode('<br>', $dots);
            break;
        default:
            $html .= implode('', $dots);
    }

    $parameters = array(
        'overlay' => $params->get(self::$key . 'position-mode') || $params->get(self::$key . 'overlay'),
        'area'    => intval($params->get(self::$key . 'position-area'))
    );

    $thumbnails = array();
    if ($params->get(self::$key . 'thumbnail-show-image')) {
        foreach ($slider->slides AS $slide) {
            $thumbnails[] = $slide->getThumbnail();
        }
        $parameters['thumbnailWidth']  = intval($params->get(self::$key . 'thumbnail-width'));
        $parameters['thumbnailHeight'] = intval($params->get(self::$key . 'thumbnail-height'));
        $parameters['thumbnailStyle']  = N2StyleRenderer::render($params->get(self::$key . 'thumbnail-style'), 'simple', $slider->elementId, '');
        $side                          = $params->get(self::$key . 'thumbnail-side', 'before');


        if ($side == 'before') {
            if ($orientation == 'vertical') {
                $position = 'left';
            } else {
                $position = 'top';
            }
        } else {
            if ($orientation == 'vertical') {
                $position = 'right';
            } else {
                $position = 'bottom';
            }
        }
        $parameters['thumbnailPosition'] = $position;
    }
    $parameters['thumbnails'] = $thumbnails;
    $parameters['action']     = $params->get(self::$key . 'action');
    $parameters['numeric']    = 1;

    N2JS::addInline('new N2Classes.SmartSliderWidgetBulletTransition("' . $id . '", ' . json_encode($parameters) . ');');

    $fullSize = intval($params->get(self::$key . 'bar-full-size'));
    if ($fullSize) {
        $barStyle .= "n2-bullet-bar-full-size ";
    }

    return N2Html::tag("div", $displayAttributes + $attributes + array(
            "class" => $displayClass . ' n2-ss-control-bullet',
            "style" => $style
        ), N2HTML::tag("div", array(
        "class" => $barStyle . " nextend-bullet-bar n2-ow nextend-bullet-bar-" . $orientation,
        "style" => "text-align: " . $params->get(self::$key . 'align') . ";"
    ), $html));
}

public static function prepareExport($export, $params) {
    $export->addVisual($params->get(self::$key . 'style'));
    $export->addVisual($params->get(self::$key . 'bar'));
    $export->addVisual($params->get(self::$key . 'font'));
}

public static function prepareImport($import, $params) {

    $params->set(self::$key . 'style', $import->fixSection($params->get(self::$key . 'style')));
    $params->set(self::$key . 'bar', $import->fixSection($params->get(self::$key . 'bar')));
        $params->set(self::$key . 'font', $import->fixSection($params->get(self::$key . 'font')));
    }
}

N2Plugin::addPlugin('sswidgetbullet', 'N2SSPluginWidgetBulletNumbers');
