<?php

declare(strict_types=1);

namespace PhpWA\Auth;

use PhpWA\Protocol\Proto\ClientPayload\UserAgent\Platform;
use PhpWA\Protocol\Proto\ClientPayload\WebInfo\WebSubPlatform;
use PhpWA\Protocol\Proto\DeviceProps\PlatformType;

final class CompanionPlatform
{
    private const QR_UNKNOWN = 9;

    public static function qrClientId(CompanionDeviceConfig $config): int
    {
        $browser = strtolower($config->browserName());
        $map = ['chrome' => 1, 'edge' => 2, 'firefox' => 3, 'ie' => 4, 'opera' => 5, 'safari' => 6];
        if ($browser === 'desktop') {
            return strtolower($config->operatingSystem()) === 'windows' ? 8 : 7;
        }
        return $map[$browser] ?? self::QR_UNKNOWN;
    }

    public static function devicePropsType(CompanionDeviceConfig $config): int
    {
        $browser = strtoupper(str_replace([' ', '-'], '_', $config->browserName()));
        $map = [
            'ANDROID' => PlatformType::ANDROID_PHONE,
            'CHROME' => PlatformType::CHROME,
            'FIREFOX' => PlatformType::FIREFOX,
            'IE' => PlatformType::IE,
            'OPERA' => PlatformType::OPERA,
            'SAFARI' => PlatformType::SAFARI,
            'EDGE' => PlatformType::EDGE,
            'DESKTOP' => PlatformType::DESKTOP,
            'UWP' => PlatformType::UWP,
        ];
        return $map[$browser] ?? PlatformType::CHROME;
    }

    public static function userAgentPlatform(CompanionDeviceConfig $config): int
    {
        return stripos($config->browserName(), 'android') !== false ? Platform::ANDROID : Platform::WEB;
    }

    public static function webSubPlatform(CompanionDeviceConfig $config): int
    {
        if ($config->syncFullHistory() && strcasecmp($config->browserName(), 'Desktop') === 0) {
            if (strcasecmp($config->operatingSystem(), 'Mac OS') === 0) { return WebSubPlatform::DARWIN; }
            if (strcasecmp($config->operatingSystem(), 'Windows') === 0) { return WebSubPlatform::WIN32; }
        }
        return WebSubPlatform::WEB_BROWSER;
    }

    private function __construct()
    {
    }
}
