<?php
/**
* @package CembraPay Payment Gateway for WooCommerce
* @author  Adrian Emil Tudorache
* @license GPL-2.0+
* @link    https://www.tudorache.me/
**/

namespace CembraPayWoo;

if ( ! defined( 'ABSPATH' ) ) {
    header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
    exit;
}

class Admin {

    function __construct() {
        add_action( 'admin_enqueue_scripts', array($this, 'scripts'), 200 );
		add_action( 'add_meta_boxes', array($this, 'meta_boxes'), 1, 2 );
		add_action( 'admin_menu', array($this, 'menu'), 99 );
		add_filter( 'plugin_action_links_'  . CEMBRABASE, array($this, 'plugins_screen_links') );
	}

    function scripts() {

        $page    = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
        $tab     = isset($_GET['tab']) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : '';
        $section = isset($_GET['section']) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : '';

        if(
			$page && 'wc-settings' === $page 
            && $tab && 'checkout' === $tab
            && $section && 'cembrapay' === $section
		) {

			wp_enqueue_style('cembrapay-admin-gateway', CEMBRAURL . 'assets/css/admin/gateway.css');
			wp_enqueue_script('cembrapay-admin-gateway', CEMBRAURL . 'assets/build/admin/gateway.js', array('jquery'), '', true);
			wp_localize_script('cembrapay-admin-gateway', 'cembrapayAdmin', array(
				'nonce' => wp_create_nonce('wp_rest')
			));

		}

		global $post;

		if( $post && 'shop_order' === $post->post_type ) {

			include( CEMBRAPATH . 'inc/data/strings.php' );

			wp_enqueue_script('cembrapay-admin-order-js', CEMBRAURL . 'assets/build/admin/order.js', array('jquery'), '', true);
			wp_localize_script('cembrapay-admin-order-js', 'cembrapayAdminOrder', array(
				'nonce'   => wp_create_nonce('wp_rest'),
				'orderId' => $post->ID,
				'strings' => $order_strings
			));

		}

    }

	function menu() {

		add_submenu_page(
			'woocommerce',
	        __( 'CembraPay Settings', 'cembrapay' ),
	        __( 'CembraPay Settings', 'cembrapay' ),
	        'manage_options',
	        'wc-settings&tab=checkout&section=cembrapay',
	        array( $this, 'interface' )
	    );

	}

	function meta_boxes( $post_type, $post ) {

        $shop_order_screen = wc_get_page_screen_id( 'shop-order' );

		if( 'shop_order' !== $post_type && $shop_order_screen !== $post_type ) {
			return;
		}

		$order = wc_get_order( $post->ID );

		if( $order && 'cembrapay' === $order->get_payment_method() && 'pending' === $order->get_status()) {

			add_meta_box(
				'cembrapay_order_status',
				'CembraPay Order status',
				array($this, 'order_status_callback'),
				array( 'shop_order', $shop_order_screen ),
				'side',
				'default'
			);

		}

        $checkout_data = $order->get_meta( '_cembrapay_checkout_data' );

        if( $order && 'cembrapay' === $order->get_payment_method() && $checkout_data) {

			add_meta_box(
				'cembrapay_checkout_data',
				'CembraPay Checkout Data',
				array($this, 'order_checkout_data_callback'),
				array( 'shop_order', $shop_order_screen ),
				'side',
				'default'
			);

		}

	}

	function order_status_callback( $object ) {
		Service::load_template('admin/meta-box-order-status');
	}

    function order_checkout_data_callback( $object ) {
        
        if( ! $object ) {
            return;
        }

        $order = is_a( $object, 'WP_Post' ) ? wc_get_order( $object->ID ) : $object;

        if( ! $order ) {
            return;
        }
        
        Service::load_template('admin/meta-box-order-checkout-data', array(
            'checkout_data' => $order->get_meta( '_cembrapay_checkout_data' )
        ));

    }

	function plugins_screen_links( $links ) {

		$settings = esc_url( add_query_arg(
			array(
				'page' 		=> 'wc-settings',
				'tab' 		=> 'checkout',
				'section' 	=> 'cembrapay'
			),
			get_admin_url() . 'admin.php'
		) );

		$links[] = '<a href=' . esc_url( $settings ) . ' target="_blank">' . __( 'Settings', 'cembrapay' ) . '</a>';

		return $links;

	}

}
?>