<?php
namespace ONLINE_ADAv4;

if ( !defined( 'ABSPATH' ) ) exit;

if ( !class_exists('WCAG_Scan') ) {

    class WCAG_Scan {
        public $ID = null; // post id
        public $status = null; // pending, in-progress, complete
		public $current_step = null; // See: get_step_keys() in core-functions.php
		public $filter_step = null;

        public function __construct( $scan_id = null ){
            if( !$scan_id ) return false;

            $scan = get_post( $scan_id );

            if ( empty($scan) || is_wp_error($scan) ) return false; // Scan does not exist.
			if ( $scan->post_type !== "wcag_scan" ) return false; // ID is not for a scan.
			if ( $scan->post_status == 'trash' ) return false; // Scan is in the trash.

			// Scan exists, prepare data
			$this->ID = (int)$scan->ID;
			$this->post = $scan;

            $this->current_step = get_post_meta( $this->ID, '_oadaas_scan-step', true );

            if ( !$this->current_step ) {
				$step_keys = get_step_keys();
				$this->current_step = $step_keys[0];
			}

            $this->status = get_post_meta( $this->ID, '_scan-status', true );
			if ( !$this->status ) {
				$this->status = 'pending';
			}

			$this->filter_step = get_post_meta($this->ID, "_filter_step", true);
			if( empty($this->filter_step) ){
				$this->filter_step = 0;
			}

            return $this;
        }

        /**
		 * Returns true if the scan's progress/completion notice has been dismissed.
		 * Note: Scans can be dismissed before they have completed. On completion, they will be displayed again, and can be dismissed a second time.
		 *
		 * @return bool
		 */
		public function is_dismissed() {
			$dismissed = get_post_meta( $this->ID, '_oadaas_scan-dismissed', true );
			return !empty( $dismissed );
		}

		/**
		 * Gets the URL for this scan to dismiss the notification.
		 */
		public function get_dismiss_url() {
			return add_query_arg( array( 'wcag_action' => 'online_ada_dismiss_scan', 'wcag_id' => $this->ID ) );
		}

        /**
		 * Starts the scan. Only one scan can be activated at a time. If a scan is already running, returns false.
		 */
		public function start_the_scan() {
			$current_scan = get_active_scan();

			// If a scan is already running, return false.
			if ( $current_scan ) {
				if ( $current_scan->ID == $this->ID ) {
					// This is the current scan, give an error if it has already been started.
					if ( $this->status != "pending" ) {
						return new \WP_Error( "audit_already_started", "This audit has already been started. You must start a new audit." );
					}
				}else{
					// A different scan is already the current one. If the other scan is completed or cancelled, or hasn't started yet, continue.
					if ( $current_scan->status == 'in-progress' ) {
						return new \WP_Error( "another_audit_started", "A different audit is currently in progress. You cannot run multiple audits at the same time." );
					}
				}

				if ( get_post_status( $current_scan->ID ) != "publish" ) {
					return new \WP_Error( "not_published", "The audit must be published in order to start it. Note that audits will not be visible to the public." );
				}
			}

			// Set this scan as in progress, and make it the current scan
			update_post_meta( $this->ID, '_scan-started', 1 );
			update_post_meta( $this->ID, '_oadaas_scan-dismissed', 0 );
			update_post_meta( $this->ID, '_scan-completed', 0 );
			update_post_meta( $this->ID, "_filter_step", 0 );
			update_post_meta( $this->ID, "_oadaas_cbr_complete", 0);
			update_post_meta( $this->ID, '_scan-status', 'in-progress' );

			update_option( 'ada_plugin_current_scan', $this->ID, 'yes' );

			//wp_schedule_single_event( _wcag_schedule_interval(), 'ada_plugin_process_scheduled_scan', array( $this->ID ) );

			do_action( 'ada_plugin-scan-started', $this );

			return true;
		}

        /**
		 * Processes the scan until it is completed.
		 */
		public function process() {
			if ( $this->status != 'in-progress' ) return false;

			update_post_meta( $this->ID, '_oadaas_scan-step', $this->current_step );
			return true;
		}

		/**
		 * Triggered when the scan has completed. Should not be called manually.
		 */
		public function complete_the_scan()
		{
			// Mark the scan as complete.
			update_post_meta($this->ID, '_scan-status', 'complete');

			// Save the date of completion
			update_post_meta($this->ID, '_oadaas_scan-completion-date', time());

			// Show the scan progress message once again, if it was already dismissed.
			delete_post_meta($this->ID, '_oadaas_scan-dismissed', 0);
			do_action(__NAMESPACE__ . '\\scan-completed', $this);
		}

		public function step_complete()
		{
			$step_keys = get_step_keys();
			$step = $this->current_step;
			$step_index = array_search($step, $step_keys);

			if ($step_index === false) {
				return "Error: Invalid step key: " . $step . ".";
			} else {
				//do_action('wcag_scan_step_complete-' . $step, $this);
				//do_action('wcag_scan_step_complete', $this);

				$next_index = $step_index + 1;

				if (isset($step_keys[$next_index])) {
					// Progress to next step
					$next_step = $step_keys[$next_index];
					$this->current_step = $next_step;
					update_post_meta($this->ID, '_oadaas_scan-step', $next_step);
				} else {
					// All steps complete!
					$this->complete_the_scan();
				}
			}

		}

    }
}
