<?php

namespace Reach\App\Fields\Types;

use Reach\App\Fields\RegisterFieldGroups;

/**
 * Class PageModulesSelect
 *
 * @package Reach\App\Fields\Types
 */
class PageModulesSelect extends \acf_field {

    /**
     * Set basic field properties
     *
     * @since 1.0.0
     */
    function __construct() {
        $this->name     = 'modules_select';
        $this->label    = __( 'Page Modules Selection', 'reach' );
        $this->category = __( 'Choice', 'reach' );
        $this->defaults = [];

        parent::__construct();
    }

    /**
     * Create the HTML interface for our button field
     *
     * @param $field
     */
    function render_field( $field ) {
        $field = array_merge( $this->defaults, $field );
        // if no choices exist, remove rendering
        if ( ! isset( $field['choices'] ) || ! is_array( $field['choices'] ) ) {
            return;
        }

        // get all available theme modules
        $available_modules = ( new RegisterFieldGroups() )->available_modules;

        // get field values
        $field_id      = esc_attr( $field['id'] );
        $field_name    = esc_attr( $field['name'] );
        $page_id       = esc_attr( $field['page_id'] );
        $default_value = isset( $field['default_value'] ) ? esc_attr( $field['default_value'] ) : '';

        // overwrite fields with empty values to avoid warning
        $field['value'] = isset( $field['value'] ) ? $field['value'] : $default_value;
        ?>
      <ul id="<?php echo $field_id; ?>" class="acf-select__page-modules">
        <input type="hidden" name="<?php echo $field_name; ?>[]"
               value="">
          <?php foreach ( $field['choices'] as $value => $slug ) : ?>
              <?php
              // if this key does not exist in the available modules than skip this
              if ( ! array_key_exists( $slug, $available_modules ) ) {
                  continue;
              }
              // set item label
              $label = $available_modules[ $slug ]['label'];
              // set item description
              $desc = $available_modules[ $slug ]['description'];
              // set the item icon
              $icon = '/assets/images/admin/' . $slug . '.svg';
              ?>
            <li class="acf__page-module">
              <label for="<?php echo $value; ?>">
                <input type="checkbox" id="<?php echo $value; ?>" name="<?php echo $field_name; ?>[]"
                       value="<?php echo $value; ?>" checked="checked">
                  <?php
                  // check that we have an existing icon for this module
                  if ( file_exists( get_template_directory() . $icon ) ) {
                      echo '<div class="page-module__icon page-module__icon--' . $slug . '"><img src="' . get_template_directory_uri() . esc_url( $icon ) . '"></div>';
                  }
                  printf( '<strong>' . __( '%s', 'ad-starter' ) . ' - <div class="page-module__id">' . substr( $value, - 4 ) . '</div><span>%s</span></strong>', esc_html( $label ), esc_html( $desc ) );
                  ?>
                <a href="javascript:void(0);" class="page-module__duplicate"
                   data-id="<?php echo strstr( $value, 'group' ); ?>" data-page="<?php echo $page_id; ?>">
                  <img src="<?php echo admin_url( 'images/spinner.gif' ); ?>">
                </a>
                <a href="javascript:void(0);" class="page-module__remove"></a>
              </label>
            </li>
          <?php endforeach; ?>
      </ul>
      <a href="javascript:void(0);" class="page-modules__toggle" data-id="<?php echo $field_id; ?>">
          <?php _e( 'Add Module', 'reach' ); ?>
      </a>
        <?php
        // get available module options
        if ( ! empty( $available_modules ) ) {
            echo '<ul class="acf__available-modules">';
            // loop through and build list of module options
            foreach ( $available_modules as $slug => $module ) {
                $icon     = '/assets/images/admin/' . $slug . '.svg';
                $icon_url = ( file_exists( get_template_directory() . $icon ) ? esc_url( get_template_directory_uri() . $icon ) : '' );
                echo '<li id="' . esc_attr( $slug ) . '" data-desc="' . $module['description'] . '" data-icon="' . $icon_url . '">' .
                     ( $icon_url ? '<img src="' . $icon_url . '">' : '' ) .
                     esc_html( $module['label'] ) .
                     '</li>';
            }
            echo '</ul>';
        }
    }
}
