<?php

namespace Brandy\Niches;

use Brandy\Admin\PostEditor\PostMetaServices;
use Brandy\Niches\Import\PostImporter;
use Brandy\Niches\Import\MenuImporter;
use Brandy\Niches\Import\CommerceImporter;
use Brandy\Niches\Import\PageImporter;
use Brandy\Niches\Import\TemplateSettingsApplier;
use Brandy\Niches\Import\SampleDataCleaner;

class NicheService {

	/**
	 * Lazily-built post importer.
	 *
	 * @var PostImporter
	 */
	private $post_importer;

	/**
	 * Lazily-built menu importer.
	 *
	 * @var MenuImporter
	 */
	private $menu_importer;

	/**
	 * Lazily-built commerce importer.
	 *
	 * @var CommerceImporter
	 */
	private $commerce_importer;

	/**
	 * Lazily-built page importer.
	 *
	 * @var PageImporter
	 */
	private $page_importer;

	/**
	 * Lazily-built template settings applier / resetter.
	 *
	 * @var TemplateSettingsApplier
	 */
	private $settings;

	/**
	 * Lazily-built sample-data cleaner.
	 *
	 * @var SampleDataCleaner
	 */
	private $cleaner;

	public $selected_niche;

	/**
	 * Contains PostMetaServices service
	 *
	 * @var PostMetaServices
	 */
	public $post_meta_services;

	public $selected_style;

	public function __construct( $niche_id, $style = '' ) {

		$this->post_meta_services = PostMetaServices::get_instance();
		$this->selected_niche     = NicheLoader::get_instance()->get_niche( $niche_id, $style );
		$this->selected_style     = $style;
	}

	private function post_importer() {
		if ( null === $this->post_importer ) {
			$this->post_importer = new PostImporter( $this->selected_niche );
		}
		return $this->post_importer;
	}

	private function menu_importer() {
		if ( null === $this->menu_importer ) {
			$this->menu_importer = new MenuImporter( $this->selected_niche );
		}
		return $this->menu_importer;
	}

	private function commerce_importer() {
		if ( null === $this->commerce_importer ) {
			$this->commerce_importer = new CommerceImporter( $this->selected_niche );
		}
		return $this->commerce_importer;
	}

	private function page_importer() {
		if ( null === $this->page_importer ) {
			$this->page_importer = new PageImporter( $this->selected_niche, $this->post_meta_services );
		}
		return $this->page_importer;
	}

	private function settings() {
		if ( null === $this->settings ) {
			$this->settings = new TemplateSettingsApplier( $this->selected_niche );
		}
		return $this->settings;
	}

	private function cleaner() {
		if ( null === $this->cleaner ) {
			$this->cleaner = new SampleDataCleaner( $this->selected_niche );
		}
		return $this->cleaner;
	}

	public function install_niche( array $allowed_content, $builder = 'gutenberg' ) {

		$result = array();

		if ( empty( $this->selected_niche ) ) {
			throw new \Error( 'Niche not found' );
		}

		update_option( 'brandy_current_niche', $this->selected_niche['id'] );

		update_option( 'brandy_current_niche_style', $this->selected_style ?? '' );

		if ( in_array( 'clean_data', $allowed_content, true ) ) {
			$this->cleaner()->clean();
		}

		if ( in_array( 'pages', $allowed_content, true ) ) {
			$result['pages'] = $this->page_importer()->import( $builder );
		}

		if ( in_array( 'posts', $allowed_content, true ) ) {
			$this->settings()->reset_data();
			$this->settings()->install_template();

			$result['posts'] = $this->post_importer()->import( $builder );
		}

		if ( function_exists( 'wc_set_time_limit' ) && ( in_array( 'products', $allowed_content, true ) || in_array( 'product_categories', $allowed_content, true ) ) ) {
			\wc_set_time_limit( 0 );
		}

		if ( in_array( 'products', $allowed_content, true ) ) {
			$result['products'] = $this->commerce_importer()->import_products();
		}

		if ( in_array( 'product_categories', $allowed_content, true ) ) {
			$result['product_categories'] = $this->commerce_importer()->import_categories();
		}

		if ( in_array( 'menus', $allowed_content, true ) ) {
			$result['menus'] = $this->menu_importer()->import();
		}

		if ( in_array( 'widgets', $allowed_content, true ) ) {
			$this->menu_importer()->import_widgets();
		}

		return $result;
	}

}
