<?php
/**
 * Class for building valid VTCore_Wordpress_Factory_Customizer
 * arrays for Zeus Schema options
 *
 * @author jason.xie@victheme.com
 * @todo use model buildContext() ?
 */
class VTCore_Zeus_Customizer_Schema
extends VTCore_Zeus_Customizer_Model {

  private $colorBuilder;

  protected $panelKey = 'zeus_schema';
  protected $key = 'schema';

  /**
   * Method for building the schema color options
   */
  public function register() {

    if (!$this->getCache()) {

      $this->object = new VTCore_Zeus_Schema_Factory();

      $i = 1;

      $this->object->setObject('colorBuilder', new VTCore_Zeus_Schema_Color($this->object->getActiveSchema()->getData('color'), false, false, true));

      $options = array();
      foreach ($this->object->getSchemas() as $key => $schema) {
        $options[$schema->getSchema('id')] = $schema->getSchema('name');
      }

      $this->context[$this->key]['panels']['styles'] = array(
        'title' => __('Color & Fonts', BASE_THEME),
        'priority' => 403,
        'capability' => 'edit_theme_options',
        'description' => __('Configure the theme color and fonts', BASE_THEME),
      );

      $this->context[$this->key]['sections']['schema'] = array(
        'title' => __('Change Schema', BASE_THEME),
        'priority' => 0,
        'capability' => 'edit_theme_options',
        'panel' => 'styles',
      );

      $this->context[$this->key]['settings']['schema-active'] = array(
        'object' => false,
        'default' => $this->object->getActiveSchemaID(),
        'transport' => 'postMessage',
        'capability' => 'edit_theme_options',
        'priority' => 0,
      );

      $this->context[$this->key]['controls']['schema-active'] = array(
        'object' => 'VTCore_Wordpress_Customizer_Element_Select',
        'label' => __('Select Style Schemas', BASE_THEME),
        'section' => 'schema',
        'settings' => 'schema-active',
        'priority' => 0,
        'type' => 'select',
        'choices' => $options,
      );

      foreach ($this->object->getActiveSchema()->getData('color') as $key => $data) {

        // Dont process broken color schema entry
        if (!isset($data['title'])
            || !isset($data['description'])
            || !isset($data['selectors'])
            || !isset($data['parent'])) {

          continue;
        }

        $section = 'color-' . $key . '-section' ;

        $this->context[$this->key]['sections'][$section] = array(
          'title' => $data['title'],
          'description' => $data['description'],
          'panel' => 'styles',
          'priority' => $i++,
        );

        foreach ($data as $elkey => $value) {

          if (in_array($elkey, array('title', 'description', 'selectors', 'parent'))) {
            continue;
          }

          $text = str_replace('-', ' ', ucfirst($elkey));
          $name = 'schemas[color][' . $key . '][' . $elkey . ']';
          $objectKey = $key . '--' . $elkey;
          $target = $this->getColorTarget($objectKey);
          $rules = $this->getColorRules($objectKey);

          $this->context[$this->key]['settings'][$name] = array(
            'default'     => $value,
            'transport'   => 'postMessage',
            'capability' => 'edit_theme_options',
            'priority' => $i++,
          );


          if (strpos($elkey, 'color') !== false
              || strpos($elkey, '-hover') !== false
              || strpos($elkey, '-focus') !== false
              || strpos($elkey, '-visited') !== false) {

            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'VTCore_Wordpress_Customizer_Element_Color',
              'label' =>  $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
              'type' => 'vtcore_color',
            );

          }

          // Not working yet!
          elseif (strpos($elkey, 'gradient') !== false) {

            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'VTCore_Wordpress_Customizer_Element_Gradient',
              'label' =>  $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
              'type' => 'vtcore_gradient',
            );


          }

          elseif (strpos($elkey, 'image') !== false) {

            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'WP_Customize_Image_Control',
              'label' =>  $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
            );

          }

          elseif (strpos($elkey, 'background-repeat') !== false) {

            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'VTCore_Wordpress_Customizer_Element_Select',
              'label' => $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
              'type' => 'vtcore_select',
              'choices' => array(
                '' => __('Not set', BASE_THEME),
                'no-repeat' => __('No Repeat', BASE_THEME),
                'repeat' => __('Repeat All', BASE_THEME),
                'repeat-x' => __('Repeat Horizontally', BASE_THEME),
                'repeat-y' => __('Repeat Vertically', BASE_THEME),
              ),
            );


          }
          elseif (strpos($elkey, 'border-style') !== false) {

            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'VTCore_Wordpress_Customizer_Element_Select',
              'label' => $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
              'type' => 'vtcore_select',
              'choices' => array(
                '' => __('Not set', BASE_THEME),
                'none' => __('None', BASE_THEME),
                'inherit' => __('Inherit', BASE_THEME),
                'solid' => __('Solid', BASE_THEME),
                'dotted' => __('Dotted',BASE_THEME),
                'dashed' => __('Dashed', BASE_THEME),
                'double' => __('Double', BASE_THEME),
                'ridge' => __('Ridge', BASE_THEME),
                'inset' => __('Inset', BASE_THEME),
                'outset' => __('Outset', BASE_THEME),
                'groove' => __('Groove', BASE_THEME),
              ),
            );


          }
          elseif (strpos($elkey, 'font-style') !== false
            || strpos($elkey, 'heading-style') !== false) {

            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'VTCore_Wordpress_Customizer_Element_Select',
              'label' => $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
              'type' => 'vtcore_select',
              'choices' => array(
                '' => __('Not set', BASE_THEME),
                'normal' => __('Normal', BASE_THEME),
                'oblique' => __('Oblique', BASE_THEME),
                'italic' => __('Italic', BASE_THEME),
                'inherit' => __('Inherit',BASE_THEME),
              ),
            );

          }
          elseif (strpos($elkey, 'weight') !== false) {

            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'VTCore_Wordpress_Customizer_Element_Select',
              'label' => $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
              'type' => 'vtcore_select',
              'choices' => array(
                '' => __('Not set', BASE_THEME),
                'inherit' => __('Inherit',BASE_THEME),
                100 => __('Thin', BASE_THEME),
                200 => __('Extra light', BASE_THEME),
                300 => __('Light', BASE_THEME),
                400 => __('Normal', BASE_THEME),
                500 => __('Medium', BASE_THEME),
                600 => __('Demi bold', BASE_THEME),
                700 => __('Bold', BASE_THEME),
                800 => __('Heavy', BASE_THEME),
                900 => __('Black', BASE_THEME),
              ),
            );

          }
          elseif (strpos($elkey, 'family') !== false) {

            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'VTCore_Wordpress_Customizer_Element_Font',
              'label' => $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
              'type' => 'vtcore_font',
            );
          }


          else {
            $this->context[$this->key]['controls'][$name] = array(
              'object' => 'VTCore_Wordpress_Customizer_Element_Text',
              'label' => $text,
              'section' => $section,
              'settings' => $name,
              'priority' => $i++,
              'type' => 'vtcore_text',
            );
          }

          $this->context[$this->key]['pointers'][$name] = array(
            'target' => implode(', ',$target),
            'rules' => array_shift($rules),
          );
        }
      }

      $this->setCache();
    }

    $this->insert();

    return $this;
  }


  public function getColorTarget($key) {
    return $this->object->getObject('colorBuilder')->getSchema($key)->getTarget();
  }



  public function getColorRules($key) {
    $rules = array();
    $objects = $this->object->getObject('colorBuilder')->getSchema($key)->getRules();
    $object = array_shift($objects);

    if (is_object($object)) {
      foreach ($object->getRules() as $rule) {
        list($key, $value) = explode(':', $rule);
        $rules[] = $key;
      }
    }

    return $rules;
  }

}