<?php
if (!function_exists('jusqWeatherCities')) {
    function jusqWeatherCities()
    {
        return array(
            array('서울', 37.5665, 126.9780), array('부산', 35.1796, 129.0756),
            array('인천', 37.4563, 126.7052), array('대구', 35.8714, 128.6014),
            array('대전', 36.3504, 127.3845), array('광주', 35.1595, 126.8526),
            array('울산', 35.5384, 129.3114), array('세종', 36.4800, 127.2890),
            array('수원', 37.2636, 127.0286), array('춘천', 37.8813, 127.7298),
            array('강릉', 37.7519, 128.8761), array('청주', 36.6424, 127.4890),
            array('전주', 35.8242, 127.1480), array('목포', 34.8118, 126.3922),
            array('제주', 33.4996, 126.5312)
        );
    }
    function jusqWeatherText($code)
    {
        $code = (int)$code;
        if ($code === 0) return '맑음';
        if (in_array($code, array(1, 2), true)) return '대체로 맑음';
        if ($code === 3) return '흐림';
        if (in_array($code, array(45, 48), true)) return '안개';
        if (in_array($code, array(51, 53, 55, 56, 57), true)) return '이슬비';
        if (in_array($code, array(61, 63, 65, 66, 67, 80, 81, 82), true)) return '비';
        if (in_array($code, array(71, 73, 75, 77, 85, 86), true)) return '눈';
        if (in_array($code, array(95, 96, 99), true)) return '천둥번개';
        return '구름 많음';
    }
    function jusqWeatherIcon($code)
    {
        $code = (int)$code;
        if ($code === 0) return 102;
        if (in_array($code, array(1, 2), true)) return 103;
        if ($code === 3) return 105;
        if (in_array($code, array(45, 48), true)) return 107;
        if (in_array($code, array(51, 53, 55, 56, 57), true)) return 108;
        if (in_array($code, array(61, 63, 65, 66, 67, 80, 81, 82), true)) return 106;
        if (in_array($code, array(71, 73, 75, 77, 85, 86), true)) return 112;
        if (in_array($code, array(95, 96, 99), true)) return 109;
        return 104;
    }
    function jusqWeatherRequest($url)
    {
        if (function_exists('getUrlData')) {
            $body = getUrlData($url, 8);
            if ($body) return $body;
        }
        $context = stream_context_create(array('http' => array('timeout' => 8, 'user_agent' => 'JusQ Weather Widget/1.0')));
        return @file_get_contents($url, false, $context);
    }
    function jusqWeatherUpdate($weatherFile)
    {
        $rows = array();
        foreach (jusqWeatherCities() as $city) {
            $url = 'https://api.open-meteo.com/v1/forecast?latitude='.rawurlencode($city[1]).'&longitude='.rawurlencode($city[2]).'&current=temperature_2m,relative_humidity_2m,weather_code,wind_speed_10m&timezone=Asia%2FSeoul';
            $data = json_decode(jusqWeatherRequest($url), true);
            if (empty($data['current'])) continue;
            $current = $data['current'];
            $temp = round((float)$current['temperature_2m'], 1);
            $code = (int)$current['weather_code'];
            $rows[] = implode('|', array($city[0], jusqWeatherText($code), round(($temp * 9 / 5) + 32, 1), $temp, '습도 '.(int)$current['relative_humidity_2m'].'%', jusqWeatherIcon($code), '바람 '.round((float)$current['wind_speed_10m'], 1).'km/h', $city[1], $city[2]));
        }
        if (!$rows) return false;
        if (!is_dir(dirname($weatherFile))) @mkdir(dirname($weatherFile), 0775, true);
        return @file_put_contents($weatherFile, implode('<split>', $rows).'<split>', LOCK_EX) !== false;
    }
}
