<?php
class UtilsClass
{
    public static function skinsFromJson(): array
    {
        $skins = [];
        $json = json_decode(file_get_contents(__DIR__ . "/../data/" . SKIN_LANGUAGE . ".json"), true);

        foreach ($json as $skin) {
            $skins[(int)$skin['weapon_defindex']][(int)$skin['paint']] = [
                'weapon_name' => $skin['weapon_name'],
                'paint_name'  => $skin['paint_name'],
                'image_url'   => $skin['image'],
            ];
        }

        return $skins;
    }

    public static function getWeaponsFromArray(): array
    {
        $weapons = [];
        $temp = self::skinsFromJson();

        foreach ($temp as $key => $value) {
            if (key_exists($key, $weapons))
                continue;
            $weapons[$key] = [
                'weapon_name' => $value[0]['weapon_name'],
                'paint_name'  => $value[0]['paint_name'],
                'image_url'   => $value[0]['image_url'],
            ];
        }

        return $weapons;
    }

    public static function getKnifeTypes(): array
    {
        $knifeIndexes = [500, 503, 505, 506, 507, 508, 509, 512, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 525, 526];
        $knifes = [];
        $temp = self::getWeaponsFromArray();

        foreach ($temp as $key => $weapon) {
            if (!in_array($key, $knifeIndexes))
                continue;

            $knifes[$key] = [
                'weapon_name' => $weapon['weapon_name'],
                'paint_name'  => rtrim(explode("|", $weapon['paint_name'])[0]),
                'image_url'   => $weapon['image_url'],
            ];
        }

        $knifes[0] = [
            'weapon_name' => 'weapon_knife',
            'paint_name'  => 'Default Knife',
            'image_url'   => 'https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/website/img/skins/weapon_knife.png',
        ];

        ksort($knifes);
        return $knifes;
    }

    public static function getGloveTypes(): array
    {
        $gloveIndexes = [4725, 5027, 5028, 5029, 5030, 5031];
        $gloves = [];
        $temp = self::skinsFromJson();

        $gloves[0] = [
            'weapon_name' => 'default_gloves',
            'paint_name'  => 'Default Gloves',
            'image_url'   => 'https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/website/img/skins/weapon_knife.png',
        ];

        foreach ($temp as $defindex => $skinGroup) {
            if (!in_array($defindex, $gloveIndexes))
                continue;
            $first = reset($skinGroup);
            $gloves[$defindex] = [
                'weapon_name' => $first['weapon_name'],
                'paint_name'  => rtrim(explode("|", $first['paint_name'])[0]),
                'image_url'   => $first['image_url'],
            ];
        }

        ksort($gloves);
        return $gloves;
    }

    public static function getSelectedSkins(array $temp): array
    {
        $selected = [];
        foreach ($temp as $weapon) {
            $selected[$weapon['weapon_defindex']] = [
                'weapon_paint_id' => $weapon['weapon_paint_id'],
                'weapon_seed'     => $weapon['weapon_seed'],
                'weapon_wear'     => $weapon['weapon_wear'],
            ];
        }
        return $selected;
    }

    public static function getWearLabel(float $wear): string
    {
        if ($wear <= 0.07) return 'FN';
        if ($wear <= 0.15) return 'MW';
        if ($wear <= 0.38) return 'FT';
        if ($wear <= 0.45) return 'WW';
        return 'BS';
    }

    public static function getSteamUserInfo(string $steamid): array
    {
        $url = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=" . STEAM_API_KEY . "&steamids=" . $steamid;
        $json = @file_get_contents($url);
        if (!$json) return [];
        $data = json_decode($json, true);
        return $data['response']['players'][0] ?? [];
    }
}
