<?php
namespace SabaiApps\Directories\Component\System\Helper;

use SabaiApps\Directories\Application;
use SabaiApps\Framework\Exception;

/**
 * CustomCssHelper provides various utility methods for custom CSS manipulation.
 */
class CustomCssHelper {

	/**
	 * Generates a custom CSS file based on settings.
	 *
	 * @param Application $application
	 * @param array|null  $settings
	 * @param array|null  $oldSettings
	 * @param bool        $force
	 * @throws Exception\RuntimeException
	 */
	public function generateFile( Application $application, ?array $settings = null, ?array $oldSettings = null, $force = false ) {
		$file              = $application->getPlatform()->getVarDir() . '/style.css';
		$file_no_bs_prefix = $application->getPlatform()->getVarDir() . '/style-no-bs-prefix.css';
		if ( ! isset( $settings ) ) {
			$settings = $application->getComponent( 'System' )->getConfig();
		}
		if ( ! $force
			&& isset( $oldSettings )
			&& file_exists( $file )
			&& file_exists( $file_no_bs_prefix )
		) {
			if ( $settings == array(
				'css'   => isset( $oldSettings['css'] ) ? $oldSettings['css'] : null,
				'color' => isset( $oldSettings['color'] ) ? $oldSettings['color'] : null,
			) ) {
				return; // no change so abort
			}
		}

		$custom_css = '';
		if ( strlen( $settings['css']['custom_css'] ) ) {
			$custom_css .= $settings['css']['custom_css'];
		}
		$colors = array();
		foreach ( array( 'primary', 'secondary', 'light', 'dark' ) as $color ) {
			if ( ! empty( $settings['color'][ $color ] ) ) {
				$colors[ $color ] = $this->_getCustomCssColors( $application, $settings['color'][ $color ] );
			}
		}
		foreach ( array(
			DRTS_BS_PREFIX => $file,
			''             => $file_no_bs_prefix,
		) as $prefix => $_file ) {
			$css = $custom_css;
			if ( ! empty( $colors ) ) {
				ob_start();
				include __DIR__ . '/_style.php';
				$css .= ob_get_clean();
			}
			if ( strlen( $css ) ) {
				if ( false === file_put_contents( $_file, $css ) ) {
					throw new Exception\RuntimeException( 'Error writing into file: ' . $_file . '. Make sure the file can be created and is writeable.' );
				}
			} else {
				@unlink( $_file );
			}
		}
	}

	/**
	 * Helper to get custom CSS color variations.
	 *
	 * @param Application $application
	 * @param string      $hex
	 * @return array
	 */
	protected function _getCustomCssColors( Application $application, $hex ) {
		$hsl = $application->System_Color_hexToHsl( $hex = substr( $hex, 1 ) );
		foreach ( array( -30, -20, -15, -12, -9, -7, -5, 5, 7, 9, 12, 15, 25, 30, 40 ) as $percent ) {
			$_hex = $application->System_Color_hslToHex( array( $hsl[0], $hsl[1], ( $hsl[2] * 100 + $percent ) / 100 ) );
			if ( $_hex === 'ffffff' ) {
				$_hex = $hex;
			}
			$ret[ $percent ] = '#' . $_hex;
			foreach ( $application->System_Color_hexToRgb( $_hex ) as $_rgb ) {
				$ret['rgb'][ $percent ][] = $_rgb * 255;
			}
		}
		$ret[0] = '#' . $hex;
		foreach ( $application->System_Color_hexToRgb( $hex ) as $_rgb ) {
			$ret['rgb'][0][] = $_rgb * 255;
		}

		return $ret;
	}
}
