<?php namespace BulkPriceEditor\PriceModifiers\Modifiers;

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

class RemovePrice extends PriceModifier {
	
	const TYPE = 'remove_prices';
	
	public function getName(): string {
		return __( 'Remove prices', 'bulk-price-editor' );
	}
	
	public function getType(): string {
		return self::TYPE;
	}
	
	public function getUpdatedRegularPrice( WC_Product $product ): ?string {
		
		$removePrices = $this->getRawArgs()['bpe_remove_prices'] ?? '';
		
		if ( 'both' === $removePrices || 'regular_price' === $removePrices ) {
			return 'N/A';
		}
		
		return null;
	}
	
	public function getUpdatedSalePrice( WC_Product $product ): ?string {
		$removePrices = $this->getRawArgs()['bpe_remove_prices'] ?? '';
		
		if ( 'both' === $removePrices || 'sale_price' === $removePrices ) {
			return 'N/A';
		}
		
		return null;
	}
	
	public function renderFields( Widget $widget ) {
		
		$widget->renderHint( __( 'Select what prices you want to remove (regular, sale, or both) from the selected products.',
			'bulk-price-editor' ) );
		
		$widget->renderSelect( array(
			'id'                => 'bpe_remove_prices',
			'label'             => __( 'Remove price', 'bulk-price-editor' ),
			'options'           => array(
				'both'          => __( 'Both regular and sale prices', 'bulk-price-editor' ),
				'regular_price' => __( 'Regular price', 'bulk-price-editor' ),
				'sale_price'    => __( 'Sale price', 'bulk-price-editor' ),
			),
			'custom_attributes' => array(
				'data-price-modificator' => 'yes',
			),
		) );
	}
	
	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 ( 'N/A' === $regularPrice ) {
			$product->set_regular_price( '' );
		}
		
		if ( 'N/A' === $salePrice ) {
			$product->set_sale_price( '' );
		}
		
		$product->save();
	}
}
