<?php

use Stichoza\GoogleTranslate\GoogleTranslate;
class scrapes
{ 
	private $Go_Curl;

    public function __construct()
    {
        $this->Go_Curl = new Go_Curl();
    }
	
	public function scrape_web_info($url)	{
		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;

		$arr				= array();
        $status = http_response($url);

        // Fetch main page
        $html = ($status === 403) ? scrapedo($url) : get_url($url);
        if (!$html || strlen($html) < 1000) {
            return ['error' => 'Failed to fetch page content'];
        }

		// 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', 'MOD', 'mod', 'Mod'],
			'',
			$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; 
		$h1_raw2			= str_replace(['APK', 'Apk', 'apk'], '', wp_strip_all_tags($this->Go_Curl->match('/<div class="app-name"><h1.*?><strong>(.*?)<\/strong>.*?<\/h1>/ms', $html, 1) ?? ''));
		//print_r($CariJudul);
		
		$arr['id_ps']		= $this->Go_Curl->match('/Google Play ID.*?<a.*?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($h1_raw2);
		}
		 
		
		//$titleId 			= $gp_id; 
		$arr['GP_ID'] 		= $titleId; 
		
		if ( !empty($titleId) ) {
			require_once get_template_directory().'/libs/addons/includes/playstore.php';
		}
		
        // Title cleaning
        $arr['title'] = trim(preg_replace(
            '/\s+/',
            ' ',
            str_replace(
                ['apkmody.io', 'apkmody.com', 'apkmody.mobi', 'apkmody', 'Apkmody', '+', 'Download for Android', 'Download', 'for', 'Android', 'download'],
                '',
                $this->Go_Curl->match('/<title>(.*?)<\/title>/ms', $html, 1) ?? ''
            )
        ));

        // Bracket swap ( ) → [ ]
        $arr['title'] = str_replace(['(', ')'], ['[', ']'], str_replace(['[', ']'], ['(', ')'], $arr['title']));

        // Alternative title from h1
        $arr['title_sc'] = trim(str_replace(
            ['apkmody.io', 'apkmody.com', 'apkmody.mobi', 'apkmody', 'Apkmody', 'APK', '-', 'MOD', '+', 'Download', 'download'],
            '',
            wp_strip_all_tags($this->Go_Curl->match('/<div class="app-name"><h1.*?>(.*?)<\/h1>/ms', $html, 1) ?? '')
        ));
        $arr['title_sc'] = str_replace(['(', ')'], ['[', ']'], str_replace(['[', ']'], ['(', ')'], $arr['title_sc']));

        foreach (['title', 'title_sc'] as $key) {
            $arr[$key] = trim(preg_replace('/\s+/', ' ', str_replace(['download', 'for Android'], '', $arr[$key])));
        }

        // Basic metadata
        $arr['poster_sc']    = $this->Go_Curl->match('/<div class="app-icon">.*?<img.*?src="(.*?)\=s.*?".*?>/ms', $html, 1);
        $arr['app_name']     = str_replace(['APK', 'Apk', 'apk'], '', wp_strip_all_tags($this->Go_Curl->match('/<div class="app-name"><h1.*?><strong>(.*?)<\/strong>.*?<\/h1>/ms', $html, 1) ?? ''));
        $arr['version_sc']   = str_replace('v', '', wp_strip_all_tags($this->Go_Curl->match('/<th.*?>Version.*?<td.*?><span.*?>(.*?)<\/span><\/td>/ms', $html, 1) ?? ''));
        $arr['size_sc']      = wp_strip_all_tags($this->Go_Curl->match('/<th.*?>Size.*?<td.*?>(.*?)<\/td>/ms', $html, 1) ?? '');
        $arr['genre_sc']     = wp_strip_all_tags($this->Go_Curl->match('/<th.*?>Category.*?<td.*?><span.*?><a.*?>(.*?)<\/a><\/span><\/td>/ms', $html, 1) ?? '');
        $arr['developer_sc'] = wp_strip_all_tags($this->Go_Curl->match('/<a href="#publisher-items">(.*?)<\/a>/ms', $html, 1) ?? '');
        $arr['updates']      = wp_strip_all_tags($this->Go_Curl->match('/<th.*?>Updated On.*?<td.*?><time.*?>(.*?)<\/time><\/td>/ms', $html, 1) ?? '');
        $arr['updated_sc']   = !empty($arr['updates']) ? date("M d, Y", strtotime($arr['updates'])) : '';

        // MOD Features
        $arr['mods'] = wp_strip_all_tags($this->Go_Curl->match('/<th.*?>MOD Features.*?<td.*?>(.*?)<\/td>/ms', $html, 1) ?? '');

        // Cleaned MOD block & article
        $arr['full_mod']       = $this->clean_mod_block($this->Go_Curl->match('/<div class="wp-block-group"><div class="wp-block-group__inner-container.*?">(.*?)<\/div><\/div>/ms', $html, 1) ?? '');
        $arr['article_sc']     = $this->clean_article_content($this->Go_Curl->match('/<div class="entry-block entry-content main-entry-content">(.*?)<div class="wp-container-flex-center font-size__small">/ms', $html, 1) ?? '');
        $arr['article_content']= $arr['article_sc'];
        $arr['summary_sc']     = wp_strip_all_tags(mb_substr($arr['article_content'], 0, 160, 'UTF-8')) . '...';

        // Canonical & download page
        $arr['canonical'] = $this->Go_Curl->match('/<link rel="canonical" href="(.*?)" \/>/ms', $html, 1);

        $download_page = $arr['canonical'] ? rtrim($arr['canonical'], '/') . '/download' : '';
        $download_page_alt = $this->Go_Curl->match('/<a class="btn btn-primary btn-block mb-3" href="(.*?)"/ms', $html, 1);

        if ($download_page_alt && filter_var($download_page_alt, FILTER_VALIDATE_URL)) {
            $download_page = $download_page_alt;
        }

        $arr['download_links_page'] = $download_page;

        if (!empty($download_page)) {
            $download_info = ($status === 403) ? scrapedo($download_page) : get_url($download_page);
            if ($download_info) {
                $this->extract_download_links($download_info, $arr);
            }
        }

        if (empty($arr['app_name']) && empty($arr['title'])) {
            $arr['error'] = 'No valid app data found';
        }

        return $arr;
    }

    private function clean_mod_block(string $html): string
    {
        if (empty($html)) return '';
        $html = preg_replace('/<figure[\s\S]*?<\/figure>/is', '', $html);
        $html = preg_replace('/<script.*?\/script>/s', '', $html);
        $html = preg_replace('/\s+(href|target|class|id|style|data-toggle|data-parent|dir)=[\'"][^\'"]*[\'"]/i', '', $html);
        $html = preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $html);
        $html = preg_replace('/<p[^>]*><\/p>/', '', $html);
        $html = str_replace(['<div>', '</div>', '<center>', '</center>', '<span>', '</span>', '&nbsp;'], '', $html);
        $html = preg_replace('/<a>(.*?)<\/a>/', '<b>$1</b>', $html);
        $html = str_replace(['<strong>', '</strong>'], ['<b>', '</b>'], $html);
        return trim(preg_replace('/\s+/', ' ', $html));
    }

    private function clean_article_content(string $html): string
    {
        if (empty($html)) return '';
        $html = preg_replace('/<div id="toc_container".*?>.*?<\/div>/s', '', $html);
        $html = preg_replace('/<div class="wp-block-image">.*?<\/div>/s', '', $html);
        $html = preg_replace('/<figure[\s\S]*?<\/figure>/is', '', $html);
        $html = preg_replace('/<img.*?>/s', '', $html);
        $html = preg_replace('/<!--.*?-->/s', '', $html);
        $html = preg_replace('/\s+(href|target|class|id|style)=[\'"][^\'"]*[\'"]/i', '', $html);
        $html = preg_replace('/<script.*?\/script>/s', '', $html);
        $html = preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $html);
        $html = preg_replace('/<p[^>]*><\/p>/', '', $html);
        $html = str_replace(['<div>', '</div>', '<center>', '</center>', '<span>', '</span>'], '', $html);
        $html = str_replace(['<strong>', '</strong>'], ['<b>', '</b>'], $html);
        return trim(preg_replace('/\s+/', ' ', $html));
    }

    private function extract_download_links(string $download_info, array &$arr): void
    {
        $section = $this->Go_Curl->match('/<div class="download-list.*?">(.*?)<\/article>/ms', $download_info, 1)
            ?? $this->Go_Curl->match('/<div class="download-list margin-top-10">(.*?)<\/article>/ms', $download_info, 1)
            ?? '';

        if (empty($section)) return;

        $arr['downloadlinks_gma']      = $this->Go_Curl->match_all('/<a href="(.*?)" class="clickable">/ms', $section, 1);
        $arr['name_downloadlinks_gma']  = $this->Go_Curl->match_all('/<div class="color__blue">(.*?)<\/div>/ms', $section, 1);
        $arr['size_downloadlinks_gma']  = $this->Go_Curl->match_all('/<span class="text-muted d-block ml-auto">(.*?)<\/span>/ms', $section, 1);
        $arr['types_downloadlinks_gma'] = array_map('trim', $this->Go_Curl->match_all('/<span class="app-tag">(.*?)<\/span>/ms', $section, 1));
        $arr['notes_downloadlinks_gma'] = $this->Go_Curl->match_all('/<div class="color__gray font-size__small">(.*?)<\/div>/ms', $section, 1);

        // Clean notes
        $arr['notes_downloadlinks_gma'] = array_map(function($note) {
            $note = preg_replace('/\s+(class|id|style)=[\'"][^\'"]*[\'"]/', '', $note);
            $note = str_replace(['<span>', '</span>'], ' ', $note);
            return trim(preg_replace('/\s+/', ' ', $note));
        }, $arr['notes_downloadlinks_gma']);

        $arr['apklinks'] = [];
        $arr['apkname']  = [];

        foreach ($arr['downloadlinks_gma'] as $i => $link_url) {
            $status = http_response($link_url);
            $page = ($status === 403) ? scrapedo($link_url) : get_url($link_url);

            if ($page) {
                $base64_link = $this->Go_Curl->match('/<a.*?data-href="(.*?)"/ms', $page, 1);
                $arr['apklinks'][$i] = $base64_link;
                $arr['apklink'][$i]  = $base64_link ? base64_decode($base64_link) : '';
                $arr['link_apk']     = $arr['apklink'][$i] ?? ''; // Direct link
                $arr['apkname'][$i]  = $this->Go_Curl->match('/<h1 class="h5 font-weight-semibold mb-3">(.*?)<\/h1>/ms', $page, 1) ?? '';
            }
        }
    }
    
}
