<?php namespace BulkPriceEditor\PriceModifiers\Modifiers;

use BulkPriceEditor\PriceEditorPage\Widgets\Widget;
use WC_Product_Variable;
use WC_Product;

abstract class PriceModifier {
	
	private array $rawArgs = array();
	
	protected array $supports = array(
		'advanced_options' => false,
	);
	
	public function updatePrices( WC_Product $product ): void {
		
		if ( $product instanceof WC_Product_Variable ) {
			$children = $product->get_children();
			
			foreach ( $children as $child ) {
				$childProduct = wc_get_product( $child );
				$this->updatePrices( $childProduct );
			}
		}
		
		$regularPrice = $this->getUpdatedRegularPrice( $product );
		$salePrice    = $this->getUpdatedSalePrice( $product );
		
		if ( ! is_null( $regularPrice ) ) {
			$product->set_regular_price( $regularPrice );
		}
		
		if ( ! is_null( $salePrice ) ) {
			$product->set_sale_price( $salePrice );
		}
		
		$product->save();
	}
	
	abstract public function getType(): string;
	
	abstract public function getName();
	
	abstract public function getUpdatedSalePrice( WC_Product $product );
	
	abstract public function getUpdatedRegularPrice( WC_Product $product );
	
	abstract public function renderFields( Widget $widget );
	
	public function renderAdvancedFields( Widget $widget ) {}
	
	public function hadAdvancedOptions(): bool {
		return (bool) $this->supports['advanced_options'];
	}
	
	public function setRawArgs( array $rawArgs ): void {
		$this->rawArgs = $rawArgs;
	}
	
	public function getRawArgs(): array {
		return $this->rawArgs;
	}
	
}
