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_types.g.dart';

@JsonSerializable(explicitToJson: true, includeIfNull: true)
@collection
class MeasureTypes extends BaseEntity with EntityWithId {
  MeasureTypes();

  @JsonKey(name: 'field_2402426')
  @Index()
  String? measureType;

  @JsonKey(name: 'field_2402581')
  List<IdValue>? measureNumber;

  @JsonKey(name: 'field_2402473')
  IdValueColor? measureGroup;

  @JsonKey(name: 'field_3001179')
  int? bioDivScoreCategory;

  @JsonKey(name: 'field_2402427')
  List<IdValueColor>? measureCategory;

  @JsonKey(name: 'field_2402431')
  String? description;

  @JsonKey(name: 'field_2402433')
  String? objective;

  @JsonKey(name: 'field_2402435')
  String? scientificBackgroundRedundant;

  @JsonKey(name: 'field_2402437')
  String? sources;

  @JsonKey(name: 'field_2402440')
  List<IdValueColor>? applyTimeFrame;

  @JsonKey(name: 'field_2429782')
  String? applyTimeFrameAdditional;

  @JsonKey(name: 'field_2429460')
  List<IdValueColor>? applyTools;

  @JsonKey(name: 'field_2428593')
  List<IdValueColor>? applyMaterial;

  @JsonKey(name: 'field_2402442')
  String? applyReminder;

  @JsonKey(name: 'field_2402443')
  String? applyWorkSteps;

  @JsonKey(name: 'field_2402444')
  String? applyLocation;

  @JsonKey(name: 'field_2402445')
  String? applySize;

  @JsonKey(name: 'field_2402446')
  String? applyAllowance;

  @JsonKey(name: 'field_2402447')
  String? applyCosts;

  @JsonKey(name: 'field_2429065')
  String? applyMaterialCosts;

  @JsonKey(name: 'field_2402448')
  String? applyTime;

  @JsonKey(name: 'field_2429064')
  String? applyTimeMinutes;

  @JsonKey(name: 'field_2402450')
  String? applyTips;

  @JsonKey(name: 'field_2498056')
  List<IdValueColor>? maintenanceTimeFrame;

  @JsonKey(name: 'field_2463465')
  String? maintenanceTimeFrameAdditional;

  @JsonKey(name: 'field_2402452')
  String? maintenanceWorkSteps;

  @JsonKey(name: 'field_2429972')
  List<IdValueColor>? maintenanceTools;

  @JsonKey(name: 'field_2463462')
  List<IdValueColor>? maintenanceMaterial;

  @JsonKey(name: 'field_2402454')
  List<IdValueColor>? maintenanceFrequency;

  @JsonKey(name: 'field_2564385')
  String? maintenanceFrequencyAdditional;

  @JsonKey(name: 'field_2402455')
  String? maintenanceSpecialFeature;

  @JsonKey(name: 'field_2565115')
  String? maintenanceTips;

  @JsonKey(name: 'field_2402458')
  String? measureCombination;

  @JsonKey(name: 'field_2402459')
  String? measureTechnicalExpertise;

  @JsonKey(name: 'field_2429451')
  List<IdValue>? measureTechnicalExpertiseServiceProvider;

  @JsonKey(name: 'field_2429454')
  List<IdValue>? measureTechnicalExpertiseContactPerson;

  @JsonKey(name: 'field_2402540')
  List<IdValue>? fundingProgram;

  @JsonKey(name: 'field_2402548')
  List<FilePart>? files;

  @JsonKey(name: 'field_2404158')
  List<IdValue>? referenceImplementation;

  @JsonKey(name: 'field_2463526')
  List<IdValue>? combinations;

  @JsonKey(name: 'field_2565015')
  String? ecologicalRelevance;

  @JsonKey(name: 'field_2565110')
  String? requirementTips;

  @JsonKey(name: 'field_2565943')
  List<IdValue>? factsheetSources;

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

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

extension MeasureExtension on MeasureTypes {
  ListMeasure toListMeasure() {
    final listMeasure = ListMeasure()
      ..isarId = isar.listMeasures.autoIncrement()
      ..id = this.id
      ..name = measureType
      ..measureCategories = measureCategory
              ?.map(
                  (cat) => cat.value!.toLowerCase().replaceUmlauts().toString())
              .toList() ??
          []
      ..measureGroups = measureGroup != null
          ? [measureGroup!.value!.toLowerCase().replaceUmlauts().toString()]
          : []
      ..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;
  }
}
