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

part 'login.g.dart';

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

  @JsonKey(name: 'field_3136271')
  String? username;
  @JsonKey(name: 'field_3136278')
  String? password;
  @JsonKey(name: 'field_3136279')
  List<FilePart>? images;
  @JsonKey(name: 'field_3136280')
  String? salutation;
  @JsonKey(name: 'field_3136281')
  String? title;
  @JsonKey(name: 'field_3136282')
  String? firstName;
  @JsonKey(name: 'field_3136283')
  String? lastName;
  @JsonKey(name: 'field_3136294')
  String? phone;
  @JsonKey(name: 'field_3136284')
  String? email;
  @JsonKey(name: 'field_3136285')
  List<IdValue>? business;

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

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

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