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

part 'measure_complete.g.dart';

@JsonSerializable()
@collection
class MeasureComplete extends BaseEntity with EntityWithId {
  MeasureComplete();

  String? name;
  FilePart? image;
  String? description;
  String? target;
  List<String>? fundingPrograms;
  List<String>? sizeOrArea;
  String? growth;
  String? climate;
  String? soilWater;
  String? riskOfSpottedWingDrosophila;
  String? thermophilic;
  String? frostHardy;
  String? lateFrost;
  String? drought;
  List<String>? soilMoisture;
  String? numberLight;
  String? numberTemperature;
  String? numberMoisture;
  String? numberReaction;
  String? numberNitrogen;
  List<String>? region;
  String? location;
  String? applyCosts;
  String? applyDuration;
  List<String>? applyMonths;
  List<String>? applyTools;
  String? applyAdditionalInfos;
  String? applySteps;
  List<String>? applyMaterials;
  List<FilePart>? gallery;
  List<String>? maintenanceMonths;
  String? maintenanceAdditionalInfos;
  String? maintenanceFrequency;
  String? maintenanceSteps;
  String? maintenanceSpecialFeatures;
  List<String>? maintenanceTools;
  String? sources;

  String? costs;
  String? costsUnit;

  String? measureType;
  List<String>? measureGroups;
  List<String>? measureCategories;

  factory MeasureComplete.fromJson(Map<String, dynamic> json) {
    List<String> helper = [];
    List<FilePart> helperFiles = [];

    MeasureComplete mc = MeasureComplete()
      ..id = json['id']
      ..name = json['field_3021886'];
    if (json['field_2402548'] != null && json['field_2402548'].isNotEmpty) {
      mc.image = FilePart.fromJson(json['field_2402548'][0]);
    } else {
      //mc.image =
    }
    mc.description = json['field_2402431'];
    mc.target = json['field_2402433'];

    if (json['field_2402540'] != null) {
      json['field_2402540'].forEach((element) {
        final IdValue idv = IdValue.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.fundingPrograms = helper;
    helper = [];
    if (json['field_2402427'] != null) {
      json['field_2402427'].forEach((element) {
        final IdValueColor idv = IdValueColor.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.measureCategories = helper;
    mc.measureGroups = [IdValueColor.fromJson(json['field_2402473']).value!];
    mc.measureType = json['field_2402426'];
    helper = [];
    mc.sizeOrArea = [
      json['field_2402445'],
      'Höhe: ${json['field_2532843']} - ${json['field_2532844']}mm'
    ];
    if (json['field_2532846'] != null) {
      json['field_2532846'].forEach((element) {
        mc.growth = IdValueColor.fromJson(element).value;
      });
    }
    if (json['field_2532849'] != null) {
      mc.climate = IdValueColor.fromJson(json['field_2532849']).value;
    }
    if (json['field_2532853'] != null) {
      mc.soilWater = json['field_2532853'];
    }
    if (json['field_2532848'] != null) {
      mc.riskOfSpottedWingDrosophila =
          IdValueColor.fromJson(json['field_2532848']).value;
    }
    if (json['field_2532860'] != null) {
      json['field_2532860'].forEach((element) {
        final IdValueColor idv = IdValueColor.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.region = helper;
    helper = [];
    mc.location = json['field_2402444'];
    mc.applyCosts = json['field_2402448'];
    mc.applyDuration = json['field_2429064'];
    if (json['field_2402440'] != null) {
      json['field_2402440'].forEach((element) {
        final IdValueColor idv = IdValueColor.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.applyMonths = helper;
    helper = [];
    mc.applyAdditionalInfos = json['field_2429782'];
    mc.applySteps = json['field_2402443'];
    if (json['field_2498056'] != null) {
      json['field_2498056'].forEach((element) {
        final IdValueColor idv = IdValueColor.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.maintenanceMonths = helper;
    helper = [];
    if (json['field_2429460'] != null) {
      json['field_2429460'].forEach((element) {
        final IdValueColor idv = IdValueColor.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.applyTools = helper;
    helper = [];
    if (json['field_2428593'] != null) {
      json['field_2428593'].forEach((element) {
        final IdValueColor idv = IdValueColor.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.applyMaterials = helper;
    helper = [];

    if (json['field_2402454'] != null) {
      json['field_2402454'].forEach((element) {
        final IdValueColor idv = IdValueColor.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.maintenanceFrequency = helper.join(', ');
    helper = [];
    mc.maintenanceAdditionalInfos = json['field_2463465'];
    mc.maintenanceSteps = json['field_2402452'];
    mc.maintenanceSpecialFeatures = json['field_2402455'];
    if (json['field_2429972'] != null) {
      json['field_2429972'].forEach((element) {
        final IdValueColor idv = IdValueColor.fromJson(element);
        helper.add(idv.value!);
      });
    }
    mc.maintenanceTools = helper;
    helper = [];
    mc.sources = json['field_2402437'];
    if (json['field_3021896'] != null) {
      json['field_3021896'].forEach((element) {
        final FilePart fp = FilePart.fromJson(element);
        helperFiles.add(fp);
      });
    }
    mc.gallery = helperFiles;
    helperFiles = [];

    if (json['field_3021904'] != null) {
      mc.costsUnit = IdValueColor.fromJson(json['field_3021904']).value;
    }
    mc.costs = json['field_3021892'];

    return mc;
  }

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

extension MeasureCompleteExtension on MeasureComplete {
  CachedNetworkImage? getFullImage() {
    CachedNetworkImage? cachedImage;
    if (image != null) {
      if (image!.url != null) {
        cachedImage = CachedNetworkImage(
          imageUrl: image!.url!,
          placeholder: (context, url) => const CircularProgressIndicator(),
          errorWidget: (context, url, error) => const Icon(Icons.error),
          fit: BoxFit.cover,
        );
      }
    }
    return cachedImage;
  }

  List<Widget>? getGalleryThumbnails() {
    List<Widget>? images = [];
    if (gallery != null && gallery!.isNotEmpty) {
      for (final FilePart image in gallery!) {
        if (image.thumbnails?.cardCover?.url != null) {
          images.add(
            Padding(
              padding: const EdgeInsets.all(10),
              child: CachedNetworkImage(
                imageUrl: image.thumbnails!.cardCover!.url!,
                placeholder: (context, url) =>
                    const CircularProgressIndicator(),
                errorWidget: (context, url, error) => const Icon(Icons.error),
              ),
            ),
          );
        }
      }
    }
    return (images.isNotEmpty) ? images : null;
  }
}
