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

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

class OrokuBlocks_GamipressAchievement_Widget extends OrokuBlocks_Widget {
  /**
   * Initialize property values
   * 
   * @since 1.0.0
   * 
   * @access protected
   */
  protected function initialize() {
    $this->widget_id = 'gamipress_achievement';

    $this->defaults = [
      'theme'                                                 => 'light',
      'light_primary_color'                                   => '#00d4cf',
      'light_background_color'                                => '#ffffff',
      'light_background_alt_color'                            => '#f7f7f9',
      'light_line_color'                                      => '#c7cdd6',
      'light_text_color'                                      => '#22212c',
      'dark_primary_color'                                    => '#09dfb9',
      'dark_background_color'                                 => '#2b2a3b',
      'dark_background_alt_color'                             => '#21202e',
      'dark_line_color'                                       => '#38374b',
      'dark_text_color'                                       => '#ffffff',
      'progress_bar_color'                                    => '#6147ff',
      'progress_bar_underline_color'                          => '#c7cdd6',
      'progress_bar_gradient'                                 => true,
      'progress_bar_gradient_start_color'                     => '#6147ff',
      'progress_bar_gradient_end_color'                       => '#00ffc2',
      'achievement_unlocked_background_color'                 => '#6147ff',
      'achievement_unlocked_background_gradient'              => true,
      'achievement_unlocked_background_gradient_start_color'  => '#6147ff',
      'achievement_unlocked_background_gradient_end_color'    => '#00ffc2',
      'use_plugin_font'                                       => true,
      'font_scale'                                            => 1,
      'card_border_radius'                                    => 24,
      'width'                                                 => 0,
      'cover_image_url'                                       => '',
      'image_url'                                             => '',
      'title'                                                 => '',
      'summary'                                               => '',
      'tag_image_url'                                         => '',
      'tag_text'                                              => '',
      'score_value'                                           => 75,
      'score_max_value'                                       => 100,
      'decimal_points'                                        => 0,
      'steps'                                                 => [
        [
          'title'     => '',
          'complete'  => false
        ]
      ],
      'progress_text'                                         => 'COMPLETED',
      'locked_text'                                           => 'LOCKED',
      'unlocked_text'                                         => 'UNLOCKED!',
      'progress_bar_width'                                    => 276,
      'progress_bar_breakpoint_width'                         => 680,
      'progress_bar_split_parts'                              => 1,
      'animate_on_scroll'                                     => true,
      'earners_count'                                         => 0,
      'earners_text'                                          => 'USERS ALREADY HAVE THIS BADGE!',
      'earners'                                               => [
        [
          'image_url' => ''
        ]
      ],
      'shortcode'                             => false
    ];

    $this->control_modules = [
      'gamipress_achievement_content' => [],
      'gamipress_media_content' => [],
      'gamipress_content' => [],
      'gamipress_earners_content' => [],
      'theme' => [],
      'theme_light' => [],
      'theme_dark' => [],
      'font' => [],
      'card' => [],
      'gamipress_content_style' => [],
      'progress_bars_style' => []
    ];

    $this->control_section_conditions = [
      'gamipress_media_content' => [
        'condition'  => [
          'version'  => [ 'v1', 'v4' ]
        ]
      ],
      'gamipress_content' => [
        'controls'  => [
          'progress_text'  => [
            'version'  =>  [ 'v1', 'v3', 'v4' ]
          ]
        ]
      ],
      'gamipress_earners_content' => [
        'condition'  => [
          'version'  => [ 'v1', 'v2', 'v3' ]
        ]
      ],
      'gamipress_content_style' => [
        'condition'  => [
          'version'  => [ 'v2' ]
        ]
      ],
      'progress_bars_style' => [
        'condition'  => [
          'version'  => [ 'v1', 'v3', 'v4' ]
        ]
      ]
    ];
  }

  /**
   * Convert Elementor control settings structure to widget arguments structure
   * 
   * @since 1.0.0
   * 
   * @param array   $control_settings       Elementor control setting values.
   * @return array  $widget_args            Widget compatible arguments.
   */
  public function parseElementorControlSettings( $control_settings ) {
    $widget_args = parent::parseElementorControlSettings( $control_settings );

    // if user has not entered an achievement slug
    if ( $widget_args['achievement_name'] === '' ) {
      $widget_args['orokublocks_widget_content_needs_user_input'] = true;
      
      return $widget_args;
    }

    $achievement = Orokublocks_Gamipress::achievementGet( $widget_args['achievement_name'] );

    // if an achievement with that slug exists
    if ( $achievement ) {
      $widget_args['title'] = $achievement['title'];
      $widget_args['summary'] = $achievement['content'];

      $widget_args['image_url'] = $achievement['image_url'];

      $widget_args['tag_text'] = $achievement['points_awarded']['amount'];
      $widget_args['tag_image_url'] = $achievement['points_awarded']['image_url'];

      $widget_args['earners_count'] = $achievement['earners']['count'];
      $widget_args['earners'] = $achievement['earners']['users'];

      $widget_args['score_value'] = $achievement['complete'] ? 100 : 0;
      $widget_args['score_max_value'] = 100;
      $widget_args['decimal_points'] = 0;

      $widget_args['steps'] = $achievement['steps'];

      $steps_count = count( $achievement['steps'] );

      if ( ( $steps_count > 0 ) && ( ! $achievement['complete'] ) ) {
        $completed_steps_count = count(
          array_filter(
            $achievement['steps'],
            function ( $step ) {
              return $step['complete'];
            }
          )
        );

        $widget_args['score_value'] = ( $completed_steps_count / $steps_count ) * $widget_args['score_max_value'];
      }
    } else {
      $widget_args['orokublocks_widget_content_error'] = [
        'title' => __( 'GamiPress Achievement', 'orokublocks' ),
        'text'  => __( 'An achievement with that name could not be found. Please ensure that the name you entered exactly matches the slug of the achievement you want to display.', 'orokublocks' )
      ];
    }

    return $widget_args;
  }
}