<?php
/**
 * OrokuBlocks Elementor Widget
 * 
 * @package OrokuBlocks
 * @author OrokuBlocks (https://orokublocks.com/)
 * 
 * @since 1.0.0
 * 
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

class OrokuBlocks_Elementor_Widget extends OrokuBlocks_Elementor_Widget_Base {
  /**
   * Register widget scripts for the elementor frontend
   * 
   * @since 1.0.0
   * 
   * @access protected
   */
  protected function registerScripts() {
    $widget_path_id = OrokuBlocks_Router::widgetPathIdGet( $this->widget_id );

    wp_register_script(
      'orokublocks-' . $widget_path_id . '-elementor-widget-scripts',
      OrokuBlocks_Router::widgetUrlGet( $this->widget_id ) . '/elementor/' . $widget_path_id . '.js',
      $this->script_dependencies,
      '1.0.0',
      true
    );
  }

  /**
   * Returns the ids of the scripts that are required to run the widget
   * 
   * @since 1.0.0
   */
  public function get_script_depends() {
    return [ 'orokublocks-' . OrokuBlocks_Router::widgetPathIdGet( $this->widget_id ) . '-elementor-widget-scripts' ];
  }

  /**
   * Define the widget controls (setting fields)
   * 
   * @since 1.0.0
   * 
   * @access protected
   */
  protected function register_controls() {
    orokublocks()->widgets[$this->widget_id]['instance']->registerElementorControlSections( $this );
  }

  /**
   * Template that generates the frontend output
   * 
   * @since 1.0.0
   * 
   * @access protected
   */
  protected function render() {
    $settings = $this->get_settings_for_display();

    if ( orokublocks()->isWidgetVersionPro( $this->widget_id, $settings['version'] ) && ( ! orokublocks()->isProVersionValid() ) ) {
      return;
    }

    $args = orokublocks()->widgets[$this->widget_id]['instance']->parseElementorControlSettings( $settings );

    if ( array_key_exists( 'orokublocks_widget_content_needs_user_input', $args ) ) {
      return;
    }

    if ( array_key_exists( 'orokublocks_widget_content_error', $args ) ) {
      orokublocks()->requireTemplate( 'includes/template/widget-content-error', $args['orokublocks_widget_content_error'] );
    } else {
      orokublocks()->includeWidget( $this->widget_id, $settings['version'], $args );
    }
  }
}