<?php

/**
 * GiveASAP Widget to Enable Entering
 */

use Simple_Giveaways\GA_Schedule;

if ( ! defined( 'ABSPATH' ) ) {
	return;
}

/**
 * Adds Foo_Widget widget.
 */
class GiveASAP_Widget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'giveasap_widget', // Base ID
			esc_html__( 'Simple Giveaway', 'giveasap' ), // Name
			array( 'description' => esc_html__( 'Display a form for people to enter the giveaway', 'giveasap' ) ) // Args
		);
	}

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {
		if ( empty( $instance['giveaway'] ) || 0 === absint( $instance['giveaway'] ) ) {
			return;
		}

		$post = get_post( $instance['giveaway'] );

		if ( 'publish' !== $post->post_status ) {
			return;
		}

		echo $args['before_widget'];

		if ( ! empty( $instance['title'] ) ) {
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
		}

		$giveasap_front    = new GiveASAP_Front( $post );
		$schedule          = get_post_meta( $post->ID, 'giveasap_schedule', true );
		$ga_schedule       = new GA_Schedule( $schedule, $post );
		$timezone          = get_option( 'gmt_offset' );
		$giveasap_settings = giveasap_get_settings();
		if ( $giveasap_front->get_step() == 1 ) {

			if ( $giveasap_settings['google_site_key'] ) { ?>
				<script src='https://www.google.com/recaptcha/api.js'></script>
				<?php
			}
			if ( ! empty( $instance['description'] ) ) {
				echo '<p>' . esc_html( apply_filters( 'giveasap_widget_description', $instance['description'] ) ) . '</p>';
			}
			?>
			<div data-timezone="<?php echo esc_attr( $timezone ); ?>" data-end="<?php echo esc_attr( giveasap_get_end_time( $post->ID ) ); ?>" class="giveasap_countdown">

			</div>

			<?php
			giveasap_the_form( $giveasap_front, $giveasap_settings );

		} else {
			// Visitor has registered already
			// First, load the scripts if not loaded already.
			?>
			<div class="giveasap-box sg-shortcode">
				<h3><?php esc_html_e( 'You are participating in this giveaway', 'giveasap' ); ?></h3>
			<?php
			giveasap_the_form( $giveasap_front, $giveasap_settings );

			?>
			</div>
		<?php } ?>
		<a href="<?php echo esc_url( $giveasap_front->get_form_link() ); ?>"><?php esc_html_e( 'Read more about it here.', 'giveasap' ); ?></a>
		<?php
		echo $args['after_widget'];
	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {
		$selected_giveaway = isset( $instance['giveaway'] ) ? $instance['giveaway'] : 0;
		$title             = isset( $instance['title'] ) ? $instance['title'] : '';
		$description       = isset( $instance['description'] ) ? $instance['description'] : '';
		$giveaways         = get_posts(
			array(
				'post_type'      => 'giveasap',
				'post_status'    => 'publish',
				'posts_per_page' => -1,
			)
		);

		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'giveasap' ); ?></label> 
			<input class="widefat" type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" value="<?php echo esc_attr( $title ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>"><?php esc_attr_e( 'Description:', 'giveasap' ); ?></label> 
			<textarea class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'description' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>"><?php echo esc_attr( $description ); ?></textarea>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'giveaway' ) ); ?>"><?php esc_attr_e( 'Giveaway:', 'giveasap' ); ?></label> 
			<?php if ( ! empty( $giveaways ) ) { ?>
			<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'giveaway' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'giveaway' ) ); ?>">
				<option value="0" <?php selected( $selected_giveaway, 0, true ); ?>><?php esc_html_e( 'Select a Giveaway', 'giveasap' ); ?></option>
				<?php

				foreach ( $giveaways as $giveaway ) {
					echo '<option ' . selected( $selected_giveaway, $giveaway->ID, false ) . ' value="' . esc_attr( $giveaway->ID ) . '">' . esc_html( $giveaway->post_title ) . '</option>';
				}

				?>
			</select>
				<?php
			} else {
				echo '<br/>';
				echo '<em>' . esc_html__( 'There is no Giveaway currently running', 'giveasap' ) . '</em>';
			}
			?>
		</p>
		<?php
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update( $new_instance, $old_instance ) {
		$instance                = array();
		$instance['giveaway']    = ( ! empty( $new_instance['giveaway'] ) ) ? wp_strip_all_tags( $new_instance['giveaway'] ) : '';
		$instance['title']       = ( ! empty( $new_instance['title'] ) ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
		$instance['description'] = ( ! empty( $new_instance['description'] ) ) ? wp_strip_all_tags( $new_instance['description'] ) : '';

		return $instance;
	}

}
