<?php
/**
 * @package Ultimate Affiliate
 * since version 9.5.5
 */
namespace Indeed\Uap\Admin;

defined( 'ABSPATH' ) || exit;

class Admin_Dashboard_Messages
{
  public function __construct() {

    // before menu content
    add_filter( 'uap_filter_admin_plugin_top_messages', array( $this, 'before_menu' ), 999, 1 );
    // after menu content
    add_action( 'uap_admin_action_dashboard_after_header', array( $this, 'plugin_pages' ), 1 );

    add_action( 'admin_notices', array( $this, 'admin_dashboard' ), 999 );
    // style & js
    add_action( "admin_enqueue_scripts", array( $this, 'add_style_scripts' ) );

  }

  public function admin_dashboard() {
    if ( isset( $_GET['page'] ) && sanitize_text_field( $_GET['page'] ) === 'ultimate_affiliates_pro' ) {
      return;
    }
    $data = get_transient( 'ua_dynamic_messages' );

    if ( $data === false || $data === '' || empty( $data['messages'] ) || empty( $data['global']['enabled'] ) ){
      return;
    }

    if ( ! empty( $data['global']['debug'] ) && ( ! defined( 'UA_DEBUG' ) || ! UA_DEBUG ) ) {
      return;
    }

    $inserted_at = isset( $data['inserted_at'] ) ? strtotime( $data['inserted_at'] ) : 0;
    foreach ($data['messages'] as $message) {
        if ( $message['location'] !== 'admin_dashboard' ) {
            continue;
        }
        if ( $message['type'] !== 'banner' ) {
            continue;
        }
        if ( ! $this->should_display( $message, $inserted_at ) ) {
          continue;
        }
        ?>
        <div class="uap-admin-dashboard-top-news-wrap" ><?php
        echo wp_kses_post( $message['html'] );
        if ( (bool)$message['dismissible'] === true ) {
          ?><div class="uap-admin-marketing-close" data-id="<?php echo esc_attr( $message['id'] );?>">x</div><?php
        }?>
        </div>
        <?php
      }
  }

  public function add_style_scripts() {
    $data = get_transient( 'ua_dynamic_messages' );

    if ( $data === false || $data === '' || empty( $data['messages'] ) || empty( $data['global']['enabled'] ) ){
      return;
    }

    // maybe debug
    if ( ! empty( $data['global']['debug'] ) && ( ! defined( 'UA_DEBUG' ) || ! UA_DEBUG ) ) {
      return;
    }

    wp_enqueue_script( 'ua-dynamic-messages', UAP_URL . 'assets/js/dynamic-messages.js', ['jquery'], UA_ASSET_VERSION );

    if ( empty( $data['style'] ) ) {
      return;
    }

    if ( empty ( $data['style'] ) ) {
      return;
    }
    $custom_css = '';
    foreach ( $data['style'] as $selector => $style_values ) {
      $custom_css .= $selector . '{';
      foreach ( $style_values as $attribute => $value ){
        $custom_css .= $attribute . ': ' . $value . ';';
      }
      $custom_css .= '}';
    }
    wp_register_style( 'ua-admin-dummy-handle', false );
    wp_enqueue_style( 'ua-admin-dummy-handle' );
    wp_add_inline_style( 'ua-admin-dummy-handle', stripslashes( $custom_css ) );
  }

  public function plugin_pages() {
    $data = get_transient( 'ua_dynamic_messages' );

    if ( $data === false || $data === '' || empty( $data['messages'] ) || empty( $data['global']['enabled'] ) ){
      return;
    }

    if ( ! empty( $data['global']['debug'] ) && ( ! defined( 'UA_DEBUG' ) || ! UA_DEBUG ) ) {
      return;
    }

    $inserted_at = isset( $data['inserted_at'] ) ? strtotime( $data['inserted_at'] ) : 0;
    foreach ($data['messages'] as $message) {
        if ( $message['location'] !== 'plugin_before_content' ) {
            continue;
        }
        if ( $message['type'] !== 'banner' ) {
            continue;
        }
        if ( ! $this->should_display( $message, $inserted_at ) ) {
          continue;
        }
        ?>
        <div class="uap-admin-dashboard-top-news-wrap" ><?php
        echo wp_kses_post( $message['html'] );
        if ( (bool)$message['dismissible'] === true ) {
          ?><div class="uap-admin-marketing-close" data-id="<?php echo esc_attr( $message['id'] );?>">x</div><?php
        }?>
        </div>
        <?php
      }
  }

  public function before_menu( $output = '' ) {
    $data = get_transient( 'ua_dynamic_messages' );

    if ( $data === false || $data === '' || empty( $data['messages'] ) || empty( $data['global']['enabled'] ) ){
      return $output;
    }

    // maybe debug
    if ( ! empty( $data['global']['debug'] ) && ( ! defined( 'UA_DEBUG' ) || ! UA_DEBUG ) ) {
      return $output;
    }

    $inserted_at = isset( $data['inserted_at'] ) ? strtotime( $data['inserted_at'] ) : 0;

    foreach ( $data['messages'] as $message ) {
        if ( $message['location'] !== 'plugin_before_menu' ) {
            continue;
        }
        if ( $message['type'] !== 'banner' ) {
            continue;
        }
        if ( ! $this->should_display( $message, $inserted_at ) ) {
          continue;
        }
        $output .= '<div class="uap-dashboard-announcement" >';
        $output .= $message['html'];
        if ( (bool)$message['dismissible'] === true ) {
          $output .= '<div class="uap-close-button uap-admin-marketing-close" data-id="' . esc_attr( $message['id'] ) . '">x</div>';
        }
        $output .= '</div>';
      }
      return $output;
  }

  /**
   * @param array
   * @param int
   * @return bool
   */
  public function should_display( $message=array(), $inserted_at=0 ) {
    $current_time = indeed_get_unixtimestamp_with_timezone();
    $message_start_date = $message['start_date'];
    $message_end_date = $message['end_date'];
    $delay_time = $message['delay_time'];
    $show_time = $message['show_time'];

    // no id - out
    if ( ! isset( $message['id'] ) || $message['id'] === '' ) {
      return false;
    }

    // no html - out
    if ( ! isset( $message['html'] ) || $message['html'] === '' ) {
      return false;
    }

    // delay time
    if ( $delay_time && $current_time < ( $inserted_at + $delay_time * HOUR_IN_SECONDS ) ){
      return false;
    }

    // show time
    if ( $show_time && $current_time > ( $inserted_at + $show_time * HOUR_IN_SECONDS ) ){
      return false;
    }

    // start date - too early
    if ( $message_start_date !== '' && strtotime( $message_start_date ) > $current_time ){
      return false;
    }

    // end date - too late
    if ( $message_end_date !== '' && strtotime( $message_end_date ) < $current_time ){
      return false;
    }

    $already_dismiss_time_array = get_option( 'ua_dismiss_dynamic_messages_time', false );
    $already_dismiss_time = isset( $already_dismiss_time_array[ $message['id'] ] ) ? $already_dismiss_time_array[ $message['id'] ] : false;

    // show only once, and the message has been dismissed
    if ( $already_dismiss_time && ! empty( $message['show_once'] ) ){
      return false;
    }

    // dismiss delay
    $dismiss_delay_time = $message['dismiss_delay_time'] ? (int)$message['dismiss_delay_time'] * HOUR_IN_SECONDS : 0;
    // dismiss delay - too early
    if ( $already_dismiss_time && ( $already_dismiss_time + $dismiss_delay_time ) > $current_time ) {
      return false;
    }
    $dismiss_counts_array = get_option( 'ua_dismiss_dynamic_messages_count', false );
    $dismiss_counts = isset( $dismiss_counts_array[ $message['id'] ] ) ? $dismiss_counts_array[ $message['id'] ] : 0;
    $dismiss_limit = $message['max_impressions'] ? (int)$message['max_impressions'] : 0;

    // dismiss limit has been reached
    if ( $dismiss_counts && $dismiss_limit < $dismiss_counts ){
      return false;
    }

    return true;
  }


}
