<?php

class WP_Superlike_Admin_Panel {

	var $options;

	/* constructor for admin panel */
	function __construct() {
		$this->slug = 'wp-superlike';
		$this->tabs = array( 'general' => __('General','wp_superlike'), 'appearance' => __('Appearance','wp_superlike'), 'theming' => __('Theming','wp_superlike') );
		$this->default_tab = 'general';
		add_action('admin_menu', array(&$this, 'add_menu'), 9);
		$this->defaults = array(
			'add_to_content' => 0,
			'multiple_vote' => 'ip_check',
			'cannot_like_method' => 'disable_button',
			'disabled_opacity' => '0.70',
			'you_already_liked' => __('You have already liked this post.','wp_superlike'),
			'already_liked_text' => __('You liked!','wp_superlike'),
			'rtl' => 0,
			'like_text' => __('Like!','wp_superlike'),
			'icon_only' => 0,
			'number_format' => 0,
			'add_to_content_after' => 'after',
			'button_size' => 'normal',
			'button_layout' => 'standard',
			'button_style' => 'builtin',
			'style' => 'default',
			'button_bg_color' => '#f5f5f5',
			'button_bg_color2' => '#f5f5f5',
			'button_bg_color_hover' => '#f5f5f5',
			'button_bg_color_hover2' => '#f5f5f5',
			'button_text_color' => '#555',
			'button_border_color' => '#ddd',
			'button_border_color_hover' => '#bbb',
			'count_bg_color' => '#fff',
			'count_text_color' => '#555',
			'count_border_color' => '#ddd'
		);
		$this->colorsdefault = array(
			'button_bg_color' => '#f5f5f5',
			'button_bg_color2' => '#f5f5f5',
			'button_bg_color_hover' => '#f5f5f5',
			'button_bg_color_hover2' => '#f5f5f5',
			'button_text_color' => '#555',
			'button_border_color' => '#ddd',
			'button_border_color_hover' => '#bbb',
			'count_bg_color' => '#fff',
			'count_text_color' => '#555',
			'count_border_color' => '#ddd'
		);
		$this->options = get_option('wp_superlike_settings');
		if (!get_option('wp_superlike_settings')) {
			update_option('wp_superlike_settings', $this->defaults);
		}
	}
	
	// add menu
	function add_menu() {
		add_submenu_page( 'options-general.php', __('WP SuperLike!','wp_superlike'), __('WP SuperLike!','wp_superlike'), 'manage_options', $this->slug, array(&$this, 'settings_page'), null);
	}
	
	// get value in admin option
	function get_value($option_id) {
		if (isset($this->options[$option_id]) && $this->options[$option_id] != '' ) {
			return $this->options[$option_id];
		} elseif (isset($this->defaults[$option_id]) && $this->defaults[$option_id] != '' ) {
			return $this->defaults[$option_id];
		} else {
			return null;
		}
	}
	
	// add setting field
	function add_plugin_setting($type, $id, $label, $pairs, $help, $extra=null) {
		
		print "<tr valign=\"top\">
				<th scope=\"row\"><label for=\"$id\">$label</label></th>
				<td>";
				
				switch ($type) {
				
					case 'input':
						$value = $this->get_value($id);
						print "<input name=\"$id\" type=\"text\" id=\"$id\" value=\"$value\" class=\"regular-text\" />";
						break;
				
					case 'select':
						print "<select name=\"$id\" id=\"$id\">";
							foreach($pairs as $k => $v) {
						
								if (is_array($v)) {
									$v = $v['name'];
								}
								
								echo '<option value="'.$k.'"';
								if (isset($this->options[$id]) && $k == $this->options[$id]) {
									echo ' selected="selected"';
								}
								echo '>';
								echo $v;
								echo '</option>';
								
							}
						print "</select>";
						break;
						
					case 'color':
						$value = $this->get_value($id);
						$default_color = $this->defaults[$id];
						print "<input name=\"$id\" type=\"text\" id=\"$id\" value=\"$value\" class=\"my-color-field\" data-default-color=\"$default_color\" />";
						break;
						
				}
		
		if ($help)
			print "<p class=\"description\">$help</p>";
		
		if (is_array($extra)) {
			echo "<div class=\"helper-wrap\">";
			foreach ($extra as $a) {
				echo $a;
			}
			echo "</div>";
		}
					
		print "</td></tr>";
		
	}
	
	// save form
	function saveform() {
		foreach($_POST as $key => $value) {
			if ($key != 'submit') {
				$this->options[$key] = esc_attr($_POST[$key]);
			}
		}
	}
	
	// save default colors
	function save_default_colors() {
		$alloptions = get_option('wp_superlike_settings');
		foreach( $this->colorsdefault as $k =>$v) {
			$alloptions[$k] = $v;
			$this->options[$k] = $v;
		}
	}
	
	// update settings
	function update() {
		update_option('wp_superlike_settings', $this->options);
		echo '<div class="updated"><p><strong>'.__('Settings saved.','wp_superlike').'</strong></p></div>';
	}
	
	// reset settings
	function reset() {
		update_option('wp_superlike_settings', $this->defaults );
		$this->options = array_merge( $this->options, $this->defaults );
		echo '<div class="updated"><p><strong>'.__('Settings are reset to default.','wp_superlike').'</strong></p></div>';
	}
	
	/* Get admin tabs */
	function admin_tabs( $current = null ) {
			$tabs = $this->tabs;
			$links = array();
			if ( isset ( $_GET['tab'] ) ) {
				$current = $_GET['tab'];
			} else {
				$current = $this->default_tab;
			}
			foreach( $tabs as $tab => $name ) :
				if ( $tab == $current ) :
					$links[] = "<a class='nav-tab nav-tab-active' href='?page=".$this->slug."&tab=$tab'>$name</a>";
				else :
					$links[] = "<a class='nav-tab' href='?page=".$this->slug."&tab=$tab'>$name</a>";
				endif;
			endforeach;
			foreach ( $links as $link )
				echo $link;
	}
	
	/* get tab ID and load its content */
	function get_tab_content() {
		$screen = get_current_screen();
		if( strstr($screen->id, $this->slug ) ) {
			if ( isset ( $_GET['tab'] ) ) {
				$tab = $_GET['tab'];
			} else {
				$tab = $this->default_tab;
			}
			$this->load_tab($tab);
		}
	}
	
	/* load tab */
	function load_tab($tab) {
		require_once PLUGIN_WP_SUPERLIKE_PATH.'admin/'.$tab.'.php';
	}

	// add settings
	function settings_page() {
	
		/**
		* @submit settings page
		*/
		if (isset($_POST['submit'])) {
			$this->saveform();
			$this->update();
		}
		
		/**
		* @submit theme reset button
		*/
		if (isset($_POST['reset-custom-theme'])) {
			$this->saveform();
			$this->save_default_colors();
			$this->update();
		}
		
		/**
		* @callback to restore all options
		*/
		if (isset($_POST['reset-options'])) {
			$this->reset();
		}
		
	?>
	
		<div class="wrap">
			<div id="icon-<?php echo $this->slug; ?>" class="icon32"><br /></div><h2 class="nav-tab-wrapper"><?php $this->admin_tabs(); ?></h2>
			<form method="post" action="">
				<table class="form-table">
					<?php $this->get_tab_content(); ?>
				</table>
				<p class="submit">
					<input type="submit" name="submit" id="submit" value="<?php _e('Save Changes','wp_superlike'); ?>" class="button button-primary" />
					<input type="submit" name="reset-options" value="<?php _e('Reset Options','wp_superlike'); ?>" class="button button-secondary" />
				</p>
			</form>
		</div>

	<?php }

}

$WP_Superlike_Admin_Panel = new WP_Superlike_Admin_Panel();