<?php
/**
 * MainWP BetterUptime Settings
 *
 * Handles BetterUptime settings.
 *
 * @package MainWP/Extensions/AUM
 */

namespace MainWP\Extensions\AUM;

/**
 * Class class MainWP_AUM_BetterUptime_Settings_Handle
 *
 * BetterUptime settings handle
 */
class MainWP_AUM_BetterUptime_Settings_Handle {

	/**
	 * Private static instance.
	 *
	 * @static
	 * @var $instance  MainWP_AUM_BetterUptime_Settings_Handle.
	 */
	private static $instance = null;

	/**
	 * Method instance()
	 *
	 * Get instance.
	 *
	 * @return object Instance.
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Method: handle_post_saving()
	 *
	 * Handles post saving.
	 *
	 * @param array $data Post data.
	 *
	 * @return bool true|false.
	 */
	public function handle_post_saving( $data ) {

		$options = array(
			'api_token' => isset( $data['api_token'] ) ? sanitize_text_field( $data['api_token'] ) : '',
		);
		MainWP_AUM_BetterUptime_API::instance()->update_options( $options );

		$this->reload_monitor_urls();

		return true;
	}

	/**
	 * Method: get_api_monitors()
	 *
	 * Gets Monitors.
	 *
	 * @return array $results Monitors data.
	 */
	public function get_api_monitors() {

		$result = MainWP_AUM_BetterUptime_API::instance()->get_all_monitors();  // to reload urls.

		if ( ! empty( $result ) ) {
			$result = json_decode( $result, true );
		}

		if ( is_array( $result ) && isset( $result['data'] ) ) {
			$result       = $result['data'];
			$monitor_urls = array();
			foreach ( $result as $mo ) {
				if ( 'monitor' == $mo['type'] ) {
					$monitor_urls[] = $mo;
				}
			}
			return $monitor_urls;
		} else {
			update_option( 'mainwp_aum_uptime_robot_message', __( 'Better Uptime monitor undefined error', 'advanced-uptime-monitor-extension' ) );
		}
		return false;
	}

	/**
	 * Method: reload_monitor_urls()
	 *
	 * Reloads monitors.
	 */
	public function reload_monitor_urls() {
		$count    = 0;
		$monitors = $this->get_api_monitors(); // saving options.

		if ( is_array( $monitors ) && ( count( $monitors ) > 0 ) ) {
			$current_urls        = MainWP_AUM_DB::instance()->get_monitor_urls( 'betteruptime' );
			$current_monitor_ids = array();
			if ( is_array( $current_urls ) && ( count( $current_urls ) > 0 ) ) {
				foreach ( $current_urls as $mo ) {
					if ( ! empty( $mo->url_address ) ) {
						$current_monitor_ids[] = $mo->monitor_id;
					}
				}
			}
			$results_uptime_monitor_ids = array();
			$this->save_api_monitors( $monitors ); // saving BetterUptime monitors.
			foreach ( $monitors as $mo ) {
				$results_uptime_monitor_ids[] = $mo['id'];
			}
			$diff_ids = array_diff( $current_monitor_ids, $results_uptime_monitor_ids );
			if ( is_array( $diff_ids ) && count( $diff_ids ) > 0 ) {
				foreach ( $diff_ids as $mo_id ) {
					// delete missing items.
					MainWP_AUM_DB::instance()->delete_db_monitor( 'monitor_id', $mo_id, 'betteruptime' );
				}
			}
		}
	}

	/**
	 * Method save_api_monitors()
	 *
	 * Saves moniotors.
	 *
	 * @param array $urls_monitor Monitors list.
	 *
	 * @return array $saved_ids Saved IDs.
	 */
	public function save_api_monitors( $urls_monitor ) {

		$saved_ids = array();
		foreach ( $urls_monitor as $monitor_url ) {

			if ( ! isset( $monitor_url['attributes'] ) ) {
				continue;
			}

			$current = MainWP_AUM_DB::instance()->get_monitor_by( 'url_address', $monitor_url['attributes']['url'], 'betteruptime' );
			$data    = array(
				'url_name'    => $monitor_url['attributes']['pronounceable_name'],
				'url_address' => $monitor_url['attributes']['url'],
				'monitor_id'  => $monitor_url['id'],
				'service'     => 'betteruptime',
			);

			if ( ! empty( $current ) ) {
				$just_insertupdate_id = MainWP_AUM_DB::instance()->update_monitor_url( $data, $current->url_id );
			} else {
				$just_insertupdate_id = MainWP_AUM_DB::instance()->insert_monitor_url( $data );
			}

			if ( ! empty( $just_insertupdate_id ) ) {
					$url_options = array(
						'monitor_type'        => $monitor_url['attributes']['monitor_type'],
						'required_keyword'    => $monitor_url['attributes']['required_keyword'],
						'monitor_interval'    => $monitor_url['attributes']['check_frequency'],
						'port'                => $monitor_url['attributes']['port'],
						'request_timeout'     => $monitor_url['attributes']['request_timeout'],
						'http_method'         => $monitor_url['attributes']['http_method'],
						'recovery_period'     => $monitor_url['attributes']['recovery_period'],
						'domain_expiration'   => $monitor_url['attributes']['domain_expiration'],
						'verify_ssl'          => $monitor_url['attributes']['verify_ssl'],
						'ssl_expiration'      => $monitor_url['attributes']['ssl_expiration'],
						'confirmation_period' => $monitor_url['attributes']['confirmation_period'],
						'request_body'        => $monitor_url['attributes']['request_body'],
						'request_headers'     => ! empty( $monitor_url['attributes']['request_headers'] ) ? json_encode( $monitor_url['attributes']['request_headers'] ) : '',
						'regions'             => ! empty( $monitor_url['attributes']['regions'] ) ? json_encode( $monitor_url['attributes']['regions'] ) : '',
						'follow_redirects'    => $monitor_url['attributes']['follow_redirects'],
						'call'                => $monitor_url['attributes']['call'],
						'sms'                 => $monitor_url['attributes']['sms'],
						'email'               => $monitor_url['attributes']['email'],
						'push'                => $monitor_url['attributes']['push'],
						'team_wait'           => $monitor_url['attributes']['team_wait'],
						'status'              => $monitor_url['attributes']['status'],
						'last_checked_at'     => $monitor_url['attributes']['last_checked_at'],
					);
					MainWP_AUM_DB::instance()->save_monitor_url_options( $just_insertupdate_id, $url_options );
					$saved_ids[] = $just_insertupdate_id;
			}
		}
		return $saved_ids;
	}

	/**
	 * Method get_overal_monitors_status()
	 *
	 * Gets overal monitor status.
	 *
	 * @return array $return Monitor status.
	 */
	public function get_overal_monitors_status() {

		$opts = array(
			'default' => true,
		);

		// get monitors status.
		$result = MainWP_AUM_DB::instance()->get_monitor_urls( 'betteruptime', $opts );

		$overal_status = array(
			'down'                => 0,
			'up'                  => 0,
			'trouble'             => 0,
			'critical'            => 0,
			'suspended'           => 0,
			'maintenance'         => 0,
			'discovery'           => 0,
			'configuration_error' => 0,
		);

		$count = 0;
		if ( $result ) {
			foreach ( $result as $val ) {
				if ( 0 == $val->status ) {
					$overal_status['down']++;
				} elseif ( 1 == $val->status ) {
					$overal_status['up']++;
				} elseif ( 2 == $val->status ) {
					$overal_status['trouble']++;
				} elseif ( 3 == $val->status ) {
					$overal_status['critical']++;
				} elseif ( 5 == $val->status ) {
					$overal_status['suspended']++;
				} elseif ( 7 == $val->status ) {
					$overal_status['maintenance']++;
				} elseif ( 9 == $val->status ) {
					$overal_status['discovery']++;
				} elseif ( 10 == $val->status ) {
					$overal_status['configuration_error']++;
				}
			}
		}
		return array( 'overal_status' => $overal_status );
	}


}
