<?php

if ( ! class_exists( 'Survey_Post_Type' ) ) {
	class Survey_Post_Type {
		public function __construct() {
			add_action( 'init', array( $this, 'survey_post_type' ) );
			add_action( 'wp_enqueue_scripts', array( $this, 'survey_enqueue_scripts' ), 9999 );
			add_action( 'admin_init', array( $this, 'add_survey_meta_boxes' ) );
			add_action( 'save_post', array( $this, 'save_survey_meta_boxes' ) );
			add_filter( 'single_template', array( $this, 'survey_template' ) );
		}
		public static function get_post_type_slug () {
			return 'survey';
		}
		public static function get_post_type_args () {
			return array(
				'labels' => array(
					'name' => __('Survey Pages', 'bw_survey'),
					'singular_name' => __('Survey Page', 'bw_survey'),
					'add_new_item' => __('Add New Survey Page', 'bw_survey'),
					'edit_item' => __('Edit Survey Page', 'bw_survey'),
					'new_item' => __('New Survey Page', 'bw_survey'),
					'view_item' => __('View Survey Page', 'bw_survey'),
					'view_items' => __('View Survey Pages', 'bw_survey'),
					'search_items' => __('Search Survey Pages', 'bw_survey'),
					'not_found' => __('No survey pages found.', 'bw_survey'),
					'not_found_in_trash' => __('No survey pages found in trash.', 'bw_survey'),
					'all_items' => __('All Survey Pages', 'bw_survey'),
					'archives' => __('Survey Page Archives', 'bw_survey'),
					'insert_into_item' => __('Insert into Survey Page', 'bw_survey'),
					'uploaded_to_this_item' => __('Uploaded to this Survey Page', 'bw_survey'),
					'filter_items_list' => __('Filter Survey Pages list', 'bw_survey'),
					'items_list_navigation' => __('Survey Pages list navigation', 'bw_survey'),
					'items_list' => __('Survey Pages list', 'bw_survey'),
					'item_published' => __('Survey Page published.', 'bw_survey'),
					'item_published_privately' => __('Survey Page published privately.', 'bw_survey'),
					'item_reverted_to_draft' => __('Survey Page reverted to draft.', 'bw_survey'),
					'item_scheduled' => __('Survey Page scheduled.', 'bw_survey'),
					'item_updated' => __('Survey Page updated.', 'bw_survey')
				),
				'menu_icon' => 'dashicons-feedback',
				'public' => true,
				'exclude_from_search' => true,
				'supports' => array('title', 'thumbnail', 'revisions')
			);
		}
		public function survey_post_type () {
			register_post_type( self::get_post_type_slug(), self::get_post_type_args() );
		}

		public function survey_enqueue_scripts () {
			if ( is_single() && get_post_type() === 'survey' ) {
				global  $wp_styles;

				foreach($wp_styles->registered as $registered)
					if(strpos($registered->src,'/wp-content/')!==FALSE)
						wp_dequeue_style($registered->handle);

				wp_enqueue_style('bw-survey-component-survey');
				wp_enqueue_script('bw-survey-component-survey');
			}
		}

		public function add_survey_meta_boxes () {
			add_meta_box(
				'survey_meta_box',
				'Survey',
				array($this, 'survey_meta_box'),
				'survey',
				'normal',
				'core'
			);
		}

		public function save_survey_meta_boxes () {
			global $post;
			if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || !isset($_POST['_survey']) ) {
				return;
			}
			update_post_meta( $post->ID, "_survey", sanitize_text_field( $_POST['_survey'] ) );
		}

		public function survey_meta_box(){
			global $post;
			global $wpdb;
			$custom = get_post_custom( $post->ID );
			$survey_id = isset($custom[ "_survey" ][ 0 ]) ? $custom[ "_survey" ][ 0 ] : false;

			// get surveys

			$results = $wpdb->get_results("SELECT id, title FROM {$wpdb->prefix}bwsurvey_surveys");
			if ($wpdb->last_error || empty($results)) {
				return new WP_Error('bw_survey_no_survey', __('Invalid survey'), array('status' => 404));
			}
			// echo survey select
			?>
				<select name="_survey">
					<option value="" disabled <?php if (empty($survey_id)) echo 'selected'; ?>>Select your survey</option>
					<?php foreach ($results as $survey) : ?>
						<option value="<?php echo $survey->id; ?>" <?php if ($survey_id == $survey->id) echo 'selected'; ?>><?php echo $survey->title; ?></option>
					<?php endforeach; ?>
				<select>
			<?php

		}

		public function survey_template ($template) {
			global $post;
			if ( 'survey' === $post->post_type && locate_template( array( 'single-survey.php' ) ) !== $template ) {
				return plugin_dir_path( BW_SURVEY_PLUGIN_FILE ) . 'single-survey.php';
			}
			return $template;
		}
	}
}
