import axios from 'axios';
import * as cheerio from 'cheerio';
import https from 'https';
import { lakesConfig } from './scripts/lakesConfig';

async function run() {
  const agent = new https.Agent({ rejectUnauthorized: false });
  for (const lake of lakesConfig) {
    const [internalId, oid] = lake.id.split('|');
    const URL = `https://www.pvl.cz/portal/nadrze/cz/pc/Mereni.aspx?oid=${oid}&id=${internalId}`;
    try {
      const res = await axios.get(URL, { httpsAgent: agent, headers: { 'User-Agent': 'Mozilla/5.0' } });
      const $ = cheerio.load(res.data);
      let storage = 0;
      $('table').each((i, tbl) => {
        const text = $(tbl).text();
        const match = text.match(/Hladina z[aá]sobn[ií]ho prostoru:\s*([\d,]+)/i);
        if (match) {
          storage = parseFloat(match[1].replace(',', '.'));
        }
      });
      console.log(`{ id: "${lake.id}", storageLevel: ${storage} },`);
    } catch (e) {
      console.error(e.message);
    }
  }
}
run();
