<?php

namespace Elex\MultiVendor\Model;

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

use ArrayAccess;

class SiteSettings { 

	const SETTINGS_KEY = 'elex_mv_admin_installation_params';

	protected $attributes = array() ;

	public function __construct( $attributes ) {
		$this->attributes = $attributes;
	}

	public static function defaultSet() {
	
		return array(

			'elex_mv_make_me_vendor_switch'             => false,
			'name'                                      => '',
			'url'                                       => '',
			'phone_code'                                => '',
			'phone_number'                              => '',
			'store_full_addr1'                          => '',
			'store_full_addr2'                          => '',
			'city'                                      => '',
			'postal_code'                               => '',
			'state'                                     => '',
			'country'                                   => '',
			'elex_mv_store_slug'                        => 'elex-store',
			
			'ship_fee'                                  => 'vendor',
			'tax_fee'                                   => 'vendor',
			'vendor_onboarding_wizard'                  => 'on',
			'allow_vendor_flat_rate'                    => '',
			'can_vendor_sell_products_without_approval' => '',
			'can_vendor_choose_shipping_services'       => '',
			'can_vendor_create_product_category'        => '',
			'can_vendor_change_order_status'            => 'on',
			'approve_vendor_onboard'                    => 'on',
			'commission_for_each_sale'                  => 0,
			'commission_type_of_sale'                   => 'percent',
			'elex_mv_date_of_month_pay_settle'          => 1,
			'elex_mv_order_status_radios'               => array( 'wc-completed', 'wc-processing' ),
			'include_shipping_cost_in_commission'       => false,
			'elex_mv_header_text'                       => 'Please Enter & Verify Your Details',
			'buffer_time'                               => 30,
			'is_setup_finished'                         => false, 
		);
	}

	public static function get_settings_menu() {

		$menus = array(
			array(
				'title' => __( 'General Settings' , 'elex-multi-vendor-for-woocommerce' ),
				'slug'  => 'general',
			),
			array(
				'title' => __( 'Vendor Capability' , 'elex-multi-vendor-for-woocommerce' ),
				'slug'  => 'vendor_capability',
			),
			array(
				'title' => __( 'Commission & Charges' , 'elex-multi-vendor-for-woocommerce' ),
				'slug'  => 'commission',
			),
			array(
				'title' => __( 'Settlement' , 'elex-multi-vendor-for-woocommerce' ),
				'slug'  => 'settlement',
			),
		
		);

		/** 
		 * Fire a filter hook for admin settings menu
		 *
		 * @since 2.0.0
		 * @param $menus
		 */
		$menus = apply_filters( 'elex_mv_admin_settings_menu', $menus );

		return $menus;
	}

	public static function load() {

		$admin_settings_values = get_option( self::SETTINGS_KEY, self::defaultSet() );

		return new self( $admin_settings_values );
	}

	public function save() {
		update_option( self::SETTINGS_KEY, $this->attributes );
		// update_option( 'already_elex_mv_customer', true );
	}

	public function merge( $new_options ) {
		$this->attributes = array_merge( $this->attributes, $new_options );
		return $this;
	}

	public function get( $key ) {
		if ( isset( $this->attributes[ $key ] ) ) {
			return $this->attributes[ $key ];
		}
		return null;
	}
	
	public function __get( $key ) {
		return $this->get( $key );
	}

	public function set( $key, $value ) {
		$this->attributes[ $key ] = $value;
	}

	public function is_setup_finished( $key ) {
		return $this->get( $key );
	}
	

	public static function get_param( $param, $default = null ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only to get current active tab.
		$value = isset( $_GET[ $param ] ) ? sanitize_text_field( wp_unslash( $_GET[ $param ] ) ) : '';
		return $value ? $value : $default;
	}
	public static function include_file( $path, $args = array() ) {
		extract( $args );
		$file = ELEX_MULTI_VENDOR_PLUGIN_PATH . $path;
		include_once $file;
	}
}
