<?php
if (!class_exists('Ihc_My_Cred') && class_exists('myCRED_Hook')):
class Ihc_My_Cred extends myCRED_Hook{

	/*
	 * @param array
	 * @return none
	 */
	public function __construct($hook_prefs=array(), $type='mycred_default'){
		$levels = get_option('ihc_levels');
		if ($levels){
			foreach ($levels as $lid=>$temp_array){
				$defaults['acquire_level_' . $lid] = array(
							'creds' => 0,
							'log'   => '%plural% for acquire level ' . $temp_array['label'],
							'run_everytime' => 0,				
				);
			}
			if (!empty($defaults)){
				parent::__construct(array(
					'id' => 'ihc_mycred',
					'defaults' => $defaults,		
				), $hook_prefs, $type);							
			}
		}
	}	
	 
	 
	/*
	 * @param none
	 * @return none
	 */
	public function run(){
		add_action('ihc_action_after_subscription_activated', array($this, 'give_points_for_level_acquire'), 1, 3);	
	}
	
	
	/*
	 * @param int, int
	 * @return none
	 */
	public function give_points_for_level_acquire($uid=0, $lid=-1, $first_time=TRUE){
		if ($uid && $lid>-1){
			if ($this->core->exclude_user($uid)){
				return;
			}
			$key = 'acquire_level_' . $lid;
			
			/// run just once ??
			if (!$first_time && !$this->prefs[$key]['run_everytime']){
				return;
			}
			
			if ($this->prefs[$key]['creds']>0){
				$this->core->add_creds(
					$key,
					$uid,
					$this->prefs[$key]['creds'],
					$this->prefs[$key]['log'],
					$lid,
					array('ref_type'=>'post'),
					$this->mycred_type
				);							
			}
		}	
	}
	
	
	/*
	 * @param array
	 * @return array
	 */
	public function add_references($references=array()){
		$levels = get_option('ihc_levels');
		if ($levels){
			foreach ($levels as $lid=>$temp_array){
				$references['acquire_level_' . $lid] = __('Acquire Level', 'ihc');
			}
		}		
		return $references;
	}
	
	
	/*
	 * Print settings form
	 * @param none
	 * @return none
	 */
	public function preferences(){
		$levels = get_option('ihc_levels');
		if ($levels):
			foreach ($levels as $lid=>$temp_array):
				$key = 'acquire_level_' . $lid;
				?>
				<div style="margin-bottom: 25px; border-bottom: 1px dotted #c0c0c0;">
					<h2><?php echo "Level '" .  $temp_array['label'] . "' ";?></h2>
					<label class="subheader" for="<?php echo $this->field_id(array($key=>'creds'));?>"><?php echo __('Points', 'ihc');?></label>
					<ol>
						<li>
							<div class="h2">
								<input type="number" min="0" name="<?php echo $this->field_name(array($key=>'creds'));?>" id="<?php echo $this->field_id(array($key=>'creds')); ?>" value="<?php echo esc_attr($this->core->number($this->prefs[$key]['creds']));?>" />
							</div>
						</li>
					</ol>
					<label class="subheader" for="<?php echo $this->field_id(array($key=>'log'));?>"><?php echo __('Log Template', 'ihc');?></label>
					<ol>
						<li>
							<div class="h2">
								<input type="text" name="<?php echo $this->field_name(array($key=>'log'));?>" id="<?php echo $this->field_id(array($key=>'log')); ?>" value="<?php echo esc_attr($this->prefs[$key]['log']);?>" class="long" placeholder="%plural% for <?php echo $temp_array['label']; ?> purchased" />
							</div>
						</li>
					</ol>
			
					<label class="subheader" for="<?php echo $this->field_id(array($key=>'custom_option'));?>"><?php echo __('Reward', 'ihc');?></label>
					<ol>
						<li>
							<div class="h2">
								<select name="<?php echo $this->field_name(array($key=>'run_everytime'));?>" id="<?php echo $this->field_id(array($key=>'run_everytime')); ?>" class="long" >
									<?php $selected = ($this->prefs[$key]['run_everytime']==0) ? 'selected' : '';?>
									<option <?php echo $selected;?> value="0" ><?php _e('Just Once', 'ihc');?></option>
									<?php $selected = ($this->prefs[$key]['run_everytime']==1) ? 'selected' : '';?>
									<option <?php echo $selected;?> value="1" ><?php _e('Everytime', 'ihc');?></option>									
								</select>
							</div>
						</li>
					</ol>
					<span class="description"><?php echo __('Available only for Recurring Subscriptions', 'ihc');?></span>															
				</div>
					
				<?php
			endforeach;
		endif;
	}
	
}
endif;