<?php

/**
 * Fontend Ajax
 * Handles all ajax requests for frontend area
 *
 * @since 1.7.6
 * @package EasyVideoReviews
 */

// Namespace.
namespace EasyVideoReviews;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit( 1 );


if ( ! class_exists( __NAMESPACE__ . '\Ajax' ) ) {
    /**
     * Frontend Ajax
     *
     * @since 1.7.6
     */
    class Ajax extends \EasyVideoReviews\Base\Controller {

        // Use utilities trait.
        use \EasyVideoReviews\Traits\Utilities;

        /**
         * Prefix for all ajax actions
         *
         * @var string
         */
        protected $prefix = 'evr_';

        /**
         * Registers all ajax hooks
         *
         * @return void
         */
        public function register_hooks() {
            add_action( 'wp_ajax_' . $this->prefix . 'update_gallery_settings', array( $this, 'update_gallery_settings' ) );
            add_action( 'wp_ajax_nopriv' . $this->prefix . 'update_gallery_settings', array( $this, 'update_gallery_settings' ) );
            add_action( 'wp_ajax_' . $this->prefix . 'check_existing_gallery', array( $this, 'check_existing_gallery' ) );
            add_action( 'wp_ajax_nopriv' . $this->prefix . 'check_existing_gallery', array( $this, 'check_existing_gallery' ) );
        }

        /**
         * Create gallery with folder embed
         *
         * @return void
         */
        public function update_gallery_settings() {

            // Check nonce.
            check_ajax_referer( 'evr_nonce', 'nonce' );

            $old_gallery_exist = false;

            $galleries = get_option( 'evr_gallaries', array() );

            $old_galleris_iputs = $this->io()->get_inputs();

            if ( 0 < count( $galleries ) ) {
                foreach ( $galleries as $gallery ) {
                    if ( isset( $gallery['previusFolder'] ) ) {
                        if ( $gallery['previusFolder'] === $old_galleris_iputs['previusFolder'] ) {
                               $old_gallery_exist = true;
                        }
                    }
                }
            }

            if ( ! $old_gallery_exist || 0 === count( $galleries ) ) {
                $galleries[] = $old_galleris_iputs;
                update_option( 'evr_gallaries', $galleries );
            }
            $this->io()->send_json( true, __( 'Settings updated', 'easy-video-reviews' ), $galleries );
        }

        /**
         * Check existing gallery with folder embed
         *
         * @return void
         */
        public function check_existing_gallery() {

            // Check nonce.
            check_ajax_referer( 'evr_nonce', 'nonce' );

            $gallery_id = $this->io()->get_inputs();

            // get created gallery from database
            $galleries = $this->option()->get( 'gallaries' );

            // get selected gallery by gallery id
            $get_selected_gallery = array_filter(
            $galleries,
            function ( $gallery ) use ( $gallery_id ) {
                return $gallery['id'] === (string) $gallery_id['gid'];
            }
            );

            if ( ! empty( $get_selected_gallery ) ) {
                $this->io()->send_json( true, 'gallery exist' );
            } else {
                $this->io()->send_json( false, 'gallery not exist' );
            }

            // $this->io()->send_json( true,'',$check_iputs);
        }
    }

    // Instantiate.
    Ajax::init();
}
