<?php

class FLCDash{
	private $source;

	public function __construct( $source ){
		global $wpdb;

		$this->source = $source;
		$this->wpdb = $wpdb;
		$this->error = array();



		$links_slug = "{$this->source->plugin_namespace}links";
		$parent_slug = "{$this->source->plugin_namespace}home";
		$settings_slug = "{$this->source->plugin_namespace}settings";

		if(isset($_POST['settings'])){
			$attrib = $_POST['attrib'];
			$social = $_POST['social'];
			$bg_color = $_POST['bg_color'];
			$link_color = $_POST['link_color'];
			$section_color = $_POST['section_color'];
			$this->source->add_option( 'social', $social );
			$this->source->add_option( 'attrib', $attrib );
			$this->source->add_option( 'bg_color', $bg_color );
			$this->source->add_option( 'link_color', $link_color);
			$this->source->add_option( 'section_color', $section_color);
		}
		if(
			isset($_POST['action']) && 
			($_POST['action'] == 'delete') && 
			($_REQUEST['page'] == $links_slug)
		){
			$ids = $_POST['link_ids'];
			if(count($ids)){
				foreach($ids as $id){
					$this->source->db->delete(
							$this->source->plugin_tables['links'],
							array('id' => $id,)
					);
				}
			}
		}
		if(
			isset($_POST['action2']) && 
			($_POST['action2'] == 'delete') && 
			($_REQUEST['page'] == $links_slug)
		){
			$ids = $_POST['link_ids'];
			if(count($ids)){
				foreach($ids as $id){
					$this->source->db->delete(
							$this->source->plugin_tables['links'],
							array('id' => $id,)
					);
				}
			}
		}
		if(isset($_REQUEST['delete']) && ($_REQUEST['source'] == 'links')){
			$id = (int)$_GET['delete'];
			$this->source->db->delete(
					$this->source->plugin_tables['links'],
					array('id' => $id,)
			);
			wp_redirect( 
					add_query_arg( array( 
								'delete' => NULL, 
								'source' => NULL
							)
					) 
			);
			exit;
		}
		if(
			isset($_POST['action']) && 
			($_POST['action'] == 'delete') && 
			($_REQUEST['page'] == $parent_slug)
		){
			$ids = $_POST['section_ids'];
			if(count($ids)){
				foreach($ids as $id){
					$this->source->db->delete(
							$this->source->plugin_tables['sections'],
							array('id' => $id,)
					);
					$this->source->db->delete(
							$this->source->plugin_tables['links'],
							array('section_id' => $id, )
					);
				}
			}
		}
		if(
			isset($_POST['action2']) && 
			($_POST['action2'] == 'delete') && 
			($_REQUEST['page'] == $parent_slug)
		){
			$ids = $_POST['section_ids'];
			if(count($ids)){
				foreach($ids as $id){
					$this->source->db->delete(
							$this->source->plugin_tables['sections'],
							array('id' => $id,)
					);
					$this->source->db->delete(
							$this->source->plugin_tables['links'],
							array('section_id' => $id, )
					);
				}
			}
		}
		if(isset($_REQUEST['delete']) && ($_REQUEST['source'] == 'section')){
			$id = (int)$_GET['delete'];
			$this->source->db->delete(
					$this->source->plugin_tables['sections'],
					array('id' => $id,)
			);

			$this->source->db->delete(
					$this->source->plugin_tables['links'],
					array('section_id' => $id, )
			);
			wp_redirect( 
					add_query_arg( array( 
								'delete' => NULL, 
								'source' => NULL
							)
					) 
			);
			exit;
		}
		if(isset($_POST['edit_link'])){
			$url = trim($_POST['url']);
			$index = trim($_POST['index']);
			$anchor = trim($_POST['anchor']);
			$target = trim($_POST['target']);
			$follow = trim($_POST['follow']);
			$section = trim($_POST['section']);

			$anchor = strlen($anchor) ? $anchor : $url;

			$id = (int)$_GET['edit'];
			if(!filter_var($url, FILTER_VALIDATE_URL)){
				$this->error['error'] = "Invalid url";
			}
			if(!strlen($section) or ((int)$section < 1)){
				$this->error['error'] = "No section added";
			}
			if(!count($this->error)){
				if($this->source->db->update(
					$this->source->plugin_tables['links'],
					array(
						'url' => $url,
						'anchor' => $anchor,
						'target' => $target,
						'do_follow' => $follow,
						'section_id' => $section,
						'do_index' => $index,
						'created' => time()
					),
					array( 'id' => $id )
				)){
					wp_redirect( add_query_arg( array( 'action' => NULL ) ) );
					exit;
				} else{
					$this->error['error'] = "Something went wrong";
				}
			}
		}
		if(isset($_POST['new_link'])){
			$url = trim($_POST['url']);
			$index = trim($_POST['index']);
			$anchor = trim($_POST['anchor']);
			$target = trim($_POST['target']);
			$follow = trim($_POST['follow']);
			$section = trim($_POST['section']);

			$anchor = strlen($anchor) ? $anchor : $url;

			if(!filter_var($url, FILTER_VALIDATE_URL)){
				$this->error['error'] = "Invalid url";
			}
			if(!strlen($section) or ((int)$section < 1 )){
				$this->error['error'] = "No section added";
			}
			if(!count($this->error)){
				if($this->source->db->insert(
					$this->source->plugin_tables['links'],
					array(
						'url' => $url,
						'anchor' => $anchor,
						'target' => $target,
						'do_follow' => $follow,
						'section_id' => $section,
						'do_index' => $index,
						'created' => time()
					)
				)){
					wp_redirect( add_query_arg( array( 'action' => NULL, 'url' => NULL, 'anchor' => NULL ) ) );
					exit;
				} else{
					$this->error['error'] = "Something went wrong";
				}
			}
		}
		if(isset($_POST['edit_section'])){
			$name = trim($_POST['name']);
			$title = trim($_POST['title']);

			$name = strlen($name) ? $name : $title;

			if(!strlen($name)){
				$this->error['error'] = "No title";
			}
			$id = (int)$_GET['edit'];
			if(!count($this->error)){
				if($this->source->db->update(
					$this->source->plugin_tables['sections'],
					array(
						'name' => $name,
						'title' => $title,
						'created' => time()
					),
					array( 'id' => $id )
				)){
					wp_redirect( add_query_arg( array( 'action' => NULL ) ) );
					exit;
				} else{
					$this->error['error'] = "Something went wrong";
				}
			}
		}
		if(isset($_POST['new_section'])){
			$name = trim($_POST['name']);
			$title = trim($_POST['title']);

			$name = strlen($name) ? $name : $title;

			if(!strlen($name)){
				$this->error['error'] = "No title";
			}
			if(!count($this->error)){
				if($this->source->db->insert(
					$this->source->plugin_tables['sections'],
					array(
						'name' => $name,
						'title' => $title,
						'created' => time()
					)
				)){
					wp_redirect( add_query_arg( array( 'action' => NULL ) ) );
					exit;
				} else{
					$this->error['error'] = "Something went wrong";
				}
			}
		}
		$capability = 10;
	  	$main_page = add_menu_page(
				$this->source->plugin_name,
				$this->source->plugin_name,
				$capability,
				$parent_slug,
				array( $this, 'print_sections' ), 
				"{$this->source->plugin_url}views/images/favicon.ico"
		);
		$sections_page = add_submenu_page(
				$parent_slug, 
				"Sections",
				"Sections",
				$capability,
				$parent_slug, 
				array( $this, 'print_sections' )
		);
		$links_page = add_submenu_page(
				$parent_slug,
				"Links",
				"Links",
				$capability,
				$links_slug, 
				array( $this, 'print_links' )
		);
		$settings_page = add_submenu_page( 
				$parent_slug, 
				'Settings',
				'Settings',
				$capability,
				$settings_slug, 
				array( $this, 'print_settings' )
		);
		add_action( 'admin_init',  array( $this, 'admin_init' ) );
	        add_action( "admin_print_styles-$main_page",  array( $this, 'styles' ));
	        add_action( "admin_print_styles-$links_page",  array( $this, 'styles' ));
	        add_action( "admin_print_styles-$sections_page",  array( $this, 'styles' ));
	        add_action( "admin_print_styles-$settings_page",  array( $this, 'styles' ));
	        add_action( "admin_print_scripts-$settings_page",  array( $this, 'scripts' ));
	}
	public function admin_init(){
		wp_register_style( 
			"{$this->source->plugin_namespace}-css",
			"{$this->source->plugin_url}views/css/".
			"{$this->source->plugin_namespace}.css"
		);
		wp_register_style(
			"{$this->source->plugin_namespace}bootstrap-css",
			"{$this->source->plugin_url}views/css/bootstrap.min.css"
		);
		wp_register_style(
			"{$this->source->plugin_namespace}colorpicker-css",
			"{$this->source->plugin_url}views/css/evol.colorpicker.css"
		);
		wp_register_style(
			"{$this->source->plugin_namespace}jquery-ui-css",
			"{$this->source->plugin_url}views/css/jquery-ui.css"
		);
		wp_register_script(
			"{$this->source->plugin_namespace}jquery-ui-js",
			"{$this->source->plugin_url}views/js/".
			"jquery-ui.js" 
		);
		wp_register_script(
			"{$this->source->plugin_namespace}colorpicker-js",
			"{$this->source->plugin_url}views/js/".
			"evol.colorpicker.min.js" 
		);
	}
	public function styles(){
		wp_enqueue_style( "{$this->source->plugin_namespace}-css" );
		wp_enqueue_style( "{$this->source->plugin_namespace}jquery-ui-css" );
		wp_enqueue_style( "{$this->source->plugin_namespace}bootstrap-css" );
		wp_enqueue_style( "{$this->source->plugin_namespace}colorpicker-css" );
	}
	public function scripts(){ 
		wp_enqueue_script( 'jquery' );
		wp_enqueue_script( "{$this->source->plugin_namespace}jquery-ui-js" );
		wp_enqueue_script( "{$this->source->plugin_namespace}colorpicker-js" );
	}
	public function print_sections(){
		$section = $_REQUEST['action'];
		switch($section){
			case 'add':
				include("{$this->source->plugin_dir}views/add-section.php");
				break;
			case 'edit':
				include("{$this->source->plugin_dir}views/edit-section.php");
				break;
			default:
				include("{$this->source->plugin_dir}views/sections.php");
				break;
		}
	}
	public function print_links(){
		$section = $_REQUEST['action'];
		switch($section){
			case 'add':
				include("{$this->source->plugin_dir}views/add-link.php");
				break;
			case 'edit':
				include("{$this->source->plugin_dir}views/edit-link.php");
				break;
			default:
				include("{$this->source->plugin_dir}views/links.php");
				break;
		}
	}
	public function print_settings(){
		include("{$this->source->plugin_dir}views/settings.php");
	}
}
