<?php

namespace App\Game\Services;

class IgtUserAgentParser
{
    private $userAgent;
    private $parsed = [];

    public function __construct($userAgent = null)
    {
        $this->userAgent = $userAgent ?? ($_SERVER['HTTP_USER_AGENT'] ?? '');
        $this->parse();
    }

    private function parse()
    {
        $ua = $this->userAgent;

        $this->parsed['IsCrawler'] = $this->isCrawler($ua) ? 'Y' : 'N';

        $this->parsed['BrowserName'] = $this->getBrowserName($ua);
        $this->parsed['BrowserVersion'] = $this->getBrowserVersion($ua);
        $this->parsed['BrowserVendor'] = $this->getBrowserVendor($this->parsed['BrowserName']);

        $this->parsed['PlatformName'] = $this->getPlatformName($ua);
        $this->parsed['PlatformVersion'] = $this->getPlatformVersion($ua);
        $this->parsed['PlatformVendor'] = $this->getPlatformVendor($this->parsed['PlatformName']);

        $this->parsed['HardwareName'] = $this->getHardwareName($ua);
        $this->parsed['HardwareModel'] = $this->getHardwareModel($ua);
        $this->parsed['HardwareFamily'] = $this->getHardwareFamily($this->parsed['HardwareName']);
        $this->parsed['HardwareVendor'] = $this->getHardwareVendor($ua);
        $this->parsed['OEM'] = $this->parsed['HardwareVendor'];

        $this->parsed['ScreenPixelsWidth'] = $this->getScreenWidth($ua);
        $this->parsed['ScreenPixelsHeight'] = $this->getScreenHeight($ua);
        $this->parsed['ScreenInchesDiagonal'] = $this->getScreenDiagonal($ua);
        $this->parsed['ScreenInchesWidth'] = '0';
        $this->parsed['ScreenInchesHeight'] = '0';

        $this->parsed['Popularity'] = $this->getPopularity($ua);
    }

    public function getDeviceConfig()
    {
        return $this->parsed;
    }

    private function isCrawler($ua)
    {
        $crawlers = [
            'bot', 'crawl', 'spider', 'slurp', 'mediapartners',
            'google', 'yahoo', 'bing', 'facebook', 'twitter'
        ];

        $uaLower = strtolower($ua);
        foreach ($crawlers as $crawler) {
            if (strpos($uaLower, $crawler) !== false) {
                return true;
            }
        }
        return false;
    }

    private function getBrowserName($ua)
    {
        if (preg_match('/Edg(e|A|iOS)\//', $ua)) {
            return 'Microsoft Edge';
        }
        if (preg_match('/Chrome\//', $ua) && !preg_match('/Chromium\//', $ua)) {
            return 'Chrome';
        }
        if (preg_match('/Safari\//', $ua) && !preg_match('/Chrome\//', $ua)) {
            if (preg_match('/Mobile\//', $ua)) {
                return 'Mobile Safari';
            }
            return 'Safari';
        }
        if (preg_match('/Firefox\//', $ua)) {
            return 'Firefox';
        }
        if (preg_match('/Opera\/|OPR\//', $ua)) {
            return 'Opera';
        }
        if (preg_match('/MSIE |Trident\//', $ua)) {
            return 'Internet Explorer';
        }
        if (preg_match('/SamsungBrowser\//', $ua)) {
            return 'Samsung Browser';
        }
        if (preg_match('/UCBrowser\//', $ua)) {
            return 'UC Browser';
        }
        return 'Unknown';
    }

    private function getBrowserVersion($ua)
    {
        $patterns = [
            '/Edg(e|A|iOS)\/(\d+\.\d+)/',
            '/Chrome\/(\d+\.\d+)/',
            '/Safari\/(\d+\.\d+)/',
            '/Firefox\/(\d+\.\d+)/',
            '/OPR\/(\d+\.\d+)/',
            '/Version\/(\d+\.\d+)/',
            '/MSIE (\d+\.\d+)/',
            '/rv:(\d+\.\d+)/',
        ];
        foreach ($patterns as $pattern) {
            if (preg_match($pattern, $ua, $matches)) {
                return $matches[count($matches) - 1];
            }
        }
        return '0.0';
    }

    private function getBrowserVendor($browserName)
    {
        $vendors = [
            'Chrome' => 'Google',
            'Safari' => 'Apple',
            'Mobile Safari' => 'Apple',
            'Firefox' => 'Mozilla',
            'Opera' => 'Opera Software',
            'Microsoft Edge' => 'Microsoft',
            'Internet Explorer' => 'Microsoft',
            'Samsung Browser' => 'Samsung',
            'UC Browser' => 'UCWeb',
        ];
        return $vendors[$browserName] ?? 'Unknown';
    }

    private function getPlatformName($ua)
    {
        if (preg_match('/Windows NT/', $ua)) {
            return 'Windows';
        }
        if (preg_match('/Mac OS X|MacPPC|MacIntel/', $ua)) {
            if (preg_match('/iPhone|iPod|iPad/', $ua)) {
                return 'iOS';
            }
            return 'macOS';
        }
        if (preg_match('/Android/', $ua)) {
            return 'Android';
        }
        if (preg_match('/Linux/', $ua)) {
            return 'Linux';
        }
        if (preg_match('/CrOS/', $ua)) {
            return 'Chrome OS';
        }
        return 'Unknown';
    }

    private function getPlatformVersion($ua)
    {
        if (preg_match('/OS (\d+)[_\.](\d+)/', $ua, $matches)) {
            return $matches[1] . '.' . $matches[2];
        }
        if (preg_match('/Android (\d+\.\d+)/', $ua, $matches)) {
            return $matches[1];
        }
        if (preg_match('/Windows NT (\d+\.\d+)/', $ua, $matches)) {
            $version = $matches[1];
            $windowsVersions = [
                '10.0' => '10',
                '6.3' => '8.1',
                '6.2' => '8',
                '6.1' => '7',
                '6.0' => 'Vista',
                '5.1' => 'XP',
            ];
            return $windowsVersions[$version] ?? $version;
        }
        if (preg_match('/Mac OS X (\d+)[_\.](\d+)/', $ua, $matches)) {
            return $matches[1] . '.' . $matches[2];
        }
        return '0.0';
    }

    private function getPlatformVendor($platformName)
    {
        $vendors = [
            'iOS' => 'Apple',
            'macOS' => 'Apple',
            'Android' => 'Google',
            'Windows' => 'Microsoft',
            'Chrome OS' => 'Google',
            'Linux' => 'Linux Foundation',
        ];
        return $vendors[$platformName] ?? 'Unknown';
    }

    private function getHardwareName($ua)
    {
        if (preg_match('/iPhone/', $ua)) {
            return 'iPhone';
        }
        if (preg_match('/iPad/', $ua)) {
            return 'iPad';
        }
        if (preg_match('/iPod/', $ua)) {
            return 'iPod';
        }
        if (preg_match('/SM-[A-Z]\d+/', $ua, $matches)) {
            return $matches[0];
        }
        if (preg_match('/(HW|HUAWEI)[- ]([A-Z0-9\-]+)/', $ua, $matches)) {
            return $matches[2];
        }
        if (preg_match('/MI [A-Z0-9]+|Redmi/', $ua, $matches)) {
            return $matches[0];
        }
        if (preg_match('/Android/', $ua)) {
            return 'Android Device';
        }
        if (preg_match('/Windows|Mac OS X|Linux/', $ua)) {
            return 'Desktop';
        }
        return 'Unknown Device';
    }

    private function getHardwareModel($ua)
    {
        if (preg_match('/iPhone/', $ua)) {
            return 'iPhone';
        }
        if (preg_match('/iPad/', $ua)) {
            return 'iPad';
        }
        if (preg_match('/\(([^)]*Android[^)]*)\)/', $ua, $matches)) {
            $parts = explode(';', $matches[1]);
            if (count($parts) >= 2) {
                return trim($parts[count($parts) - 1]);
            }
        }
        return $this->getHardwareName($ua);
    }

    private function getHardwareFamily($hardwareName)
    {
        if (strpos($hardwareName, 'iPhone') !== false) {
            return 'iPhone';
        }
        if (strpos($hardwareName, 'iPad') !== false) {
            return 'iPad';
        }
        if (strpos($hardwareName, 'iPod') !== false) {
            return 'iPod';
        }
        if (strpos($hardwareName, 'SM-') !== false) {
            return 'Samsung Galaxy';
        }
        if (strpos($hardwareName, 'Desktop') !== false) {
            return 'Desktop PC';
        }
        return 'Mobile';
    }

    private function getHardwareVendor($ua)
    {
        if (preg_match('/iPhone|iPad|iPod|Macintosh/', $ua)) {
            return 'Apple';
        }
        if (preg_match('/Samsung|SM-/', $ua)) {
            return 'Samsung';
        }
        if (preg_match('/Huawei|HW-|HUAWEI/', $ua)) {
            return 'Huawei';
        }
        if (preg_match('/Xiaomi|MI |Redmi/', $ua)) {
            return 'Xiaomi';
        }
        if (preg_match('/OPPO/', $ua)) {
            return 'OPPO';
        }
        if (preg_match('/vivo/', $ua)) {
            return 'Vivo';
        }
        if (preg_match('/OnePlus/', $ua)) {
            return 'OnePlus';
        }
        if (preg_match('/LG-/', $ua)) {
            return 'LG';
        }
        if (preg_match('/Sony/', $ua)) {
            return 'Sony';
        }
        if (preg_match('/Pixel/', $ua)) {
            return 'Google';
        }
        return 'Unknown';
    }

    private function getScreenWidth($ua)
    {
        if (preg_match('/iPhone/', $ua)) {
            return '750';
        }
        if (preg_match('/iPad/', $ua)) {
            return '2048';
        }
        if (preg_match('/Android.*Mobile/', $ua)) {
            return '1080';
        }
        if (preg_match('/Android/', $ua) && !preg_match('/Mobile/', $ua)) {
            return '1280';
        }
        return '1920';
    }

    private function getScreenHeight($ua)
    {
        if (preg_match('/iPhone/', $ua)) {
            return '1334';
        }
        if (preg_match('/iPad/', $ua)) {
            return '2732';
        }
        if (preg_match('/Android.*Mobile/', $ua)) {
            return '1920';
        }
        if (preg_match('/Android/', $ua) && !preg_match('/Mobile/', $ua)) {
            return '800';
        }
        return '1080';
    }

    private function getScreenDiagonal($ua)
    {
        if (preg_match('/iPhone/', $ua)) {
            return '4.7';
        }
        if (preg_match('/iPad/', $ua)) {
            return '10.2';
        }
        if (preg_match('/Android.*Mobile/', $ua)) {
            return '6.0';
        }
        if (preg_match('/Android/', $ua) && !preg_match('/Mobile/', $ua)) {
            return '10.0';
        }
        return '24.0';
    }

    private function getPopularity($ua)
    {
        if (preg_match('/iPhone/', $ua)) {
            return '9';
        }
        if (preg_match('/iPad/', $ua)) {
            return '8';
        }
        if (preg_match('/Samsung/', $ua)) {
            return '8';
        }
        if (preg_match('/Chrome/', $ua)) {
            return '9';
        }
        if (preg_match('/Safari/', $ua)) {
            return '7';
        }
        return '5';
    }

    public function getUserAgent()
    {
        return $this->userAgent;
    }

    public function get($field)
    {
        return $this->parsed[$field] ?? null;
    }

    public function getAll()
    {
        return $this->parsed;
    }
}



