import 'package:ambito/src/entity/entities.dart';
import 'package:ambito/src/extensions/extensions.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:isar/isar.dart';
import 'package:json_annotation/json_annotation.dart';

import '../../../consts/consts.dart';
import '../../lists/list_measure.dart';

part 'measure_general.g.dart';

@collection
@JsonSerializable(explicitToJson: true)
class MeasureGeneral extends BaseEntity with EntityWithId {
  MeasureGeneral();

  @JsonKey(name: 'field_3021886')
  @Index()
  String? name;
  @JsonKey(name: 'field_3021893')
  List<IdValue>? measureType;
  @JsonKey(name: 'field_3050639')
  List<IdValueMix>? measureGroup;
  @JsonKey(name: 'field_3021890')
  int? bioDivScore;
  @JsonKey(name: 'field_3021889')
  List<IdValue>? producerOrProvider;
  @JsonKey(name: 'field_3021896')
  List<FilePart>? files;
  @JsonKey(name: 'field_3021990')
  List<IdValueColor>? region;
  @JsonKey(name: 'field_3050742')
  List<IdValueColor>? applyTimeFrame;
  @JsonKey(name: 'field_3024626')
  List<IdValue>? organisms;
  @JsonKey(name: 'field_3021904')
  IdValueColor? baseUnit;
  @JsonKey(name: 'field_3021892')
  String? baseUnitPrice;
  @JsonKey(name: 'field_3049762')
  IdValueColor? effort;
  @JsonKey(name: 'field_3021887')
  String? remarks;
  @JsonKey(name: 'field_3021888')
  String? composition;
  @JsonKey(name: 'field_3049760')
  List<FilePart>? resources;

  factory MeasureGeneral.fromJson(Map<String, dynamic> json) =>
      _$MeasureGeneralFromJson(json);

  Map<String, dynamic> toJson() => _$MeasureGeneralToJson(this);
}

extension MeasureGeneralExtension on MeasureGeneral {
  ListMeasure toListMeasure() {
    final listMeasure = ListMeasure()
      ..isarId = isar.listMeasures.autoIncrement()
      ..id = this.id
      ..name = name
      ..measureGroups = measureGroup
              ?.map((group) =>
                  group.value!.value!.toLowerCase().replaceUmlauts().toString())
              .toList() ??
          []
      ..measureTypes = measureType
              ?.map((type) =>
                  type.value!.toLowerCase().replaceUmlauts().toString())
              .toList() ??
          []
      ..files = files;

    return listMeasure;
  }

  CachedNetworkImage? getThumbnail() {
    CachedNetworkImage? image;
    if (files != null && files!.isNotEmpty) {
      if (files![0].thumbnails?.cardCover?.url != null) {
        image = CachedNetworkImage(
          imageUrl: files![0].thumbnails!.cardCover!.url!,
          placeholder: (context, url) => const CircularProgressIndicator(),
          errorWidget: (context, url, error) => const Icon(Icons.error),
        );
      }
    }
    return image;
  }

  CachedNetworkImageProvider? getFullImageProvider() {
    if (files != null && files!.isNotEmpty) {
      if (files![0].url != null) {
        return CachedNetworkImageProvider(files![0].url!);
      }
    }
    return null;
  }

  CachedNetworkImage? getFullImage() {
    CachedNetworkImage? image;
    if (files != null && files!.isNotEmpty) {
      if (files![0].url != null) {
        image = CachedNetworkImage(
          imageUrl: files![0].url!,
          placeholder: (context, url) => const CircularProgressIndicator(),
          errorWidget: (context, url, error) => const Icon(Icons.error),
          fit: BoxFit.cover,
        );
      }
    }
    return image;
  }

  Widget? getImage() {
    Widget? image;
    if (files != null && files!.isNotEmpty) {
      if (files![0].thumbnails?.cardCover?.url != null) {
        image = CachedNetworkImage(
          width: 600,
          height: 350,
          imageUrl: files![0].thumbnails!.cardCover!.url!,
          placeholder: (context, url) => const CircularProgressIndicator(),
          errorWidget: (context, url, error) => const Icon(Icons.error),
          fit: BoxFit.cover,
        );
      }
    }
    return image;
  }
}
