<?php
/*
Module:       MyArcadePlugin Cubepoints Integration
Author:       Daniel Bakovic
Author URI:   http://myarcadeplugin.com
Version:      1.00
*/

/**
 * Extends the Cubepoints settings page
 */
function myarcade_cp_settings() {
  ?>
  <tr valign="top">
    <th scope="row">
      <label for="cp_play_points"><?php _e('Points for playing games', MYARCADE_TEXT_DOMAIN); ?>:</label>
    </th>
    <td valign="middle">
      <input type="text" id="cp_play_points" name="cp_play_points" value="<?php echo get_option('cp_play_points'); ?>" size="30" />
    </td>
    <td>
      <input type="button" onclick="document.getElementById('cp_play_points').value='0'" value="<?php _e('Do not add points for game plays', MYARCADE_TEXT_DOMAIN); ?>" class="button" />
    </td>
  </tr>
  
  <tr valign="top">
    <th scope="row">
      <label for="cp_score_points"><?php _e('Points for submitting scores', MYARCADE_TEXT_DOMAIN); ?>:</label>
    </th>
    <td valign="middle">
      <input type="text" id="cp_score_points" name="cp_score_points" value="<?php echo get_option('cp_score_points'); ?>" size="30" />
    </td>
    <td>
      <input type="button" onclick="document.getElementById('cp_score_points').value='0'" value="<?php _e('Do not add points for score submittes', MYARCADE_TEXT_DOMAIN); ?>" class="button" />
    </td>
  </tr> 

  <tr valign="top">
    <th scope="row">
      <label for="cp_highscore_points"><?php _e('Points for getting highscore', MYARCADE_TEXT_DOMAIN); ?>:</label>
    </th>
    <td valign="middle">
      <input type="text" id="cp_highscore_points" name="cp_highscore_points" value="<?php echo get_option('cp_highscore_points'); ?>" size="30" />
    </td>
    <td>
      <input type="button" onclick="document.getElementById('cp_highscore_points').value='0'" value="<?php _e('Do not add points when user gets highscore on a game', MYARCADE_TEXT_DOMAIN); ?>" class="button" />
    </td>
  </tr> 

 <!-- <tr valign="top">
    <th scope="row">
      <label for="cp_challange_points"><?php _e('Points for winning a challange', MYARCADE_TEXT_DOMAIN); ?>:</label>
    </th>
    <td valign="middle">
      <input type="text" id="cp_challange_points" name="cp_challange_points" value="<?php echo get_option('cp_challange_points'); ?>" size="30" />
    </td>
    <td>
      <input type="button" onclick="document.getElementById('cp_challange_points').value='0'" value="<?php _e('Do not add points when user wins a challange', MYARCADE_TEXT_DOMAIN); ?>" class="button" />
    </td>
  </tr>   -->
  <?php
}


/**
 * Handles the MyArcadePlugin settings for Cubepoints
 */
function myarcade_cp_save_settings() {
  if ( isset($_POST['cp_admin_form_submit']) && $_POST['cp_admin_form_submit'] == 'Y') {
    update_option('cp_play_points', (int)$_POST['cp_play_points']);
    //update_option('cp_play_timeout', (int)$_POST['cp_play_timeout']);
    update_option('cp_score_points', (int)$_POST['cp_score_points']);
    update_option('cp_highscore_points', (int)$_POST['cp_highscore_points']);
    //update_option('cp_challange_points', (int)$_POST['cp_challange_points']);    
  }
}


/**
 * Functions that handles the points
 */
function myarcade_cp_play_game() {
  if( function_exists('cp_points') && is_user_logged_in() ) {
    $points = get_option('cp_play_points');
    cp_points('Played a game', cp_currentUser(), $points, '');
  }
}

function myarcade_cp_submit_score() {
  if( function_exists('cp_points') && is_user_logged_in() ) {
    $points = get_option('cp_score_points');
    cp_points('Score Submit', cp_currentUser(), $points, '');
  }
}

function myarcade_cp_new_highscore() {
  if( function_exists('cp_points') && is_user_logged_in() ) {
    $points = get_option('cp_highscore_points');
    cp_points('New Highscore', cp_currentUser(), $points, '');
  }
}

/*function myarcade_cp_challange_won() {
  if( function_exists('cp_points') && is_user_logged_in() ) {
    $points = get_option('cp_challange_points');
    cp_points('Won Challange', cp_currentUser(), $points, '');
  }
}*/


/**
 *******************************************************************************
 *   C U B E P O I N T S  H O O K S
 *******************************************************************************
 */
add_action('cp_config_process', 'myarcade_cp_save_settings');
add_action('cp_config_form_points', 'myarcade_cp_settings');

add_action('myarcade_update_play_points', 'myarcade_cp_play_game');
add_action('myarcade_new_score', 'myarcade_cp_submit_score');
add_action('myarcade_new_highscore', 'myarcade_cp_new_highscore');
//add_action('myarcade_challange_won', 'myarcade_cp_challange_won');