<?php
/**
 * GFB Setting Class
 *
 * @package GFB.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! class_exists( 'APBGF_Setting' ) ) {
	/**
	 * Checks if WooCommerce is enabled.
	 */
	class APBGF_Setting {     
		/**
		 * Instance
		 *
		 * @var $instance
		 */
		private static $instance = null;

		private static $settings = null;

		/**
		 * Init the Dependencies.
		 */
		private function __construct() {

			self::$settings = get_option('apbgf_settings', array());

			add_action('apbgf_after_menu_register', array( $this, 'register_menu' ));

			add_action('wp_ajax_apbgf_general_settings', array( $this, 'apbgf_general_settings' ) );
		}

		public static function get_settings() {         

			$apbgf_settings = self::$settings;

			$data = array();
			
			$data['hide_slot_capacity'] = isset($apbgf_settings['general']['hide_slot_capacity']) ? $apbgf_settings['general']['hide_slot_capacity'] : 'disabled';

			$data['apbgf_price_policy'] = isset($apbgf_settings['general']['apbgf_price_policy']) ? $apbgf_settings['general']['apbgf_price_policy'] : 'admin';

			$data['apbgf_appointment_policy'] = isset($apbgf_settings['general']['apbgf_appointment_policy']) ? $apbgf_settings['general']['apbgf_appointment_policy'] : 'admin';

			$data['gbf_lead_time'] = isset($apbgf_settings['general']['gbf_lead_time']) ? $apbgf_settings['general']['gbf_lead_time'] : '60';

			$data['gbf_time_duration'] = isset($apbgf_settings['general']['gbf_time_duration']) && !empty($apbgf_settings['general']['gbf_time_duration']) ? $apbgf_settings['general']['gbf_time_duration'] : '30';
			
			$data['gbf_time_duration'] = isset($apbgf_settings['general']['gbf_time_duration']) && !empty($apbgf_settings['general']['gbf_time_duration']) ? $apbgf_settings['general']['gbf_time_duration'] : '30';

			$data['gbf_time_interval'] = isset($apbgf_settings['general']['gbf_time_interval']) && !empty($apbgf_settings['general']['gbf_time_interval']) ? $apbgf_settings['general']['gbf_time_interval'] : '15';

			$data['apbgf_show_timeslot'] = isset($apbgf_settings['general']['gbf_show_timeslot']) ? $apbgf_settings['general']['gbf_show_timeslot'] : 'right';

			$data['apbgf_calendar_timeslot_format'] = isset($apbgf_settings['general']['apbgf_calendar_timeslot_format']) ? $apbgf_settings['general']['apbgf_calendar_timeslot_format'] : '24';

			 $data['apbgf_prior_months_book_appointment'] = isset($apbgf_settings['general']['apbgf_prior_months_book_appointment']) && !empty($apbgf_settings['general']['apbgf_prior_months_book_appointment']) ? $apbgf_settings['general']['apbgf_prior_months_book_appointment'] : '3';          

			/*CT-284*/
			$data['apbgf_prior_days_book_appointment'] = isset($apbgf_settings['general']['apbgf_prior_days_book_appointment']) && '' != $apbgf_settings['general']['apbgf_prior_days_book_appointment'] ? $apbgf_settings['general']['apbgf_prior_days_book_appointment'] : '0';
			/*CT-284*/

			$data['apbgf_cal_gradient_color_top'] = isset($apbgf_settings['display']['apbgf_cal_gradient_color_top']) ? $apbgf_settings['display']['apbgf_cal_gradient_color_top'] : '#f1f3f6';

			$data['apbgf_cal_gradient_color_bottom'] = isset($apbgf_settings['display']['apbgf_cal_gradient_color_bottom']) ? $apbgf_settings['display']['apbgf_cal_gradient_color_bottom'] : '#f1f3f6';

			$data['apbgf_cal_border_color'] = isset($apbgf_settings['display']['apbgf_cal_border_color']) ? $apbgf_settings['display']['apbgf_cal_border_color'] : '#26547c21';

			$data['apbgf_cal_header_bg_color'] = isset($apbgf_settings['display']['apbgf_cal_header_bg_color']) ? $apbgf_settings['display']['apbgf_cal_header_bg_color'] : '#7C9299';

			$data['apbgf_cal_header_text_color'] = isset($apbgf_settings['display']['apbgf_cal_header_text_color']) ? $apbgf_settings['display']['apbgf_cal_header_text_color'] : '#FFFFFF';


			$data['apbgf_cal_text_color'] = isset($apbgf_settings['display']['apbgf_cal_text_color']) ? $apbgf_settings['display']['apbgf_cal_text_color'] : '#6D8298';

			$data['apbgf_cal_selected_text_color'] = isset($apbgf_settings['display']['apbgf_cal_selected_text_color']) ? $apbgf_settings['display']['apbgf_cal_selected_text_color'] : '#FFFFFF';

			$data['apbgf_cal_selected_text_bg_color'] = isset($apbgf_settings['display']['apbgf_cal_selected_text_bg_color']) ? $apbgf_settings['display']['apbgf_cal_selected_text_bg_color'] : '#00D0AC';

			$data['apbgf_cal_available_days_text_color'] = isset($apbgf_settings['display']['apbgf_cal_available_days_text_color']) ? $apbgf_settings['display']['apbgf_cal_available_days_text_color'] : '#6D8298';

			$data['apbgf_cal_available_days_bg_color'] = isset($apbgf_settings['display']['apbgf_cal_available_days_bg_color']) ? $apbgf_settings['display']['apbgf_cal_available_days_bg_color'] : '#FFFFFF';

			$data['apbgf_cal_holiday_days_text_color'] = isset($apbgf_settings['display']['apbgf_cal_holiday_days_text_color']) ? $apbgf_settings['display']['apbgf_cal_holiday_days_text_color'] : '#6D8298';

			$data['apbgf_cal_holiday_days_bg_color'] = isset($apbgf_settings['display']['apbgf_cal_holiday_days_bg_color']) ? $apbgf_settings['display']['apbgf_cal_holiday_days_bg_color'] : '';
			
			$data['apbgf_label_location'] = isset($apbgf_settings['label']['apbgf_label_location']) && !empty($apbgf_settings['label']['apbgf_label_location']) ? $apbgf_settings['label']['apbgf_label_location'] : 'Location';

			$data['apbgf_label_category'] = isset($apbgf_settings['label']['apbgf_label_category']) && !empty($apbgf_settings['label']['apbgf_label_category']) ? $apbgf_settings['label']['apbgf_label_category'] : 'Service Category';

			$data['apbgf_label_service'] = isset($apbgf_settings['label']['apbgf_label_service']) && !empty($apbgf_settings['label']['apbgf_label_service']) ? $apbgf_settings['label']['apbgf_label_service'] : 'Service';

			$data['apbgf_label_staff'] = isset($apbgf_settings['label']['apbgf_label_staff']) && !empty($apbgf_settings['label']['apbgf_label_staff']) ? $apbgf_settings['label']['apbgf_label_staff'] : 'Staff';
		  
			$data['apbgf_company_name'] = isset($apbgf_settings['company']['apbgf_company_name']) ? $apbgf_settings['company']['apbgf_company_name'] : get_bloginfo('name');
			
			$data['apbgf_company_address'] = isset($apbgf_settings['company']['apbgf_company_address']) ? $apbgf_settings['company']['apbgf_company_address'] : '';

			$data['apbgf_company_number'] = isset($apbgf_settings['company']['apbgf_company_number']) ? $apbgf_settings['company']['apbgf_company_number'] : '';

			$data['apbgf_company_email'] = isset($apbgf_settings['company']['apbgf_company_email']) ? $apbgf_settings['company']['apbgf_company_email'] : '';

			$data['apbgf_company_website_url'] = isset($apbgf_settings['company']['apbgf_company_website_url']) ? $apbgf_settings['company']['apbgf_company_website_url'] : get_bloginfo('url');

			$data['apbgf_sender_name'] = isset($apbgf_settings['email']['apbgf_sender_name']) ? $apbgf_settings['email']['apbgf_sender_name'] : get_bloginfo('name');

			$data['apbgf_sender_email'] = isset($apbgf_settings['email']['apbgf_sender_email']) ? $apbgf_settings['email']['apbgf_sender_email'] : get_bloginfo('admin_email');

			return $data;
		}

		public function apbgf_general_settings() {

			check_ajax_referer( 'appointment-and-booking-for-gravity-forms', 'nonce' );

			$apbgf_settings = get_option('apbgf_settings', array());

			$response = array(
				'message' => esc_html__( 'Setting not saved.', 'appointment-and-booking-for-gravity-forms' ),
				'status'  => 'error'
			);          

			$type = isset($_POST['type']) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
			$data = $_POST;

			if ( ! empty($type) ) {

				foreach ( $data as $k => $val ) {
					if ( 'nonce' != $k && 'action' != $k && 'type' != $k ) {
						$apbgf_settings[ $type ][ $k ] = $val;
					}
				}

				update_option('apbgf_settings', $apbgf_settings );              
				$response['message'] = __('Settings saved successfully', 'appointment-and-booking-for-gravity-forms');
				$response['status'] = 'success';
				$response['settings'] = get_option('apbgf_settings');
			}

			echo wp_json_encode( $response );
			wp_die();
		}

		public function register_menu() {

			do_action('apbgf_before_settings_menu');

			add_submenu_page(
				'appointment-and-booking-for-gravity-forms',
				__( 'Settings - Gravity Booking', 'appointment-and-booking-for-gravity-forms' ),
				__( 'Settings', 'appointment-and-booking-for-gravity-forms' ),
				'manage_options',
				'apbgf-setting',
				array( get_class($this), 'apbgf_setting_page' ),
				30
			);
		}

		public static function apbgf_setting_page() {
			require_once APBGF_ADMIN_VIEW_PATH . 'setting.php';
		}
		
		/**
		 * Method gcal_synchronization_fields
		 *
		 * @return array
		 */
		public static function gcal_synchronization_fields() {

		$fields = array(
			'name'              => esc_html__( 'Customer/Staff Name', 'appointment-and-booking-for-gravity-forms' ),
			'email'             => esc_html__( 'Customer/Staff Email', 'appointment-and-booking-for-gravity-forms' ),
			'phone'             => esc_html__( 'Customer/Staff Phone Number', 'appointment-and-booking-for-gravity-forms' ),
			'appointment_id'    => esc_html__( 'Appointment ID', 'appointment-and-booking-for-gravity-forms' ),
			'appointment_date'  => esc_html__( 'Appointment Date', 'appointment-and-booking-for-gravity-forms' ),
			'appointment_time'  => esc_html__( 'Appointment Time', 'appointment-and-booking-for-gravity-forms' ),
			'location'          => esc_html__( 'Location', 'appointment-and-booking-for-gravity-forms' ),
			'service'           => esc_html__( 'Service', 'appointment-and-booking-for-gravity-forms' ),
			'staff'             => esc_html__( 'Staff', 'appointment-and-booking-for-gravity-forms' ),
			'slot'              => esc_html__( 'Slot Qty', 'appointment-and-booking-for-gravity-forms' ),
			'cost'              => esc_html__( 'Cost', 'appointment-and-booking-for-gravity-forms' )
		);

		return ( array ) apply_filters( 'apbgf_gcal_synchronization_fields', $fields );
		}

		/**
		 * Singleton Class Instatnce.
		 */
		public static function get_instance() {
			if ( null === self::$instance ) {
				self::$instance = new APBGF_Setting();
			}

			return self::$instance;
		}
	}

	APBGF_Setting::get_instance();
}
