<?php
declare( strict_types=1 );

namespace FiboFilters\Product;

use FiboFilters\Integrations\Multilingual;
use WC_Product;

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

/**
 * Class Product
 */
class Product {
	/** @var int */
	private $product_id = 0;

	/** @var WC_Product */
	private $wc_product = null;

	/** @var string */
	private $lang_code = 'en';

	/** @var Multilingual */
	private $multilingual;

	/**
	 * @param \WP_Post|WC_Product|int $product Product post, object or ID.
	 * @param Multilingual            $multilingual Multilingual object.
	 */
	public function __construct( $product, Multilingual $multilingual ) {
		if ( ! empty( $product ) && is_object( $product ) && is_a( $product, 'WC_Product' ) ) {
			$this->product_id = $product->get_id();
			$this->wc_product = $product;
		}

		if ( ! empty( $product ) && is_object( $product ) && is_a( $product, 'WP_Post' ) ) {
			$this->product_id = absint( $product->ID );
			$obj              = wc_get_product( $product );
			if ( is_object( $obj ) && is_a( $obj, 'WC_Product' ) ) {
				$this->wc_product = $obj;
			}
		}

		if ( is_numeric( $product ) && ('product' === get_post_type( $product ) || 'product_variation' === get_post_type( $product )) ) {
			$this->product_id = absint( $product );
			$obj              = wc_get_product( $product );
			if ( is_object( $obj ) && is_a( $obj, 'WC_Product' ) ) {
				$this->wc_product = $obj;
			}
		}

		$this->multilingual = $multilingual;

		$this->set_language();
	}

	/**
	 * Set info about product language
	 */
	public function set_language(): void {
		if ( $this->is_valid() ) {
			if ( $this->is_type( 'variation' ) ) {
				$this->lang_code = $this->multilingual->get_post_language( $this->get_wc_product()->get_parent_id() );
			} else {
				$this->lang_code = $this->multilingual->get_post_language( $this->get_id() );
			}
		}
	}

	/**
	 * Get product ID (post_id)
	 */
	public function get_id(): int {
		return $this->product_id;
	}

	/**
	 * Get product language
	 */
	public function get_language(): string {
		if ( ! $this->is_valid() ) {
			return '';
		}

		return $this->lang_code;
	}

	/**
	 * Check, if class is initialized correctly
	 */
	public function is_valid(): bool {
		return is_a( $this->wc_product, 'WC_Product' );
	}

	/**
	 * WooCommerce product object
	 */
	public function get_wc_product(): WC_Product {
		return $this->wc_product;
	}

	/**
	 * Check product type
	 *
	 * @param string $type Product type.
	 *
	 * @return bool
	 */
	public function is_type( string $type ): bool {
		if ( ! $this->is_valid() ) {
			return false;
		}

		return $this->wc_product->is_type( $type );
	}

	/**
	 * Get product type
	 */
	public function get_type(): string {
		if ( ! $this->is_valid() ) {
			return '';
		}

		return $this->wc_product->get_type();
	}

	/**
	 * Check if the product is published and visible for catalog or search
	 */
	public function is_published_and_visible(): bool {
		$result = false;

		if ( ! $this->is_valid() ) {
			return $result;
		}

		if ( $this->get_wc_product()->get_status() === 'publish' ) {
			if ( in_array( $this->get_wc_product()->get_catalog_visibility(), [ 'visible', 'search' ], true ) ) {
				$result = true;
			}
		}

		return $result;
	}

	/**
	 * Check if the product can be indexed
	 */
	public function can_index(): bool {
		$can_index = true;

		if ( ! $this->is_valid() ) {
			$can_index = false;
		}

		return $can_index;
	}
}
