<?php
namespace WTC_CHECKOUT\Modules\Dashboard\Services;
defined( 'ABSPATH' ) || exit;
class DashboardStyleManager {
	private static $css_filename = 'dashboard-custom.css';
	private static $defaults = array(
		'width_mode'            => 'theme',
		'width_custom'          => '1200',
		'radius'                => '14',
		'btn_radius'            => '10',
		'card_radius'           => '14',
		'card_min_width'        => '175',
		'card_shadow'           => 'yes',
		'bg_color'              => '#F8F9FD',
		'sidebar_bg'            => '#FFFFFF',
		'card_bg'               => '#FFFFFF',
		'gfxcard_bg'            => '#FFFFFF',
		'text_primary'          => '#1A202C',
		'text_secondary'        => '#718096',
		'border_color'          => '#E2E8F0',
		'select_bg'              => '#FFFFFF',
		'select_text'            => '#1A202C',
		'btn-text-color'             => '#3B3B3B',
		'btn-hover-color'       => '#1A202C',
		'btn-hover-bg'         => '#F7F7F7',
		'btn-active-bg'        => '#3182CE',
		'btn-active-color'      => '#FFFFFF',
		'avatar-circle-bg'     => '#3182CE',
		'avatar-circle-bg-to'  => '#2b6cb0',
		'avatar-border'        => '#d8d8d8',
		'card_1_bg'             => '#FFFFFF',
		'card_1_icon_bg'        => '#EBF8FF',
		'card_1_icon'           => '#3182CE',
		'card_1_border'         => '#BEE3F8',
		'card_2_bg'             => '#FFFFFF',
		'card_2_icon_bg'        => '#FAF5FF',
		'card_2_icon'           => '#805AD5',
		'card_2_border'         => '#E9D8FD',
		'card_3_bg'             => '#FFFFFF',
		'card_3_icon_bg'        => '#F0FFF4',
		'card_3_icon'           => '#38A169',
		'card_3_border'         => '#C6F6D5',
		'card_4_bg'             => '#FFFFFF',
		'card_4_icon_bg'        => '#FFF5F5',
		'card_4_icon'           => '#E53E3E',
		'card_4_border'         => '#FED7D7',
	);
	public static function get_all_settings(): array {
		$settings = array();
		foreach ( self::$defaults as $key => $default ) {
			$option_name = 'wtc_dashboard_' . $key;
			$settings[ $key ] = get_option( $option_name, $default );
		}
		return $settings;
	}
	public static function get_defaults(): array {
		return self::$defaults;
	}
	public static function custom_css_exists(): bool {
		$file_path = self::get_css_file_path();
		return file_exists( $file_path );
	}
	public static function get_css_file_path(): string {
		$upload_dir = wp_upload_dir();
		return $upload_dir['basedir'] . '/wtc/' . self::$css_filename;
	}
	public static function regenerate_css(): bool {
		$enabled = get_option('wtc_dashboard_custom_styles_enabled', 'no');
		$upload_dir = wp_upload_dir();
		$dir_path   = $upload_dir['basedir'] . '/wtc';
		$file_path  = $dir_path . '/' . self::$css_filename;
		if ( 'yes' !== $enabled ) {
			if ( file_exists( $file_path ) ) {
				wp_delete_file( $file_path );
			}
			return true;
		}
		$css = self::generate_css_content();
		if ( ! file_exists( $dir_path ) ) {
			wp_mkdir_p( $dir_path );
		}
		global $wp_filesystem;
		if ( empty( $wp_filesystem ) ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
			WP_Filesystem();
		}
		$result = $wp_filesystem->put_contents( $file_path, $css, FS_CHMOD_FILE );
		return (bool) $result;
	}
	private static function get_user_value( $key ) {
		$val = get_option( 'wtc_dashboard_' . $key, false );
		if ( $val === '' ) return false;
		return $val;
	}
	private static function generate_css_content(): string {
		$css = "\n\n";
		$width_mode = get_option('wtc_dashboard_width_mode', self::$defaults['width_mode']);
		$width_custom = get_option('wtc_dashboard_width_custom', self::$defaults['width_custom']);
		$css  .= ".wtc-dashboard-container {\n";
		if ( 'custom' === $width_mode ) {
			$css  .= "    max-width: {$width_custom}px !important;\n";
		} else {
			$css  .= "    max-width: 100% !important;\n";
		}
		$bg_color = self::get_user_value('bg_color');
		$text_primary = self::get_user_value('text_primary');
		$text_secondary = self::get_user_value('text_secondary');
		if ($bg_color) 	    $css .= "    background-color: var(--wtc-bg) !important;\n";
		if ($text_primary)  $css .= "    color: var(--wtc-text-primary) !important;\n";
		if ($text_secondary) $css .= "    color: var(--wtc-text-secondary) !important;\n";
		$css  .= "}\n\n";
		$variables = array(
			'--wtc-bg'             => 'bg_color',
			'--wtc-sidebar-bg'     => 'sidebar_bg',
			'--wtc-card-bg'        => 'card_bg',
			'--wtc-gfxcard-bg'     => 'gfxcard_bg',
			'--wtc-text-primary'   => 'text_primary',
			'--wtc-text-secondary' => 'text_secondary',
			'--wtc-border-color'   => 'border_color',
			'--wtc-select-bg'       => 'select_bg',
			'--wtc-select-text'     => 'select_text',
			'--wtc-btn-text-color'             => 'btn-text-color',
			'--wtc-btn-hover-color'       => 'btn-hover-color',
			'--wtc-btn-hover-bg'         => 'btn-hover-bg',
			'--wtc-btn-active-bg'        => 'btn-active-bg',
			'--wtc-btn-active-color'      => 'btn-active-color',
			'--wtc-avatar-circle-bg'     => 'avatar-circle-bg',
			'--wtc-avatar-circle-bg-to'  => 'avatar-circle-bg-to',
			'--wtc-avatar-border'        => 'avatar-border',
			'--card-1-bg'             => 'card_1_bg',
			'--card-1-icon-bg'        => 'card_1_icon_bg',
			'--card-1-icon'           => 'card_1_icon',
			'--card-1-border'         => 'card_1_border',
			'--card-2-bg'             => 'card_2_bg',
			'--card-2-icon-bg'        => 'card_2_icon_bg',
			'--card-2-icon'           => 'card_2_icon',
			'--card-2-border'         => 'card_2_border',
			'--card-3-bg'             => 'card_3_bg',
			'--card-3-icon-bg'        => 'card_3_icon_bg',
			'--card-3-icon'           => 'card_3_icon',
			'--card-3-border'         => 'card_3_border',
			'--card-4-bg'             => 'card_4_bg',
			'--card-4-icon-bg'        => 'card_4_icon_bg',
			'--card-4-icon'           => 'card_4_icon',
			'--card-4-border'         => 'card_4_border',
		);
		$root_css = "";
		$dark_css = "";
		foreach ($variables as $css_var => $option_key) {
			$val = self::get_user_value($option_key);
			if ($val !== false) {
				$root_css .= "    {$css_var}: {$val} !important;\n";
				$dark_css .= "    {$css_var}: {$val} !important;\n";
			}
		}
		$radius = self::get_user_value('radius');
		if ($radius !== false) {
			$root_css .= "    --wtc-radius: {$radius}px !important;\n";
		}
		$btn_radius = self::get_user_value('btn_radius');
		if ($btn_radius !== false) {
			if (is_numeric($btn_radius)) $btn_radius .= 'px';
			$root_css .= "    --wtc-btn-radius: {$btn_radius} !important;\n";
		}
		$card_radius = self::get_user_value('card_radius');
		if ($card_radius !== false) {
			if (is_numeric($card_radius)) $card_radius .= 'px';
			$root_css .= "    --wtc-card-radius: {$card_radius} !important;\n";
		}
		$card_min_width = self::get_user_value('card_min_width');
		if ($card_min_width !== false) {
			if (is_numeric($card_min_width)) $card_min_width .= 'px';
			$root_css .= "    --wtc-card-min-width: {$card_min_width} !important;\n";
		}
		if (!empty($root_css)) {
			$css .= ":root {\n" . $root_css . "}\n\n";
		}
		if (!empty($dark_css)) {
			$css .= "[data-wtc-theme=\"dark\"] {\n" . $dark_css . "}\n\n";
		}
		$card_shadow = self::get_user_value('card_shadow');
		if ($card_shadow === 'no') {
			$css .= ".wtc-stat-card,\n";
			$css .= ".wtc-sidebar,\n";
			$css .= ".wtc-dash-header {\n";
			$css .= "    box-shadow: none !important;\n";
			$css .= "}\n\n";
		}
		return $css;
	}
	public static function reset_to_defaults(): bool {
		foreach ( self::$defaults as $key => $default ) {
			$option_name = 'wtc_dashboard_' . $key;
			delete_option( $option_name );
		}
		delete_option('wtc_dashboard_custom_styles_enabled');
		$file_path = self::get_css_file_path();
		if ( file_exists( $file_path ) ) {
			wp_delete_file( $file_path );
		}
		return true;
	}
	public static function save_settings( array $settings ): bool {
		foreach ( $settings as $key => $value ) {
			$option_name = 'wtc_dashboard_' . $key;
			if ( empty( $value ) && $value !== '0' ) {
				delete_option( $option_name );
			} else {
				update_option( $option_name, sanitize_text_field( $value ) );
			}
		}
		return self::regenerate_css();
	}
	public static function get_settings_groups(): array {
		return array(
			'general_ui' => array(
				'label'  => __( 'Genel Görünüm ve Yapı', 'wtc-checkout' ),
				'row'    => 1,
				'fields' => array(
					'width_mode'      => array(
						'label'   => __( 'Genişlik Modu', 'wtc-checkout' ),
						'type'    => 'select',
						'options' => array(
							'theme'  => __( 'Tema Genişliği (%100)', 'wtc-checkout' ),
							'custom' => __( 'Özel Genişlik', 'wtc-checkout' ),
						),
					),
					'width_custom'    => array(
						'label'       => __( 'Maksimum Genişlik', 'wtc-checkout' ),
						'type'        => 'number',
						'min'         => 600,
						'step'        => 1,
						'suffix'      => 'px',
						'conditional' => array( 'field' => 'width_mode', 'value' => 'custom' ),
					),
					'radius'          => array( 'label' => __( 'Arkaplan Köşe Yarıçap', 'wtc-checkout' ), 'type' => 'number', 'min' => 0, 'max' => 50, 'suffix' => 'px' ),
					'btn_radius'      => array( 'label' => __( 'Buton Köşe Yarıçap', 'wtc-checkout' ), 'type' => 'number', 'min' => 0, 'max' => 50, 'suffix' => 'px' ),
					'card_radius'     => array( 'label' => __( 'Kart Köşe Yarıçap', 'wtc-checkout' ), 'type' => 'number', 'min' => 0, 'max' => 50, 'suffix' => 'px' ),
					'card_min_width'  => array( 'label' => __( 'Kart Minimum Genişlik', 'wtc-checkout' ), 'type' => 'number', 'min' => 0, 'max' => 500, 'suffix' => 'px' ),
					'card_shadow'     => array( 'label' => __( 'Kart Gölge', 'wtc-checkout' ), 'type' => 'toggle' ),
				),
			),
			'page_colors' => array(
				'label'  => __( 'Sayfa ve Menü Renkleri', 'wtc-checkout' ),
				'row'    => 1,
				'fields' => array(
					'bg_color'            => array( 'label' => __( 'Sayfa Arkaplanı', 'wtc-checkout' ), 'type' => 'color' ),
					'gfxcard_bg'          => array( 'label' => __( 'Grafik Kart Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
					'card_bg'             => array( 'label' => __( 'Kart Arkaplan (Genel)', 'wtc-checkout' ), 'type' => 'color' ),
					'text_primary'        => array( 'label' => __( 'Ana Başlık Rengi', 'wtc-checkout' ), 'type' => 'color' ),
					'text_secondary'      => array( 'label' => __( 'İkincil Yazı Rengi', 'wtc-checkout' ), 'type' => 'color' ),
					'border_color'        => array( 'label' => __( 'Çerçeve Rengi', 'wtc-checkout' ), 'type' => 'color' ),
					'sidebar_bg'          => array( 'label' => __( 'Menü Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
					'btn-text-color'      => array( 'label' => __( 'Menü Yazı', 'wtc-checkout' ), 'type' => 'color' ),
					'btn-hover-color'     => array( 'label' => __( 'Menü Yazı (Hover)', 'wtc-checkout' ), 'type' => 'color' ),
					'btn-hover-bg'        => array( 'label' => __( 'Menü Arkaplan (Hover)', 'wtc-checkout' ), 'type' => 'color' ),
					'btn-active-bg'       => array( 'label' => __( 'Menü Aktif Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
					'btn-active-color'    => array( 'label' => __( 'Menü Aktif Yazı', 'wtc-checkout' ), 'type' => 'color' ),
					'select_bg'           => array( 'label' => __( 'Select Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
					'select_text'         => array( 'label' => __( 'Select Yazı', 'wtc-checkout' ), 'type' => 'color' ),
					'avatar-circle-bg'    => array( 'label' => __( 'Avatar Arkaplan (Başla)', 'wtc-checkout' ), 'type' => 'color' ),
					'avatar-circle-bg-to' => array( 'label' => __( 'Avatar Arkaplan (Bitir)', 'wtc-checkout' ), 'type' => 'color' ),
					'avatar-border'       => array( 'label' => __( 'Avatar Çerçeve', 'wtc-checkout' ), 'type' => 'color' ),
				),
			),
			'card_1' => array(
				'label'  => __( 'Kart 1: Harcama', 'wtc-checkout' ),
				'row'    => 2,
				'icon'   => 'cart',
				'fields' => array(
					'card_1_bg'      => array( 'label' => __( 'Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
					'card_1_border'  => array( 'label' => __( 'Çerçeve', 'wtc-checkout' ), 'type' => 'color' ),
					'card_1_icon'    => array( 'label' => __( 'İkon Rengi', 'wtc-checkout' ), 'type' => 'color' ),
					'card_1_icon_bg' => array( 'label' => __( 'İkon Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
				),
			),
			'card_2' => array(
				'label'  => __( 'Kart 2: Siparişler', 'wtc-checkout' ),
				'row'    => 2,
				'icon'   => 'list',
				'fields' => array(
					'card_2_bg'      => array( 'label' => __( 'Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
					'card_2_border'  => array( 'label' => __( 'Çerçeve', 'wtc-checkout' ), 'type' => 'color' ),
					'card_2_icon'    => array( 'label' => __( 'İkon Rengi', 'wtc-checkout' ), 'type' => 'color' ),
					'card_2_icon_bg' => array( 'label' => __( 'İkon Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
				),
			),
			'card_3' => array(
				'label'  => __( 'Kart 3: Puanlar', 'wtc-checkout' ),
				'row'    => 2,
				'icon'   => 'star',
				'fields' => array(
					'card_3_bg'      => array( 'label' => __( 'Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
					'card_3_border'  => array( 'label' => __( 'Çerçeve', 'wtc-checkout' ), 'type' => 'color' ),
					'card_3_icon'    => array( 'label' => __( 'İkon Rengi', 'wtc-checkout' ), 'type' => 'color' ),
					'card_3_icon_bg' => array( 'label' => __( 'İkon Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
				),
			),
			'card_4' => array(
				'label'  => __( 'Kart 4: Kuponlar', 'wtc-checkout' ),
				'row'    => 2,
				'icon'   => 'ticket',
				'fields' => array(
					'card_4_bg'      => array( 'label' => __( 'Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
					'card_4_border'  => array( 'label' => __( 'Çerçeve', 'wtc-checkout' ), 'type' => 'color' ),
					'card_4_icon'    => array( 'label' => __( 'İkon Rengi', 'wtc-checkout' ), 'type' => 'color' ),
					'card_4_icon_bg' => array( 'label' => __( 'İkon Arkaplan', 'wtc-checkout' ), 'type' => 'color' ),
				),
			),
		);
	}
}