<?php
/**
 * OrokuBlocks SportsPress Player Card
 * 
 * @package OrokuBlocks
 * @author OrokuBlocks (https://orokublocks.com/)
 * 
 * @since 1.0.0
 * 
 */

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

if ( ! class_exists( 'OrokuBlocks_SportspressPlayerCard_Widget' ) ) {
  class OrokuBlocks_SportspressPlayerCard_Widget extends OrokuBlocks_Widget {
    /**
     * Initialize property values
     * 
     * @since 1.0.0
     * 
     * @access protected
     */
    protected function initialize() {
      $this->widget_id = 'sportspress_player_card';

      $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',
        'use_plugin_font'                         => true,
        'font_scale'                              => 1,
        'card_border_radius'                      => 24,
        'card_image_border_radius'                => 18,
        'player_image_style'                      => 'contained',
        'player_background_type'                  => 'color',
        'player_background_color'                 => '#6147ff',
        'player_background_gradient_start_color'  => '#4527ff',
        'player_background_gradient_end_color'    => '#d400ff',
        'player_overlay_color'                    => '#000000',
        'player_tag_color'                        => '#6147ff',
        'player_metrics_background_color'         => '#6147ff',
        'frame_style'                             => 'holographic',
        'width'                                   => 0,
        'cover_image_url'                         => '',
        'image_url'                               => '',
        'featured_image_url'                      => '',
        'title'                                   => '',
        'number'                                  => '',
        'tag_text'                                => '',
        'metrics'                                 => [
          [
            'title' => '',
            'text'  => ''
          ]
        ],
        'shortcode'                               => false
      ];

      $this->control_modules = [
        'sportspress_player_content' => [],
        'sportspress_media_content' => [],
        'sportspress_player_metrics' => [],
        'theme' => [],
        'theme_light' => [],
        'theme_dark' => [],
        'font' => [],
        'card' => [ 'card_border_radius' ],
        'media_style' => [],
        'sportspress_frame_style' => [],
        'sportspress_player_style' => []
      ];

      $this->control_section_conditions = [
        'sportspress_frame_style' => [
          'condition'  => [
            'version'  => [ 'v4' ]
          ]
        ],
        'sportspress_player_style'  => [
          'controls'  => [
            'player_metrics_background_color' => [
              'version'  => [ 'v3' ]
            ]
          ]
        ]
      ];
    }

    /**
     * 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 a player id
      if ( $widget_args['player_id'] === '' ) {
        $widget_args['orokublocks_widget_content_needs_user_input'] = true;
        
        return $widget_args;
      }

      $player = OrokuBlocks_Sportspress::playerGet( $widget_args['player_id'] );

      // // if a player with that id exists
      if ( $player ) {
        $widget_args['title'] = $player['title'];
        $widget_args['number'] = $player['number'];
        $widget_args['image_url'] = $player['image_url'];
        $widget_args['logo_image_url'] = array_key_exists( 'image_url', $player['team'] ) ? $player['team']['image_url'] : '';
        $widget_args['featured_image_url'] = array_key_exists( 'image_url', $player['nationality'] ) ? $player['nationality']['image_url'] : '';
        $widget_args['tag_text'] = array_key_exists( 'name', $player['position'] ) ? strtoupper( $player['position']['name'] ) : '';

        // only show metrics as indicated by the user
        $player_metrics = array_change_key_case( $player['metrics'], CASE_LOWER );
        
        $widget_args['metrics'] = [];
        
        foreach ( $widget_args['sportspress_metrics'] as $sportspress_metric ) {
          $metric_id = $sportspress_metric['metric'];

          if ( $metric_id === '' ) {
            continue;
          }

          $metric = '-';

          if ( array_key_exists( $metric_id, $player_metrics ) ) {
            $metric = $player_metrics[$metric_id];
          }

          $widget_args['metrics'][] = [
            'title' => strtoupper( $metric_id ),
            'text'  => $metric
          ];
        }
      } else {
        $widget_args['orokublocks_widget_content_error'] = [
          'title' => __( 'SportsPress Player', 'orokublocks' ),
          'text'  => __( 'A player with that id could not be found. Please ensure that the id you entered exactly matches the id of the player you want to display.', 'orokublocks' )
        ];
      }

      return $widget_args;
    }
  }
}