import 'package:ambito/src/domain/entity/base_entity.dart';
import 'package:ambito/src/domain/entity/id_value/id_value.dart';
import 'package:ambito/src/domain/entity/id_value_color/id_value_color.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 '../file/file_part.dart';
import '../file/thumbnail.dart';
import '../file/thumbnails.dart';
import '../id_value_mix/id_value_mix.dart';

part 'massnahme.g.dart';

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

  @JsonKey(name: 'order')
  String? orderString;
  @JsonKey(name: 'Maßnahmenbezeichung')
  @Index()
  String? name;
  @JsonKey(name: 'Flächentyp Factsheet')
  List<IdValueColor>? factsheetAreaType;
  @JsonKey(name: 'Beispielabbildung\\\n(Link)')
  String? examples;
  @JsonKey(name: 'Definition Factsheet')
  String? factsheetDefinition;
  @JsonKey(name: 'Zielsetzung Factsheet')
  String? factsheetTarget;
  @JsonKey(name: 'Wissenschaftlicher Hintergrund redundant')
  String? scientificBackgroundRedundant;
  @JsonKey(name: 'Wissenschaftlicher Hintergrund Factsheet')
  String? factsheetScientificBackground;
  @JsonKey(name: 'Quellen')
  String? sources;
  @JsonKey(name: 'Zeitrahmen Anlage')
  List<IdValueColor>? timeFrame;
  @JsonKey(name: 'Beachtenswert - Anlage')
  String? attachmentReminder;
  @JsonKey(name: 'Arbeitsschritte Anlage')
  String? attachmentWorkSteps;
  @JsonKey(name: "Standort Fact Sheet")
  String? factsheetLocation;
  @JsonKey(name: "Größe/Fläche Factsheet")
  String? factsheetSizeArea;
  @JsonKey(name: "Genehmigungspflicht redundant")
  String? redundantAllowance;
  @JsonKey(name: "Fachgerechte Umsetzung_Anlage_Kosten_Geld")
  String? moneyAction;
  @JsonKey(name: "Fachgerechte Umsetzung_Anlage_Kosten_Zeit")
  String? timeAction;
  @JsonKey(name: "Dienstleister redundant")
  String? serviceProvider;
  @JsonKey(name: "Tipp -  Anlage")
  String? attachmentTip;
  @JsonKey(name: "Zeitrahmen Pflege")
  String? timeMaintenance;
  @JsonKey(name: "Arbeitsschritte Pflege")
  String? workMaintenance;
  @JsonKey(name: "Frequenz Pflege")
  List<IdValueColor>? frequencyMaintenance;
  @JsonKey(name: "Besonderheiten - Pflege")
  String? specialsMaintenance;
  @JsonKey(name: "Maßnahmenkombination (passt gut zu)")
  String? matchingActions;
  @JsonKey(name: "Fachexpertise redundant")
  String? redundantExpertise;
  @JsonKey(name: "Maßnahmen_geförderte_Organismen")
  List<IdValue>? organismActions;
  @JsonKey(name: "Maßnahmengruppe")
  IdValueColor? actionGroup;
  @JsonKey(name: "Förderprogramme")
  List<IdValueColor>? fundingPrograms;
  @JsonKey(name: "Datei")
  List<FilePart>? files;
  @JsonKey(name: "Maßnahmennummer")
  List<IdValue>? numberOfAction;
  @JsonKey(name: "Factsheet_Status")
  List<IdValue>? factsheetStatus;
  @JsonKey(name: "04 Standortansprüche")
  List<IdValue>? requiredLocation;
  @JsonKey(name: "06 Betriebsverzeichnis")
  List<IdValue>? business;
  @JsonKey(name: "05 Referenzumsetzung")
  List<IdValue>? referenceAction;
  @JsonKey(name: "Material Anlage")
  List<IdValueColor>? attachmentMaterial;
  @JsonKey(name: "08 Genehmigungspflicht")
  List<IdValueColor>? requiredAllowance;
  @JsonKey(name: "Zeitaufwand Anlage (Minuten)")
  String? requiredTimeForAction;
  @JsonKey(name: "Materialkosten")
  String? costsMaterial;
  @JsonKey(name: "Fachexpertise Dienstleister")
  List<IdValueColor>? expertiseWorker;
  @JsonKey(name: "Fachexpertise Ansprechperson")
  List<IdValueColor>? expertiseContact;
  @JsonKey(name: "Arbeitsmittel Anlage")
  List<IdValueColor>? materialAction;
  @JsonKey(name: "Zeitrahmen Anlage Zusatz")
  String? timeframeActionAdditional;
  @JsonKey(name: "Arbeitsmittel Pflege")
  List<IdValueColor>? maintenanceMaterial;
  @JsonKey(name: "12 Quellen")
  List<IdValue>? sourcesList;
  @JsonKey(name: "Sonstiges - Voraussetzungen")
  String? requirements;
  @JsonKey(name: "Material - Pflege")
  List<IdValueColor>? materialMaintenance;
  @JsonKey(name: "Pflege Zeitrahmen Zusatz")
  String? timeMaintenanceAdditional;
  @JsonKey(name: "Maßnahmenkombinationen")
  List<IdValue>? combinedActions;
  @JsonKey(name: "Bearbeitet")
  bool? done;
  @JsonKey(name: "Factsheet vorhanden")
  List<IdValueMix>? existingFactSheet;
  @JsonKey(name: "Zeitrahmen Pflege 2")
  List<IdValueColor>? timeframeMaintenance2;
  @JsonKey(name: "13 Baumarten")
  List<IdValueColor>? treeTypes;
  @JsonKey(name: "Pflege Frequenz Zusatz")
  String? frequencyMaintenanceAdditional;
  @JsonKey(name: "Besonderheiten - Anlage")
  String? specialCreation;
  @JsonKey(name: "Ökologische Relevanz - Hintergrund")
  String? oekologischeRelevanzBackground;
  @JsonKey(name: "Beachtenswert - Vorraussetzungen")
  String? remarkablePresets;
  @JsonKey(name: "Beachtenswert .- Pflege")
  String? remarkableMaintenance;
  @JsonKey(name: "Tipp - Vorraussetzungen")
  String? tipsPresets;
  @JsonKey(name: "Tipp - Pflege")
  String? tipsMaintenance;
  @JsonKey(name: "Gut zu wissen - Anlage")
  String? goodToKnowAttachment;
  @JsonKey(name: "Hintergrund - Tipps")
  String? tipsBackground;
  @JsonKey(name: "Besonderheit - Hintergrund")
  String? specialBackground;
  @JsonKey(name: "12 Quellen - Verwendet in Maßnahmendatenblatt")
  List<IdValue>? sourcesUsedInDataSheet;

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

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

extension MassnahmenExtension on Massnahme {

  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;
  }

}