import 'package:didvan/models/category.dart';
// ignore: depend_on_referenced_packages
import 'package:html/parser.dart';

class OverviewData {
  final int id;
  final String title;
  final String image;
  final String description;
  final String? category;
  final int? timeToRead;
  final int? duration;
  final String? reference;
  final String? link;
  final String? iframe;
  final bool forManagers;
  final String createdAt;
  final String type;
  int typeInteger;
  int comments;
  bool marked;
  bool liked;
  int likes;
  final List<CategoryData>? categories;

  OverviewData({
    required this.id,
    required this.title,
    required this.image,
    required this.description,
    required this.createdAt,
    required this.type,
    required this.marked,
    required this.liked,
    required this.likes,
    required this.comments,
    required this.forManagers,
    this.category,
    this.link,
    this.iframe,
    this.duration,
    this.timeToRead,
    this.reference,
    this.categories,
    this.typeInteger = 0,
  }) {
    switch (type) {
      case 'radar':
        typeInteger = 1;
        break;
      case 'news':
        typeInteger = 2;
        break;
      case 'video':
        typeInteger = 3;
        break;
      case 'podcast':
        typeInteger = 4;
        break;
      case 'saha':
        typeInteger = 6;
        break;
      case 'infography':
        typeInteger = 7;
        break;
      default:
        typeInteger = 5;
    }
  }

  factory OverviewData.fromJson(Map<String, dynamic> json) {
    String? description;
    if (json['description'] != null) {
      final document = parse(json['description']);
      description = parse(document.body!.text).documentElement!.text;
    }
    return OverviewData(
      id: json['id'],
      title: json['title'],
      image: json['image'] ?? 'https://wallpapercave.com/fwp/wp12977378.jpg',
      description: description ?? '',
      timeToRead: json['timeToRead'],
      reference: json['reference'],
      forManagers: json['forManagers'] ?? false,
      comments: json['comments'] ?? 0,
      category: json['category'] ?? '',
      createdAt: json['createdAt'],
      duration: json['duration'],
      type: json['type'] ?? '',
      marked: json['marked'] ?? true,
      liked: json['liked'] ?? true,
      likes: json['likes'] ?? 0,
      link: json['link'],
      iframe: json['iframe'],
      categories: json['categories'] != null
          ? List<CategoryData>.from(
              json['categories'].map(
                (e) => CategoryData.fromJson(e),
              ),
            )
          : null,
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'title': title,
        'image': image,
        'description': description,
        'timeToRead': timeToRead,
        'reference': reference,
        'forManagers': forManagers,
        'createdAt': createdAt,
        'category': category,
        'type': type,
        'categories': categories?.map((e) => e.toJson()).toList(),
      };
}
