import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as htmlParser;
import 'package:html/dom.dart' as htmlDom;
import 'package:flutter_project/pages/embalses';

// Define el estado
class MyState {

}

// Define el bloc
class MyBloc extends Cubit<MyState> {
  MyBloc() : super(MyState());

}

Future<List<String>> obtenerDatos() async {
    final response = await http.get(Uri.parse('https://crcivan.asociacionador.es/contenedores.html'));
    if (response.statusCode == 200) {
      final document = htmlParser.parse(response.body);
      final elementosSpan = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > table > tbody > tr > td > span');
      List<String> embalsesData = [];

      for (var elemento in elementosSpan) {
        final style = elemento.attributes['style'];
        if (style != null && style.contains('color: #000000; font-size: 12pt;')) {
          embalsesData.add(elemento.text);
        }
      }
      return embalsesData;
    } else {
      throw Exception('Error al cargar los datos de embalses');
    }
  }

Future<void> launchAemetURL() async {
  const urlString = 'https://www.aemet.es/es/eltiempo/prediccion/municipios/caspe-id50074';
  final url = Uri.parse(urlString);

  try {
          await launch(urlString);

  /*  if (await canLaunch(urlString)) {
      await launch(urlString);
    } else {
      throw 'Could not launch $urlString';
    }*/
  } catch (e) {
    throw Exception('Error al lanzar la URL de Aemet: $e');
  }
}