<?php
namespace FileBird\Model;

use FileBird\Utils\Singleton;
use FileBird\Admin\Settings;

defined( 'ABSPATH' ) || exit;

class SettingModel {
  	use Singleton;
	private $settings = array();
	private $config   = array();

	public function __construct() {
		$this->initialize();
		$this->settings = $this->loadSettings();

		add_filter( 'fbv_data', array( $this, 'addUserSettingsData' ), 10, 1 );
	}

	public function initialize() {
		$this->config = array(
			'USER_MODE'           => array(
				'get' => 'getUserMode',
				'set' => 'setUserMode',
			),
			'SVG_SUPPORT'         => array(
				'get' => 'getSvgSupport',
				'set' => 'setSvgSupport',
			),
			'IS_SEARCH_USING_API' => array(
				'get' => 'getFolderSearchMethod',
				'set' => 'setFolderSearchMethod',
			),
			'enable_cache_optimization' => array(
				'get' => 'getEnableCacheOptimization',
				'set' => 'setEnableCacheOptimization',
			),
		);
	}

	public function addUserSettingsData( $data ) {
		$data['user_settings'] = array_merge( $data['user_settings'], $this->settings );

		return $data;
	}

	public function loadSettings() {
		foreach ( $this->config as $key => $value ) {
			$this->settings[ $key ] = $this->{$value['get']}();
		}

		return $this->settings;
	}

	public function get( $key ) {
		if ( in_array( $key, array_keys( $this->config ) ) ) {
			return $this->settings[ $key ];
		}
	}

	public function setSettings( $params ) {
		foreach ( $params as $key => $value ) {
			if ( isset( $this->config[ $key ] ) ) {
				$this->{$this->config[ $key ]['set']}( $value );
			}
		}
	}

	public function getUserMode() {
		return get_option( 'njt_fbv_folder_per_user', '0' ) === '1';
	}

	public function setUserMode( $value ) {
		update_option( 'njt_fbv_folder_per_user', $value );
	}

	public function getSvgSupport() {
		return get_option( 'njt_fbv_allow_svg_upload', '0' ) === '1';
	}

	public function setSvgSupport( $value ) {
		update_option( 'njt_fbv_allow_svg_upload', $value );
	}

	public function getFolderSearchMethod() {
		return get_option( 'njt_fbv_is_search_using_api', '0' ) === '1';
	}

	public function setFolderSearchMethod( $value ) {
		update_option( 'njt_fbv_is_search_using_api', $value );
	}

	public function getEnableCacheOptimization() {
		$settings = (array) get_option( 'fbv_settings', array() );
		return isset( $settings['enable_cache_optimization'] ) ? (string) $settings['enable_cache_optimization'] : "0";
	}

	public function setEnableCacheOptimization( $value ) {
		$settings = (array) get_option( 'fbv_settings', array() );
		$settings['enable_cache_optimization'] = $value;
		update_option( 'fbv_settings', $settings );
	}
}
