<?php
/**
 * Main Class for building social icons widgets
 * extending WP_Widget class.
 *
 * @author jason.xie@victheme.com
 *
 * @see VTCore_Wordpress_Init();
 */
class VTCore_Wordpress_Widgets_Social
extends WP_Widget {


  private $defaults = array(
    'title' => '',
    'description' => '',
    'align' => 'text-center',
    'links' => array(
      array(
       'icon' => '',
        'href' => '',
      ),
    ),
  );


  private $instance;
  private $args;


  // Define the valid icon that icon-picker
  // can display.
  private $iconset = array(
    'tumblr',
    'facebook',
    'google-plus',
    'pinterest',
    'linkedin',
    'github',
    'twitter',
    'flickr' ,
    'youtube',
    'dribbble',
    'instagram',
    'weibo',
    'bitbucket',
    'dropbox',
    'foursquare',
    'gittip',
    'renren',
    'skype',
    'stack-exchange',
    'trello',
    'vk',
    'vimeo-square',
    'xing',
  );



  /**
   * Registering widget as WP_Widget requirement
   */
  public function __construct() {
    parent::__construct(
      'vtcore_wordpress_widgets_social',
      'Social Icon',
      array('description' => 'Displaying Social Icons')
    );
  }




  /**
   * Registering widget
   */
  public function registerWidget() {
    return register_widget('vtcore_wordpress_widgets_social');
  }





  /**
   * Extending widget
   *
   * @see WP_Widget::widget()
   */
  public function widget($args, $instance) {

    $this->args = $args;
    $this->instance = VTCore_Utility::arrayMergeRecursiveDistinct($instance, $this->defaults);

    $title = apply_filters( 'widget_title', $instance['title'] );
    echo $this->args['before_widget'];

    if (!empty($title)) {
      echo $this->args['before_title'] . $title . $this->args['after_title'];
    }

    $element = new VTCore_Bootstrap_Element_Base();

    if (!empty($this->instance['description'])) {
      $element
        ->BsElement(array(
          'type' => 'div',
          'text' => $this->instance['description'],
          'attributes' => array(
            'class' => array('post-social-desciption'),
          ),
          'raw' => true,
        ));
    }

    $social = $element
      ->BsElement(array(
        'type' => 'div',
        'attributes' => array(
          'class' => array('post-social'),
        ),
      ))
      ->lastChild()
      ->List(array(
        'type' => 'ul',
        'attributes' => array(
          'class' => array('unstyled', 'inline', $instance['align'])
        ),
      ))
      ->lastChild();

    foreach ($this->instance['links'] as $link) {
      $icon = new VTCore_Html_HyperLink(array(
        'attributes' => array(
          'href' => $link['href'],
        ),
      ));

      $icon->addChildren(new VTCore_Fontawesome_faIcon($link));
      $social->addContent($icon);
    }

    // Allow other plugin to alter the output
    do_action('wp_widget_social_output', $element, $this);

    $element->render();

    echo $args['after_widget'];
  }





  /**
   * Widget configuration form
   * @see WP_Widget::form()
   */
  public function form($instance) {

    VTCore_Wordpress_Utility::loadAsset('jquery-table-manager');
    VTCore_Wordpress_Utility::loadAsset('wp-bootstrap');
    VTCore_Wordpress_Utility::loadAsset('wp-widget');


    $this->instance = VTCore_Utility::arrayMergeRecursiveDistinct($instance, $this->defaults);

    $this
      ->buildForm()
      ->processForm()
      ->processError(true)
      ->render();

  }




  /**
   * Function for building the form object
   */
  private function buildForm() {

    $widget = new VTCore_Bootstrap_Form_BsInstance(array(
      'type' => false,
    ));

    $widget
      ->BsText(array(
        'text' => __('Title', VTCORE_LANGUAGE),
        'name' => $this->get_field_name('title'),
        'id' => $this->get_field_id('title'),
        'value' => $this->instance['title'],
      ))
      ->BsTextarea(array(
        'text' => __('Description', VTCORE_LANGUAGE),
        'name' => $this->get_field_name('description'),
        'id' => $this->get_field_id('description'),
        'value' => $this->instance['description'],
      ))
      ->BsSelect(array(
        'text' => __('Alignment', VTCORE_LANGUAGE),
        'name' => $this->get_field_name('align'),
        'id' => $this->get_field_id('align'),
        'value' => $this->instance['align'],
        'options' => array(
          'text-left' => __('Left', VTCORE_LANGUAGE),
          'text-right' => __('Right', VTCORE_LANGUAGE),
          'text-center' => __('Center', VTCORE_LANGUAGE),
        ),
      ))
      ->BsElement(array(
        'type' => 'div',
        'attributes' => array(
          'class' => array('table-manager'),
        ),
      ))
      ->lastChild()
      ->Table(array(
        'headers' => array(
          ' ',
          __('Content', VTCORE_LANGUAGE),
          ' ',
        ),
        'rows' => $this->buildRows(),
        'attributes' => array(
          'data-filter' => 2,
        ),
      ))
      ->Button(array(
        'text' => __('Add New Entry', VTCORE_LANGUAGE),
        'attributes' => array(
          'data-tablemanager-type' => 'addrow',
          'class' => array('button', 'button-large', 'button-primary'),
        ),
      ));

    return $widget;
  }





  /**
   * Building the table manager rows
   */
  private function buildRows() {
    $rows = array();
    foreach ($this->instance['links'] as $key => $link) {

      // Draggable Icon
      $rows[$key][] = array(
        'content' => new VTCore_Bootstrap_Element_BsElement(array(
          'type' => 'span',
          'attributes' => array(
            'class' => array('drag-icon'),
          ),
        )),
        'attributes' => array(
          'class' => array('drag-element'),
        ),
      );


      // Icon selector
      $rows[$key]['content'] = new VTCore_Bootstrap_Element_Base();
      $rows[$key]['content']
        ->addOverloaderPrefix('VTCore_Fontawesome_Form_')
        ->faIcon(array(
          'iconset' => $this->iconset,
          'text' => __('Icon', VTCORE_LANGUAGE),
          'name' => $this->get_field_name('links][' . $key . '][icon'),
          'value' => $link['icon'],
        ))
        ->BsUrl(array(
          'text' => __('Link URL', VTCORE_LANGUAGE),
          'name' => $this->get_field_name('links][' . $key . '][href'),
          'value' => $link['href'],
          'required' => true,
        ));


      // Remove button
      $rows[$key][] = array(
        'content' => new VTCore_Form_Button(array(
            'text' => 'X',
            'attributes' => array(
              'data-tablemanager-type' => 'removerow',
              'class' => array('button', 'button-mini', 'form-button'),
            ),
          )),
          'attributes' => array(
            'class' => array('remove-button')
          ),
        );
    }

    return $rows;
  }





  /**
   * Widget update function.
   * @see WP_Widget::update()
   */
  public function update($new_instance, $old_instance) {

    $form = $this->buildForm()->processForm()->processError();
    $errors = $form->getErrors();

    if (empty($errors)) {
      return wp_unslash($new_instance);
    }

    return false;
  }
}