import 'dart:convert';
import 'dart:ui' as ui;
import 'dart:io';
import 'package:image/image.dart' as img;

import 'package:camera/camera.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import "package:universal_html/html.dart" as html;
import 'package:flutter/foundation.dart' show kIsWeb;

import '../models/media_model.dart';
import '../services/base_service.dart';

class MediaViewModel extends ChangeNotifier {
  List<XFile> _audios = [];
  List<XFile> get audios => _audios;

  List<XFile> _temporales = [];
  List<XFile> get temporales => _temporales;

  List<XFile> _archivos = [];
  List<XFile> get archivos => _archivos;

  bool _isLoading = false;
  bool get isLoading => _isLoading;

  XFile? _firma;
  XFile? get firma => _firma;

  XFile? _firmaChofer;
  XFile? get firmaChofer => _firmaChofer;

  XFile? _firmaSupervisor;
  XFile? get firmaSupervisor => _firmaSupervisor;

  XFile? getFirmaChofer() {
    return _firmaChofer;
  }

  XFile? getFirmaSupervisor() {
    return _firmaSupervisor;
  }

  Future<void> fetchAudio() async {
    _temporales = [];
    notifyListeners();
  }

  setFirma(XFile value) {
    _firma = value;
    notifyListeners();
  }

  setFirmaChofer(XFile value) {
    _firmaChofer = value;
    notifyListeners();
  }

  setFirmaSupervisor(XFile value) {
    _firmaSupervisor = value;
    notifyListeners();
  }

  Future agregarAudio(XFile value) async {
    _audios.add(value);
    notifyListeners();
  }

  Future limpiar() async {
    _audios = [];
    _temporales = [];
    _archivos = [];
    _firma = null;
    _firmaChofer = null;
    _firmaSupervisor = null;
    notifyListeners();
  }

  void setIsLoading(bool loading) {
    _isLoading = loading;
    notifyListeners();
  }

  Future guardarAudio(
      {required int idModuloLocal,
      File? audio,
      required String nombreModulo}) async {}

  Future<XFile?> convertirPlatformFileAXFile(PlatformFile platformFile) async {
    if (!kIsWeb) {
      final XFile xFile = XFile(platformFile.path.toString(),
          name: platformFile.name, mimeType: "archivo");
      return xFile;
    }

    try {
      final Uint8List bytes = platformFile.bytes!;
      final html.Blob blob = html.Blob([bytes], 'application/octet-stream');
      String url = html.Url.createObjectUrlFromBlob(blob);

      final XFile xFile =
          XFile(url.toString(), name: platformFile.name, mimeType: "archivo");
      return xFile;
    } catch (e) {
      print('Error al convertir PlatformFile a XFile: $e');
      return null;
    }
  }

  void agregarArchivo(XFile? value) {
    if (value != null) {
      _archivos.add(value);
      notifyListeners();
    }
  }

  Future<XFile> convertirAImagePickerWebFile(ui.Image image) async {
    ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData!.buffer.asUint8List();
    final tempDir = Directory.systemTemp;
    String uniqueFileName =
        '${tempDir.path}/temp_image_${DateTime.now().millisecondsSinceEpoch}.png';
    final tempFile = await File(uniqueFileName).writeAsBytes(pngBytes);
    return XFile(tempFile.path);
  }

  Future<void> eliminar(Media m) async {
    var r = await BaseService()
        .delete("admin/media", body: {"id": m.id.toString()});
    Map<String, dynamic> resJson = jsonDecode(r.body);
    if (r.statusCode == 200) {}
  }
}
