<?php

namespace WooSwatches;

class Modules {

	private $modules = array();

	private $settings = array();

	public $module_instances = array();

	public function __construct() {
		$this->settings = array();
		$this->register_modules();
		$this->load_modules();
		return $this;
	}

	public function get_settings() {
		return $this->settings;
	}

	public function get_all_modules( $name_only = false ) {
		$modules = $this->modules;
		if ( $name_only ) {
			return array_keys( $modules );
		}
		return $modules;
	}

	public function load_modules() {
		require_once ABSPATH . 'wp-admin/includes/file.php';

		$module_dir = plugin_dir_path( __FILE__ ) . 'modules';

		$module_files = list_files( $module_dir );

		$active_modules = $this->get_all_modules( true );

		if ( empty( $active_modules ) ) {
			return;
		}

		foreach ( $module_files as $module_file ) {
			$modules_name = wp_basename( $module_file, '.php' );
			if ( in_array( $modules_name, $active_modules ) ) {
				$this->load_module( $modules_name, $module_file );
			}
		}
	}

	public function load_module( $modules_name, $module_file ) {
		if ( file_exists( $module_file ) ) {
			$module_class = Helpers::get_class_name( $modules_name );

			require_once GS_WOO_SWATCHES_PLUGIN_DIR . 'includes/base/module.php';
			require_once $module_file;

			$namespace = '\WooSwatches\Modules\\';

			$module_class = $namespace . $module_class;
			$module       = new $module_class();

			$this->module_instances[ $modules_name ] = $module;
			$this->settings[ $modules_name ]         = $module->get_settings();
		}
	}

	public function save_settings( array $settings ) {
		foreach ( $settings as $modules_name => $module_settings ) {
			$module                          = $this->get_module( $modules_name );
			$module_settings                 = $module->save_settings( $module_settings );
			$this->settings[ $modules_name ] = $module->get_settings();
		}

		return $this->settings;
	}

	public function register_modules() {
		$modules = array(
			array(
				'name'  => 'general-settings',
				'path'  => '/',
				'title' => __( 'General', 'gs-woo-variation' ),
			),
			array(
				'name'  => 'advanced-settings',
				'path'  => 'advanced-settings',
				'title' => __( 'Advanced', 'gs-woo-variation' ),
			),
			array(
				'name'  => 'style-settings',
				'path'  => 'style-settings',
				'title' => __( 'Styles', 'gs-woo-variation' ),
			),
			array(
				'name'  => 'product-page-settings',
				'path'  => 'product-page-settings',
				'title' => __( 'Product Page', 'gs-woo-variation' ),
			),
			array(
				'name'  => 'archive-shop',
				'path'  => 'archive-shop-settings',
				'title' => __( 'Archive/Shop', 'gs-woo-variation' ),
			)
		);

		foreach ( $modules as &$module ) {
			$this->modules[ $module['name'] ] = $module;
		}
	}

	public function get_module( $module_name ) {
		return $this->module_instances[ $module_name ];
	}

	public function get_module_settings( $module_name ) {
		
		return $this->get_module( $module_name )->get_settings();
	}
}
