import 'dart:convert';

import 'package:ambito/src/entity/base_entity.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:isar/isar.dart';
import 'package:json_annotation/json_annotation.dart';

import '../../consts/consts.dart';
import '../_general/id_value/id_value.dart';

part 'area.g.dart';

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

  @JsonKey(name: 'field_3077525')
  String? name;
  @JsonKey(name: 'field_3077543')
  List<IdValue>? business;
  @JsonKey(name: 'field_3077526')
  String? description;
  @JsonKey(name: 'field_3077544')
  String? size;
  @JsonKey(name: 'field_3077545')
  String? polygon;

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

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

extension AreaExtension on Area {
  Polygon? toPolygon() {
    if (polygon == null) {
      return null;
    }

    final json = jsonDecode(polygon!);

    List<LatLng> points = [];

    for (final point in json) {
      points.add(LatLng(point[0], point[1]));
    }

    logger.d(points.toString());

    return Polygon(
      polygonId: PolygonId(
        id.toString(),
      ),
      strokeColor: const Color(0xFF60845E),
      fillColor: const Color(0xff87A34E).withValues(alpha: .7),
      strokeWidth: 2,
      points: points,
    );
  }
}
