<?php 
use Stichoza\GoogleTranslate\GoogleTranslate;

class scrapes 
{
	private $Go_Curl;

    public function __construct()
    {
        $this->Go_Curl = new Go_Curl();
    }
	
	public function scrape_web_info($sources) {
		require_once get_template_directory() . '/libs/addons/inc/ssl.php';
		
		@ini_set('user_agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');

		 
		global $opt_themes; 
		
		if ($opt_themes['bahasa_on']) {
			$tr				= new GoogleTranslate();
			$bahasa			= $opt_themes['bahasa'];
		}

		$url			= $_POST['wp_url'];

		$arr			= array();

		//$links		= scrape_site($url);	
		$html			= file_get_html($url);
		//$links		= file_get_contents($url, false, stream_context_create($ssl));
		//$links		= $this->Go_Curl->geturl($url);	 

		// Asumsikan $html sudah berisi konten halaman dari Go_Curl

		// 1. Ekstrak isi <title> (full title)
		$raw_titles = $this->Go_Curl->match('/<title>(.*?)<\/title>/msi', $html, 1);

		if (empty($raw_titles)) {
			// fallback jika regex gagal
			$raw_titles = '';
		}

		// 2. Cleaning agresif tapi aman
		$raw_titles = str_replace(
			['download', 'Download', 'DOWNLOAD', 'Downloads', 'downloads', 
			 'apk', 'Apk', 'APK', 'Apks', 'APKS'],
			'',
			$raw_titles
		);

		$clean_titles = $raw_titles;

		// Hapus prefix "Download " (case insensitive)
		$clean_titles = preg_replace('/^download\s+/i', '', $clean_titles);

		// Hapus suffix " for Android" (case insensitive, termasuk variasi spasi)
		$clean_titles = preg_replace('/\s+for\s+Android$/i', '', $clean_titles);

		// Hapus kata-kata tidak perlu: download, apk, mod, dll.
		
		//$clean_titles = preg_replace('/\b(download|downloads|apk|apks|mod|hack|premium|pro|full|unlimited)\b\s*/i', ' ', $clean_titles);

		// Hapus semua isi dalam kurung beserta kurungnya ((), [], {}, <>)
		$clean_titles = preg_replace('/\s*[\(\[\{<].*?[\)\]\}>]\s*/i', ' ', $clean_titles);

		// Hapus versi angka (1.9.12, v1.0, 2.0.1, dll.)
		$clean_titles = preg_replace('/\b(v?[\d\.\_]+)\b/i', ' ', $clean_titles);

		// Hapus tanda hubung akhir atau garbage lain (misal: " - 5play.org")
		$clean_titles = preg_replace('/\s*[-–—].*$/', '', $clean_titles);

		// Bersihkan spasi berlebih dan trim
		$clean_titles = trim(preg_replace('/\s+/', ' ', $clean_titles));

		// Hasil akhir: $clean_titles siap dipakai untuk search Play Store ID
		 

		$CariJudul 			= $clean_titles; 
		
		//print_r($CariJudul);
		
		
		/*
		id info
		*/
		$arr['id_ps']		= $this->Go_Curl->match('/<a class="ga" href="(.*?)".*?>/ms', $html, 1);
		$arr['id_ps']		= $this->Go_Curl->validate_gplay_id($arr['id_ps']);

		$arr['title_id']	= $arr['id_ps'];
		$arr['title_id_2']	= $arr['id_ps'];
		
		 
        $gp_id 				= !empty($arr['id_ps']) ? $arr['id_ps'] : ($arr['title_id'] ?? $arr['title_id_2'] ?? '');
		
		
		if ( !empty($gp_id) ) {			
			$titleId		= $gp_id; 
		} else {			  
			$titleId 		= $this->Go_Curl->get_playstore_id_by_title_google($CariJudul);
		}
		 
		//$titleId		= $gp_id;  
		$arr['GP_ID'] 		= $titleId; 
		
		if ( !empty($titleId) ) {
			require_once get_template_directory().'/libs/addons/includes/playstore.php';
		}
		
		/*
		data info
		*/
		// === Improved Title Extraction for game4n.net (or similar MOD APK sites) ===

		// Primary: Use Open Graph title (often cleanest and most reliable)
		$og_title_raw = $this->Go_Curl->match( '/<meta property="og:title" content="(.*?)"/msi', $html, 1 );
		$arr['title'] = trim( strip_tags( $og_title_raw ) );

		// Fallback 1: Page <title> tag
		if ( empty( $arr['title'] ) ) {
			$page_title_raw = $this->Go_Curl->match( '/<title>(.*?)<\/title>/msi', $html, 1 );
			$arr['title'] = trim( strip_tags( $page_title_raw ) );
		}

		// Fallback 2: Whatever you had in $arr['title3'] (e.g., from h1 or other)
		if ( empty( $arr['title'] ) ) {
			$arr['title'] = $arr['title3'] ?? '';
		}

		// Normalize whitespace everywhere
		$arr['title'] = preg_replace( '/\s+/', ' ', $arr['title'] );

		// Secondary title source (from <title> tag, cleaned differently)
		$title_sc_raw = $this->Go_Curl->match( '/<title>(.*?)<\/title>/msi', $html, 1 );
		$arr['title_sc'] = trim( strip_tags( $title_sc_raw ) );

		// Comprehensive cleaning function
		$clean_title = function( $text ) {
			if ( empty( $text ) ) {
				return '';
			}

			// Decode entities first
			$text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5 );

			// Remove common unwanted site-specific junk
			$unwanted = [
				'GAME4N', 'game4n', 'Game4N',
				'for Android', 'For Android',
				'Download', 'download', 'Free Download',
				'MOD APK', 'Mod Apk', 'Premium', 'Pro', 'Unlocked',
				'-', '–', '—', '|', '::'
			];
			$text = str_ireplace( $unwanted, ' ', $text );

			// Normalize separators and whitespace
			$text = preg_replace( '/\s*[-–—|•:\/]\s*/', ' ', $text );
			$text = preg_replace( '/\s+/', ' ', $text );

			return trim( $text );
		};

		$arr['title']    = $clean_title( $arr['title'] );
		$arr['title_sc'] = $clean_title( $arr['title_sc'] );

		// Standardize brackets for MOD features ( [] is common standard)
		$arr['title'] = str_replace( ['(', ')'], ['[', ']'], $arr['title'] );

		// For title_sc: Use commas as separators if it contains version/mod lists
		$arr['title_sc'] = str_replace( ['(', ')', '/'], ', ', $arr['title_sc'] );
		$arr['title_sc'] = preg_replace( '/,\s*,/', ',', $arr['title_sc'] ); // Clean double commas
		$arr['title_sc'] = trim( $arr['title_sc'], " ,-" );

		// Final removal of stray "download"
		$arr['title']    = str_ireplace( ' download ', ' ', $arr['title'] );
		$arr['title_sc'] = str_ireplace( ' download ', ' ', $arr['title_sc'] );

		$arr['title']    = trim( $arr['title'] );
		$arr['title_sc'] = trim( $arr['title_sc'] );

		$arr['poster_sc']			= $this->Go_Curl->match('/<img width="96" height="96" class="rounded-lg wp-post-image lozad".*?data-src="(.*?)".*?>/ms', $html, 1);
 
 
		$arr['app_name']			= $this->Go_Curl->match('/<nav class="breadcrumbs" aria-label="Breadcrumb">.*?<span>(.*?)\ Mod.*?<\/span>/ms', $html, 1);
		
		$arr['genre_sc']			= $this->Go_Curl->match('/<dt class="additional-title">Category.*?<dd class="additional-info"><a.*?>(.*?)<\/a><\/dd>/ms', $html, 1);
		
		$arr['version_sc']			= trim($this->Go_Curl->match('/<dt class="additional-title">Latest Version.*?<dd.*?>(.*?)<\/dd>/ms', $html, 1));
		
		$arr['size_sc']				= $this->Go_Curl->match('/<a class="download-btn".*><i class="download-icon home-icons"><\/i>Download .?\((.*?)\).?<\/a>/ms', $html, 1);
		
		$arr['developer_sc']		= $this->Go_Curl->match('/<p class="title">Uploaded by.*?<p class="additional-info">(.*?)<\/p>/ms', $html, 1);
		
		$arr['updated_sc']			= $this->Go_Curl->match('/<time class="pdt-info-data">(.*?)<\/time>/ms', $html, 1);
		
		$arr['updated_sc'] 			= date("M d, Y", strtotime($arr['updated_sc']));
		
		// === Improved MOD Features Extraction (Robust & Clean) ===

		// Primary: Try to extract text inside parentheses from the main <h1> title
		$mods_raw = $this->Go_Curl->match( '/<div class="pdt-info">.*?<h1[^>]*>(.*?)<\/h1>/msi', $html, 1 );

		if ( ! empty( $mods_raw ) ) {
			// Decode entities first (e.g., &#8211; → –)
			$mods_raw = html_entity_decode( $mods_raw, ENT_QUOTES | ENT_HTML5 );

			// Extract content inside the last pair of parentheses (common for MOD features)
			if ( preg_match( '/\(([^)]*)\)$/', trim( $mods_raw ), $matches ) ) {
				$arr['mods'] = trim( $matches[1] );

				// Clean up inside the brackets
				$arr['mods'] = preg_replace( '/\s+/', ' ', $arr['mods'] ); // normalize spaces
				$arr['mods'] = trim( $arr['mods'], " ,;|•-–" );           // remove trailing separators
			} else {
				$arr['mods'] = '';
			}
		} else {
			$arr['mods'] = '';
		}

		// Fallback: Look for a dedicated MOD features section (common on many sites)
		if ( empty( $arr['mods'] ) ) {
			// Example: <h2>MOD Features</h2> followed by list or paragraph
			$fallback_mods = $this->Go_Curl->match( '/<h2[^>]*>MOD Features?<\/h2>\s*<[^>]+>(.*?)<\/[^>]+>/msi', $html, 1 );
			if ( ! empty( $fallback_mods ) ) {
				$arr['mods'] = trim( strip_tags( $fallback_mods ) );
				$arr['mods'] = preg_replace( '/\s+/', ' ', $arr['mods'] );
			}
		}

		// Final fallback: Extract from og:title or page title if it contains [ ] or ( )
		if ( empty( $arr['mods'] ) && ! empty( $arr['title'] ) ) {
			if ( preg_match( '/[\(\[]([^\)\]]+)[\)\]]$/', $arr['title'], $m ) ) {
				$arr['mods'] = trim( $m[1] );
				$arr['mods'] = preg_replace( '/\s+/', ' ', $arr['mods'] );
			}
		}
		
		//print_r($arr['mods']);
		/*
		<div class="pdt-info">.*?<h1>(.*?)<\/h1>
		 <section class="info-box">.*?<h2>.*?Mod Info<\/h2>.*?<div class="info-desc">(.*?)<\/div>.*?<\/section>
		*/
		
		//$arr['mods1']				= $this->Go_Curl->match('/<section class="info-box">.*?<h2>.*?Mod Info<\/h2>.*?<div class="info-desc">(.*?)<\/div>.*?<\/section>/ms', $html, 1);
		//$arr['mods2']				= str_replace(array('APK Home', 'Android', '-', 'Download'), '', $this->Go_Curl->match('/<h1 class="new-pdt-app-title">.*?\[(.*?)]<\/h1>/ms', $html, 1));
		//if ($arr['mods'] === FALSE or $arr['mods'] == '') $arr['mods'] = $arr['mods2'];


		//$arr['carimod']		= preg_match('/\[([^\]]*)\]/', $arr['title_sc'], $arr['hasilmod']);
		//$arr['mods']		= $arr['hasilmod'][1];
		
		/* https://www.happymod.cloud/zerowake-gates-bl-rpg/com.storytaco.p33client/ */
		$arr['full_mod_title']	= '';
		$arr['full_mod']		= $this->Go_Curl->match('/<section class="info-box">.*?<h2>.*?Mod Info<\/h2>.*?<div class="info-desc">(.*?)<\/div>.*?<\/section>/ms', $html, 1);
		$arr['full_mod'] 		= $this->clean_mod_content( $arr['full_mod'] ?? '' );
		
		
		$arr['article_sc']		= $this->Go_Curl->match('/<div class="content">(.*?)<div class="additional">/ms', $html, 1); 
		$arr['article_content'] = $this->clean_article_content( $arr['article_sc'] ?? '' );

		$arr['downloadlink']		= $sources."downloading.html";

		$arr['namedownloadlink']	= $this->Go_Curl->match('/<div class="pdt-info">.*?<h1>(.*?)<\/h1>/ms', $html, 1);
		$arr['downloadlinks_gma']		= $this->Go_Curl->match_all('/<a href="(.*?)">.*?<\/a>/ms', $this->Go_Curl->match('/<div class="vc_tta-panel-body">(.*?)<\/ul>/ms', $html, 1), 1);
		$arr['name_downloadlinks_gma']	= $this->Go_Curl->match_all('/<a.*?>(.*?)<\/a>/ms', $this->Go_Curl->match('/<div class="vc_tta-panel-body">(.*?)<\/ul>/ms', $html, 1), 1);
		
		// === Improved & Safe Translation Block for MOD Content ===

		if ( ! empty( $opt_themes['bahasa_on'] ) && isset( $tr ) ) {
			// Auto-detect source language for better accuracy (no need to set every time)
			$tr->setSource( null );
			// Target language already set earlier: $tr->setTarget( $bahasa );

			/**
			 * Safe translation function with HTML tag fix
			 */
			$translate_safely = function( $text ) use ( $tr ) {
				if ( empty( $text ) ) {
					return $text;
				}

				try {
					$translated = $tr->translate( $text );

					// If translation fails or returns nothing, fall back to original
					if ( empty( $translated ) ) {
						return $text;
					}

					// Fix common spacing issues around HTML tags caused by Google Translate
					$translated = str_replace(
						[ '< ', ' <', '< /', '</ ', ' >' ],
						[ '<',  '<',  '</', '</',  '>'  ],
						$translated
					);

					return $translated;
				} catch ( Exception $e ) {
					// Log if needed: error_log( 'Translation error: ' . $e->getMessage() );
					return $text; // Return original on error
				}
			};

			// Translate MOD features from title
			if ( ! empty( $arr['mods'] ) ) {
				$arr['mods'] = $translate_safely( $arr['mods'] );
			}

			// Translate full MOD description
			if ( ! empty( $arr['full_mod'] ) ) {
				$arr['full_mod'] = $translate_safely( $arr['full_mod'] );
			}

			// Translate main article content
			if ( ! empty( $arr['article_content'] ) ) {
				$arr['article_content'] = $translate_safely( $arr['article_content'] );
			}

			// Optional: Also translate summary, title, etc. if needed
			// if ( ! empty( $arr['summary_sc'] ) ) {
			//     $arr['summary_sc'] = $translate_safely( $arr['summary_sc'] );
			// }
		}

		/* 
		echo '<pre>';		
		print_r($arr['downloadlink_gma_'[$i]]);
		echo '</pre>';	
		*/

		if (empty($titleId)) {
			$arr['error']			= NO_ID;
			echo $arr['error'];
		}

		 

		return $arr;
	}
		
	/**
	 * Clean MOD description/content – removes junk, attributes, scripts, empty tags
	 * while preserving useful formatting (paragraphs, lists, bold, etc.)
	 *
	 * @param string $text Raw HTML content
	 * @return string Cleaned HTML ready for WordPress post
	 */
	private function clean_mod_content( $text )
	{
		if ( empty( $text ) ) {
			return '';
		}

		// 1. Decode HTML entities first (&nbsp;, &#8211;, etc.)
		$text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5 );
		$text = str_replace( '&nbsp;', ' ', $text );

		// 2. Remove scripts and comments entirely
		$text = preg_replace( '/<script[^>]*>.*?<\/script>/is', '', $text );
		$text = preg_replace( '/<!--.*?-->/s', '', $text );

		// 3. Remove ALL attributes (href, class, id, style, target, data-*, onclick, etc.)
		$text = preg_replace( '/\s+(href|target|class|id|style|data-[a-z-]+|onclick|onload|rel)=[\'"][^\'"]*[\'"]/', '', $text );

		// 4. Remove completely empty tags: <p></p>, <div class="x"> </div>, <span>   </span>, etc.
		// Repeat a few times to catch nested empties
		$text = preg_replace( '/<([^\/>]+)[^>]*>\s*<\/\1>/', '', $text );
		$text = preg_replace( '/<([^\/>]+)[^>]*>\s*<\/\1>/', '', $text ); // second pass

		// 5. Allow only safe, useful tags for formatting
		$allowed_tags = '<p><br><ul><ol><li><b><strong><em><i><blockquote><h2><h3><h4>';
		$text = strip_tags( $text, $allowed_tags );

		// 6. Standardize bold tags
		$text = str_replace( ['<strong>', '</strong>'], ['<b>', '</b>'], $text );

		// 7. Normalize whitespace (multiple spaces, tabs, newlines → single space)
		$text = preg_replace( '/\s+/', ' ', $text );

		// 8. Optional: Add line breaks after paragraphs and list items for better readability
		$text = preg_replace( '/<\/(p|li|blockquote)>/i', "</$1>\n", $text );

		// 9. Final trim
		return trim( $text );
	}

	/**
	 * Deep-clean scraped article content – removes images, ads, scripts,
	 * empty tags, all attributes, and normalizes whitespace.
	 *
	 * @param string $text Raw HTML from the source page
	 * @return string Safe, clean HTML ready for WordPress post content
	 */
	private function clean_article_content( $text )
	{
		if ( empty( $text ) ) {
			return '';
		}

		// 1. Decode entities first
		$text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5 );

		// 2. Remove scripts, comments, and images completely
		$text = preg_replace( '/<script[^>]*>.*?<\/script>/is', '', $text );
		$text = preg_replace( '/<!--.*?-->/s', '', $text );
		$text = preg_replace( '/<img[^>]*>/is', '', $text );

		// 3. Remove ALL attributes (href, class, id, style, target, data-*, onclick, etc.)
		$text = preg_replace( '/\s+(href|target|class|id|style|data-[a-z-]+|onclick|onload|rel|aria-[a-z]+)=[\'"][^\'"]*[\'"]/', '', $text );

		// 4. Remove empty tags (including nested ones) – run twice for safety
		$text = preg_replace( '/<([^\/>]+)[^>]*>\s*<\/\1>/is', '', $text );
		$text = preg_replace( '/<([^\/>]+)[^>]*>\s*<\/\1>/is', '', $text );

		// 5. Remove <div> tags completely (you usually don't want them in post content)
		$text = preg_replace( '/<\/?div[^>]*>/i', ' ', $text );

		// 6. Allow only safe formatting tags
		$allowed = '<p><br><ul><ol><li><b><strong><em><i><blockquote><h2><h3><h4><h5><h6>';
		$text = strip_tags( $text, $allowed );

		// 7. Standardize bold tags
		$text = str_replace( ['<strong>', '</strong>'], ['<b>', '</b>'], $text );

		// 8. Normalize whitespace & add proper line breaks
		$text = preg_replace( '/\s+/', ' ', $text );
		$text = preg_replace( '/<\/(p|li|h[2-6]|blockquote)>/i', "</$1>\n", $text );

		// 9. Final trim
		return trim( $text );
	}

}
