import 'dart:io';
import 'package:flutter/services.dart';

import 'package:mime/mime.dart';
import 'package:path/path.dart' as p;

class FilesModel {
  final String path;
  late String? name;
  late String extname;
  late File main;
  final bool isRecorded;
  final bool? audio;
  final bool? image;
  final bool? video;
  final bool? network;
  final Uint8List? bytes;
  final Duration? duration;

  FilesModel(
    this.path, {
    this.name,
    this.isRecorded = false,
    this.audio,
    this.image,
    this.video,
    this.network,
    this.bytes,
    this.duration,
  }) {
    name = name ?? p.basename(path);
    extname = path.isNotEmpty
        ? p.extension(path)
        : name != null
            ? p.extension(name!)
            : '';

    main = File(path);
  }

  bool isAudio() {
    return audio ??
        (lookupMimeType(path)?.startsWith('audio/') ?? false) ||
            path.contains(".mp3");
  }

  bool isImage() {
    return image ??
        lookupMimeType(path)?.startsWith('image/') ??
        false || path.contains(".png");
  }

  bool isVideo() {
    return video ??
        lookupMimeType(path)?.startsWith('video/') ??
        false || path.contains(".mp4");
  }

  bool isNetwork() {
    return network ?? path.startsWith('blob:') || path.startsWith('/uploads');
  }
}
