<?php
/**
* Created by PhpStorm.
* User: denis
* Date: 11.01.17
* Time: 20:28
*/



class PHMM_Base_AdminNotice {
    
    private $option_notices;
    private $option_prefix;
    private $notice_ids = array();
    private $notices = array();
    private $product_display_name;
    private $product_slug;
    private $product_install_time_option;
    private $review_link;
    private $ajax_hook;
    private $notice_css_class;
    
    function __construct($product_display_name, $product_slug, $review_link, $product_install_time_option=null) {
        
       
        
        $this->product_display_name = $product_display_name;
        $this->product_slug = $product_slug;
        $this->product_install_time_option = $product_install_time_option || 'codeneric/'.$product_slug.'/install_time';
        $this->review_link = $review_link;
        $this->option_notices = $this->product_slug.'_notices';
        $this->option_prefix = $this->product_slug.'_notice_';
        $this->notice_css_class = $this->product_slug.'-notice';
        $this->ajax_hook= $this->product_slug.'_notice_dismiss';
        
        $this->set_notices();
        add_filter( 'admin_notices', array( $this, 'show_notices' ) );
        add_action( 'admin_head', array( $this, 'notices_head' ) );
        add_action( 'wp_ajax_'.$this->ajax_hook, array( $this, 'dismiss_notice' ) );
        
    }
    
    public function registerNotice(){
        add_action( 'admin_init', array( $this, 'admin_notices' ));
    }
    
    public function set_notices() {
        $notice_ids = get_option( $this->option_notices );
        if ( empty( $notice_ids ) ) return;
        foreach ( $notice_ids as $notice_id ) {
            $notice = get_option( $this->option_prefix . $notice_id );
            $this->notices[ $notice_id ] = $notice;
        }
        $this->notice_ids = $notice_ids;
    }
    
    public function add_notice( $id, $notice, $class = 'notice-success', $dismissible = true, $retry = '', $force_recreate = false ) {
        if ( isset( $this->notices[ $id ] ) && $this->notices[ $id ]['status'] == 'dismissed' && !$force_recreate ) return;
        $this->notices[ $id ] = array(
        'time' => current_time( 'timestamp' ),
        'notice' => $notice,
        'class' => $class,
        'retry' => $retry,
        'dismissible' => $dismissible,
        'status' => 'active'
        );
        if ( $dismissible ) {
            update_option( $this->option_prefix . $id, $this->notices[ $id ] );
            if ( !in_array( $id, $this->notice_ids ) ) {
                $this->notice_ids[] = $id;
                update_option( $this->option_notices, $this->notice_ids );
            }
        }
    }
    
    public function has_notices() {
        return !empty( $this->notices );
    }
    
    public function show_notices() {
        $notices = '';
        if ( $this->has_notices() ) {
            foreach ( $this->notices as $notice_id => $notice ) {
                //if ( $notice['status'] == 'active' || ( $notice['status'] == 'dismissed' && $notice['retry'] && ( $notice['time'] + $notice['retry'] ) < current_time( 'timestamp' ) ) ) {
                if ( $notice['status'] == 'active' ) {
                    if ( $notice['class'] == 'custom' ) {
                        echo $notice['notice'];
                    } else {
                        $dismissible = ( $notice['dismissible'] ) ? 'is-dismissible' : '';
                        $notices .= '<div data-notice-id="' . $notice_id . '" class="'.$this->notice_css_class.' notice ' . $dismissible . ' ' . $notice['class'] . '"><p>' . $notice['notice'] . '</p></div>';
                    }
                    /*
                    if ( $notice['status'] == 'dismissed' ) {
                    $notice['time'] = current_time( 'timestamp' );
                    $notice['status'] = 'active';
                    update_option( $this->option_prefix . $notice_id, $notice );
                    }
                    */
                }
            }
        }
        echo $notices;
    }
    
    public function notices_head() {
        if ( $this->has_notices() ) {
            ?>
  <script>
    jQuery(document).on('click', '.<?php echo $this->notice_css_class; ?> .notice-dismiss', function() {
      var notice_id = jQuery(this).parent().data('notice-id');
      if (notice_id) {
        jQuery.ajax({
          type: 'POST',
          url: ajaxurl,
          data: {
            action: '<?php echo $this->ajax_hook; ?>',
            id: notice_id
          }
        })
      }
      return false;
    });
    jQuery(document).on('click', '.<?php echo $this->notice_css_class; ?> .notice-dismiss-button', function() {
      var notice_id = jQuery(this).closest('.<?php echo $this->notice_css_class; ?>').data('notice-id');
      if (notice_id) {
        jQuery.ajax({
          type: 'POST',
          url: ajaxurl,
          data: {
            action: '<?php echo $this->ajax_hook; ?>',
            id: notice_id
          }
        })
      }
      jQuery(this).closest('.<?php echo $this->notice_css_class; ?>').fadeOut();
    });
  </script>
  <?php
        }
    }
    
    
    public function dismiss_notice() {
        $notice = get_option( $this->option_prefix . $_POST['id'] );
        $notice['status'] = 'dismissed';
        $notice['dismissed_time'] = current_time( 'timestamp' );
        update_option( $this->option_prefix . $_POST['id'], $notice );
        die();
    }
    
    public function delete_notice( $id ) {
        unset( $this->notices[ $id ] );
        return delete_option( $this->option_prefix . $id );
    }
    
    public function admin_notices()
    {
        
        $install_time = get_option($this->product_install_time_option, false);
        if($install_time === false)
        return update_option($this->product_install_time_option, current_time('timestamp') );
        
        if ((current_time('timestamp') - $install_time) >= DAY_IN_SECONDS * 60 ) {
            $review_text = '<p>' . __('You having been using '.$this->product_display_name.' for a bit and that\'s awesome! Could you please do '.$this->product_display_name.' a big favor and give it a 5-star rating on WordPress?  Reviews from users like you really help '.$this->product_display_name.' to grow and continue to improve.', $this->product_slug) . '<br /><br />
                ' . __('Thank You', $this->product_slug) . '<br />
            Denis, '.$this->product_display_name.' Developer</p>
            <p><a href="'.$this->review_link.'" target="_blank" class="button-primary notice-dismiss-button">' . __('Sure thing!', $this->product_slug) . '</a> &nbsp;
            <a href="#" class="button notice-dismiss-button">' . __('No thanks', $this->product_slug) . '</a></p>';
            $this->add_notice('review', $review_text, 'notice-info', true);
        }
    }
}