<?php

namespace HulkPlugins\Woo\ProductDescriptionInLoop\Admin\Services;

use HulkPlugins\Woo\ProductDescriptionInLoop\Core\SingletonTrait;

defined( 'ABSPATH' ) || exit;

class SettingsService {

	use SingletonTrait;

	public function getDescriptionPositionsOptions(): ?array {
		static $cache = null;

		if ( $cache !== null ) {
			return $cache;
        }

		$cache = [
			'before_product_title'  => esc_attr__( 'Before product title', 'product-description-in-loop-for-woocommerce' ),
			'after_product_title'   => esc_attr__( 'After product title', 'product-description-in-loop-for-woocommerce' ),
			'before_product_rating' => esc_attr__( 'Before product rating', 'product-description-in-loop-for-woocommerce' ),
			'after_product_rating'  => esc_attr__( 'After product rating', 'product-description-in-loop-for-woocommerce' ),
			'before_product_price'  => esc_attr__( 'Before product price', 'product-description-in-loop-for-woocommerce' ),
			'after_product_price'   => esc_attr__( 'After product price', 'product-description-in-loop-for-woocommerce' ),
			'before_add_to_cart'    => esc_attr__( 'Before product "Add to cart" button', 'product-description-in-loop-for-woocommerce' ),
			'after_add_to_cart'     => esc_attr__( 'After product "Add to cart" button', 'product-description-in-loop-for-woocommerce' ),
		];

		return $cache;
	}

	public function getDescriptionContentOptions(): ?array {
		static $cache = null;

		if ( $cache !== null ) {
			return $cache;
		}

		$cache = [
			'product_short_description'  => esc_attr__( 'Product short description', 'product-description-in-loop-for-woocommerce' ),
			'product_detail_description' => esc_attr__( 'Product detail description', 'product-description-in-loop-for-woocommerce' ),
			'loop_description'           => esc_attr__( 'Loop description', 'product-description-in-loop-for-woocommerce' ),
		];

		return $cache;
	}

	public function getTextAlignOptions(): ?array {
		static $cache = null;

		if ( $cache !== null ) {
			return $cache;
		}

		$cache = [
			'left'   => esc_attr__( 'Left', 'product-description-in-loop-for-woocommerce' ),
			'right'  => esc_attr__( 'Right', 'product-description-in-loop-for-woocommerce' ),
			'center' => esc_attr__( 'Center', 'product-description-in-loop-for-woocommerce' ),
		];

		return $cache;
	}

	public function getSavedSettings(): ?array {
		static $cache = null;

		if ( $cache !== null ) {
			return $cache;
		}

		$cache = [
			'enabled_pages'        => (array) get_option( 'hulk_woo_pdil_enabled_pages', [ 'shop_page' ] ),
			'description_position' => (string) get_option( 'hulk_woo_pdil_description_position', 'after_product_title' ),
			'description_content'  => (string) get_option( 'hulk_woo_pdil_description_content', 'product_short_description' ),
			'word_limit'           => (int) get_option( 'hulk_woo_pdil_word_limit', 0 ),
			'enable_style'         => (bool) get_option( 'hulk_woo_pdil_enable_style', true ),
			'text_align'           => (string) get_option( 'hulk_woo_pdil_text_align', 'center' ),
			'font_size'            => (int) get_option( 'hulk_woo_pdil_font_size', 16 ),
			'text_color'           => (string) get_option( 'hulk_woo_pdil_text_color' ),
		];

		return $cache;
	}

	/**
	 * @return array{
	 *     shop_page: string,
	 *     archive_pages: string,
	 *     category_pages: string,
	 *     tag_pages: string,
	 * }
	 */
	public function getEnabledPages(): array {
		return (array) $this->getSavedSettings()['enabled_pages'];
	}

	public function getDescriptionPosition(): string {
		return (string) $this->getSavedSettings()['description_position'];
	}

	public function getDescriptionContent(): string {
		return (string) $this->getSavedSettings()['description_content'];
	}

	public function getWordLimit(): int {
		return (int) $this->getSavedSettings()['word_limit'];
	}

	public function isEnableStyle(): bool {
		return (bool) $this->getSavedSettings()['enable_style'];
	}

	public function getTextAlign(): string {
		return (string) $this->getSavedSettings()['text_align'];
	}

	public function getFontSize(): int {
		return (int) $this->getSavedSettings()['font_size'];
	}

	public function getTextColor(): string {
		return (string) $this->getSavedSettings()['text_color'];
	}

	public function getWooLinks(): ?array {
		static $cache = null;

		if ( $cache !== null ) {
			return $cache;
		}

		$cache = [
			'shop_url'     => $this->getWooShopUrl(),
			'category_url' => $this->getWooFirstCategoryUrl(),
			'tag_url'      => $this->getWooFirstTagUrl(),
		];

		return $cache;
	}

	public function getWooShopUrl(): string {
		$shopUrl = wc_get_page_permalink( 'shop' );
		return esc_url( $shopUrl );
	}

	public function getWooFirstCategoryUrl(): string {
		$categories = get_terms(
            [
				'taxonomy'   => 'product_cat',
				'parent'     => 0,
				'hide_empty' => false,
				'number'     => 1,
            ]
        );

		if ( ! empty( $categories[0] ) && ! is_wp_error( $categories ) ) {
			$url = get_term_link( $categories[0] );
			if ( ! is_wp_error( $url ) ) {
				return esc_url( $url );
			}
		}

		return '';
	}

	public function getWooFirstTagUrl(): string {
		$tags = get_terms(
			[
				'taxonomy'   => 'product_tag',
				'parent'     => 0,
				'hide_empty' => false,
				'number'     => 1,
			]
		);

		if ( ! empty( $tags[0] ) && ! is_wp_error( $tags ) ) {
			$url = get_term_link( $tags[0] );
			if ( ! is_wp_error( $url ) ) {
				return esc_url( $url );
			}
		}

		return '';
	}
}
