<?php

namespace App\Paper;


class Airly
{

    private $apiKey = '8b6d77b2950e4e018b0684912bf7b9ed';


    private $stations = ['2210', '2256', '2180'];
    private $airlyApi = 'https://airapi.airly.eu/v1';


    public function getStations()
    {
        return $this->stations;
    }

    function getStationInfo($stationId)
    {
        return json_decode(file_get_contents(sprintf('%s/sensors/%d?apikey=%s', $this->airlyApi, $stationId, $this->apiKey)), 1);
    }

    function getStationMeasurements($stationId)
    {
        return json_decode(file_get_contents(sprintf('%s/sensor/measurements?sensorId=%d&apikey=%s', $this->airlyApi, $stationId, $this->apiKey)), 1);
    }

    public function getPollutionLevelToText($pollutionLevel)
    {
        return ["Wspaniałe powietrze! Idealny dzień na aktywność na świeżym powietrzu",
            "Dobre powietrze. Możesz bez obaw wyjść na zewnątrz i cieszyć się dniem",
            "Bywało lepiej… To nie jest najlepszy dzień na aktywność poza domem",
            "Zła jakość powietrza! Lepiej zostań dzisiaj w domu",
            "Zła jakość powietrza! Lepiej zostań dzisiaj w domu",
            "Bardzo zła jakość powietrza! Zostań dziś w domu"][$pollutionLevel - 1];
    }


    public function getInformationText($stationId)
    {
        $stationMeasurements = $this->getStationMeasurements($stationId);

        if ($stationMeasurements['currentMeasurements'] === []) {
            $dataText = 'Brak aktualnych danych ze stacji';
        } else {
            $dataText = "Aktualne warunki:" . PHP_EOL;
            if (isset($stationMeasurements['currentMeasurements']['temperature'])) {
                $dataText .= "Temperatura:  " . round((int)$stationMeasurements['currentMeasurements']['temperature'], 0) . "°C" . PHP_EOL;
            }
            if (isset($stationMeasurements['currentMeasurements']['pressure'])) {
                $dataText .= "Ciśnienie:    " . round(((float)$stationMeasurements['currentMeasurements']['pressure'] / 100), 2) . "hPa" . PHP_EOL;
            }
            if (isset($stationMeasurements['currentMeasurements']['humidity'])) {
                $dataText .= "Wilgotność:   " . round((int)$stationMeasurements['currentMeasurements']['humidity'], 2) . "%" . PHP_EOL . PHP_EOL;
            }
            if (isset($stationMeasurements['currentMeasurements']['pm1'])) {
                $dataText .= "PMI 1:   " . (int)$stationMeasurements['currentMeasurements']['pm1'] . "ppm" . PHP_EOL;
            }
            if (isset($stationMeasurements['currentMeasurements']['pm25'])) {
                $dataText .= "PMI 2.5: " . (int)$stationMeasurements['currentMeasurements']['pm25'] . "ppm / " . round((int)$stationMeasurements['currentMeasurements']['pm25'] / 0.25, 0) . '%' . PHP_EOL;
            }
            if (isset($stationMeasurements['currentMeasurements']['pm10'])) {
                $dataText .= "PMI 10:  " . (int)$stationMeasurements['currentMeasurements']['pm10'] . "ppm / " . round((int)$stationMeasurements['currentMeasurements']['pm10'] / 0.50, 0) . '%' . PHP_EOL;
            }
            if (isset($stationMeasurements['currentMeasurements']['airQualityIndex'])) {
                $dataText .= "Ogólna jakość powietrza: " . (int)$stationMeasurements['currentMeasurements']['airQualityIndex'] . '/100' . PHP_EOL;
            }
            if (isset($stationMeasurements['currentMeasurements']['pollutionLevel'])) {
                $dataText .= "Stopień zanieczyszczeń: " . (int)$stationMeasurements['currentMeasurements']['pollutionLevel'] . '/6' . PHP_EOL;
                $dataText .= $this->getPollutionLevelToText((int)$stationMeasurements['currentMeasurements']['pollutionLevel']);
            }
        }
        return $dataText;
    }
}
