<?php

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

if ( ! class_exists( 'AIEO_Repeat_Order', false ) ) {

class AIEO_Repeat_Order {

	
	public function __construct() {
		if ( ! class_exists( 'WooCommerce' ) ) {
			return;
		}

		$settings = $this->settings();
		if ( empty( $settings['enabled'] ) ) {
			return;
		}

		
		if ( class_exists( 'OM_CE_Repeat_Order' ) ) {
			return;
		}

		$this->init_hooks();
	}

	
	private function settings() {
		$defaults = array(
			'enabled'                  => true,
			'show_on_order_list'       => true,
			'create_order_note'        => true,
			'reactivate_order_again'   => false,
			'show_reorder_from_column' => false,
			'cart_notice'              => '',
			'button_label'             => '',
			'allowed_statuses'         => array( 'completed', 'processing' ),
		);

		if ( ! class_exists( 'AIEO_DMM_Settings' ) ) {
			return $defaults;
		}

		$out = array();
		foreach ( $defaults as $key => $default ) {
			$out[ $key ] = AIEO_DMM_Settings::get( 'repeat_order_' . $key, $default );
		}

		return $out;
	}

	
	private function init_hooks() {
		
		add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'add_order_again_action' ), 10, 2 );

		
		add_action( 'woocommerce_ordered_again', array( $this, 'ordered_again' ) );

		
		add_action( 'woocommerce_thankyou', array( $this, 'create_order_note' ), 10, 1 );

		
		add_action( 'woocommerce_cart_is_empty', array( $this, 'reset_session_flag' ) );

		
		add_action( 'init', array( $this, 'reactivate_action' ), 9999 );

		
		add_filter( 'woocommerce_admin_order_actions', array( $this, 'add_admin_order_action_button' ), 100, 2 );

		
		add_filter( 'woocommerce_account_orders_columns', array( $this, 'add_my_account_orders_column' ) );
		add_action( 'woocommerce_my_account_my_orders_column_reorder', array( $this, 'my_orders_reorder_column' ) );

		
		add_filter( 'woocommerce_valid_order_statuses_for_order_again', array( $this, 'valid_order_statuses_for_order_again' ), 10 );
	}

	
	public function reactivate_action() {
		$settings = $this->settings();
		if ( ! empty( $settings['reactivate_order_again'] ) ) {
			add_action( 'woocommerce_order_details_after_order_table', 'woocommerce_order_again_button', 9999 );
		}
	}

	
	public function ordered_again( $order_id ) {
		if ( ! WC()->session ) {
			return;
		}

		WC()->session->set( 'reorder_from_orderid', $order_id );

		$settings = $this->settings();
		$notice   = isset( $settings['cart_notice'] ) ? $settings['cart_notice'] : '';

		if ( ! empty( $notice ) ) {
			wc_add_notice( $notice, 'notice' );
		}
	}

	
	public function create_order_note( $order_id ) {
		if ( ! WC()->session ) {
			return;
		}

		$reorder_id = WC()->session->get( 'reorder_from_orderid' );

		if ( empty( $reorder_id ) ) {
			return;
		}

		$order = wc_get_order( $order_id );
		if ( ! is_a( $order, 'WC_Order' ) ) {
			
			WC()->session->set( 'reorder_from_orderid', '' );
			return;
		}

		
		$order->add_meta_data( '_reorder_from_id', $reorder_id, true );
		$order->save();

		$settings = $this->settings();
		if ( empty( $settings['create_order_note'] ) ) {
			WC()->session->set( 'reorder_from_orderid', '' );
			return;
		}

		
		
		$url  = get_edit_post_link( $reorder_id );
		$note = sprintf(
			
			wp_kses(
				__( 'This is a reorder of order #<a href="%1$s">%2$s</a>.', 'ai-eshop-optimizer' ),
				array(
					'a' => array(
						'href' => array(),
					),
				)
			),
			esc_url( $url ),
			(int) $reorder_id
		);

		
		$order->add_order_note( apply_filters( 'aieo_repeat_order_note', $note, $reorder_id, $order_id ) );

		
		WC()->session->set( 'reorder_from_orderid', '' );
	}

	
	public function add_order_again_action( $actions, $order ) {
		$settings = $this->settings();

		if ( empty( $settings['show_on_order_list'] ) ) {
			return $actions;
		}

		if ( ! $order || ! is_user_logged_in() ) {
			return $actions;
		}

		
		$valid_statuses = $this->get_allowed_statuses();
		if ( ! $order->has_status( $valid_statuses ) ) {
			return $actions;
		}

		
		
		
		$label = isset( $settings['button_label'] ) ? trim( (string) $settings['button_label'] ) : '';
		if ( $label === '' ) {
			$label = __( 'Order again', 'woocommerce' );
		}

		$actions['order-again'] = array(
			'url'  => wp_nonce_url( add_query_arg( 'order_again', $order->get_id() ), 'woocommerce-order_again' ),
			'name' => $label,
		);

		return $actions;
	}

	
	private function get_allowed_statuses() {
		$settings = $this->settings();
		$statuses = isset( $settings['allowed_statuses'] ) ? $settings['allowed_statuses'] : array( 'completed', 'processing' );

		if ( empty( $statuses ) ) {
			$statuses = array( 'completed', 'processing' );
		}

		return (array) $statuses;
	}

	
	public function valid_order_statuses_for_order_again( $statuses ) {
		return $this->get_allowed_statuses();
	}

	
	public function add_admin_order_action_button( $actions, $order ) {
		if ( ! is_a( $order, 'WC_Order' ) ) {
			return $actions;
		}

		$reorder_from = $order->get_meta( '_reorder_from_id', true );

		if ( $reorder_from > 0 ) {
			$actions['pr_repeat'] = array(
				'url'    => wp_nonce_url( admin_url( 'post.php?post=' . (int) $reorder_from . '&action=edit' ), '' ),
				'name'   => __( 'Show original order', 'ai-eshop-optimizer' ),
				'action' => 'view-original-order',
			);
		}

		return $actions;
	}

	
	public function add_my_account_orders_column( $columns ) {
		$settings = $this->settings();

		if ( empty( $settings['show_reorder_from_column'] ) ) {
			return $columns;
		}

		$new_columns = array();
		foreach ( $columns as $key => $name ) {
			if ( 'order-actions' === $key ) {
				$new_columns['reorder'] = __( 'Reorder from', 'ai-eshop-optimizer' );
			}
			$new_columns[ $key ] = $name;
		}

		return $new_columns;
	}

	
	public function my_orders_reorder_column( $order ) {
		$settings = $this->settings();

		if ( empty( $settings['show_reorder_from_column'] ) ) {
			return;
		}

		if ( ! is_a( $order, 'WC_Order' ) ) {
			return;
		}

		$reorder_from = $order->get_meta( '_reorder_from_id', true );

		if ( $reorder_from > 0 ) {
			$reorder = wc_get_order( $reorder_from );
			if ( $reorder ) {
				?>
				<a href="<?php echo esc_url( $reorder->get_view_order_url() ); ?>">
					<?php
					
					echo esc_html( sprintf( _x( '#%s', 'hash before order number', 'woocommerce' ), $reorder->get_order_number() ) );
					?>
				</a>
				<?php
			}
		}
	}

	
	public function reset_session_flag() {
		if ( WC()->session ) {
			WC()->session->set( 'reorder_from_orderid', '' );
		}
	}
}
}
