<?php
/*
 * This file belongs to the YIT Framework.
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 */

if ( isset( $_POST['YITH_WC_amazon_s3_storage_connection_access_key_text'] ) && isset( $_POST['YITH_WC_amazon_s3_storage_connection_secret_access_key_text'] ) ) {
	$access_key = ( isset( $_POST['YITH_WC_amazon_s3_storage_connection_access_key_text'] ) ? $_POST['YITH_WC_amazon_s3_storage_connection_access_key_text'] : get_option( 'YITH_WC_amazon_s3_storage_connection_access_key_text' ) );
	$secret_access_key = ( isset( $_POST['YITH_WC_amazon_s3_storage_connection_secret_access_key_text'] ) ? $_POST['YITH_WC_amazon_s3_storage_connection_secret_access_key_text'] : get_option( 'YITH_WC_amazon_s3_storage_connection_secret_access_key_text' ) );

	if ( empty( $_POST['YITH_WC_amazon_s3_storage_connection_access_key_text'] ) && empty( $access_key ) || empty( $_POST['YITH_WC_amazon_s3_storage_connection_secret_access_key_text'] ) && empty( $secret_access_key ) ) {
		update_option( 'YITH_WC_amazon_s3_storage_connection_success', 0 );
	}else{
		require_once( constant( 'YITH_WC_AMAZON_S3_STORAGE_PATH' ) . 'includes/class.yith-wc-amazon-s3-storage-aws-s3-client.php' );
		$aws_s3_client = new YITH_WC_Amazon_S3_Storage_Aws_S3_Client( $access_key, $secret_access_key );

		$buckets = $aws_s3_client->Checking_Credentials();
	}
}


$settings = array(
	'connect-s3' => array(
		'connect-options'              => array(
			'title' => __( 'API Keys', 'yith-amazon-s3-storage' ),
			'type'  => 'title',
			'desc'  => '',
		),
		'yith-amazon-s3-storage-access-key'        => array(
			'id'        => 'YITH_WC_amazon_s3_storage_connection_access_key_text',
			'name'      => __( 'Access Key', 'yith-amazon-s3-storage' ),
			'type'      => 'yith-field',
			'yith-type' => 'text',
			'desc'      => __( 'Set the access key', 'yith-amazon-s3-storage' ),
		),

		'yith-amazon-s3-storage-secret-access-key'        => array(
			'id'        => 'YITH_WC_amazon_s3_storage_connection_secret_access_key_text',
			'name'      => __( 'Secret Access Key', 'yith-amazon-s3-storage' ),
			'type'      => 'yith-field',
			'yith-type' => 'text',
			'desc'      => __( 'Set the secret access key. <a href="https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/" target="_blank">
                    Where can I find my secret key?
                </a>', 'yith-amazon-s3-storage' ),
		),
		'general-connect-options-end'          => array(
			'type' => 'sectionend',
			'id'   => 'yith-amazon-s3-storage-connect-options'
		),
	)
);

$connected_status = false;
$connected_message = '';

if ( get_option( 'YITH_WC_amazon_s3_storage_connection_success' ) ) {
	$connected_status = true;
	$path_valid_image = constant( 'YITH_WC_AMAZON_S3_STORAGE_ASSETS_URL' ) . '/images/access-ok.png';
	$connected_icon = '<img class="YITH_WC_amazon_s3_storage_error_accessing_class_img" style="width: 35px;" src="' . $path_valid_image . '"/>';

	$connected_message = esc_html__( 'Connection to Amazon S3 Storage was successful', 'yith-amazon-s3-storage' );
} else {
	$path_error_image = constant( 'YITH_WC_AMAZON_S3_STORAGE_ASSETS_URL' ) . '/images/access-error-logs.png';
	$connected_icon = '<img class="YITH_WC_amazon_s3_storage_error_accessing_class_img" style="width: 35px;" src="' . $path_error_image . '"/>';

	$connected_message = esc_html__( 'An error occurred while accessing, the credentials (access key or secret key) are NOT correct',
		'yith-amazon-s3-storage' );

}

if ( $connected_status ) {
	$connected_array = array(
		'connected-status' => array(
			'name'      => $connected_icon . ' ' . $connected_message,
			'type'      => 'yith-field',
			'yith-type' => 'yith-wcams3-connected-status',
		),
	);
	$settings['connect-s3'] = YITH_WC_amazon_s3_merge_array_by_key( $settings['connect-s3'], $connected_array, 'yith-amazon-s3-storage-secret-access-key', 'connected-status' );
}

return apply_filters( 'yith_wc_amazon_s3_storage_connect_options', $settings );
