import 'dart:collection';
import 'package:intl/intl.dart';

import 'customer_property.dart';

class Customer {
  String? name;
  String? email;
  String? firstname;
  String? sex;
  int? age;
  String? active;
  int? customerId;
  String? password;
  int? birthYear;
  String? goal;
  String? fitnessLevel;
  String? bodyType;
  int? admin;
  int? trainer;
  int? dataPolicyAllowed;
  String? firebaseUid;
  DateTime? dateAdd;
  DateTime? dateChange;
  int? emailSubscription;
  int? sportId;

  LinkedHashMap<String, CustomerProperty> properties = LinkedHashMap();

  Customer(
      {this.customerId,
      this.name,
      this.firstname,
      this.email,
      this.sex,
      this.age,
      this.active,
      this.password,
      this.birthYear,
      this.bodyType,
      this.fitnessLevel,
      this.goal,
      this.admin,
      this.trainer,
      this.dataPolicyAllowed,
      this.firebaseUid,
      this.dateAdd,
      this.dateChange}) {
    dateAdd = DateTime.now();
    dateChange = DateTime.now();
  }

  Customer.fromJson(Map json) {
    this.customerId = json['customerId'];
    this.name = json['name'];
    this.firstname = json['firstname'];
    this.email = json['email'];
    this.sex = json['sex'];
    this.age = json['age'];
    this.active = json['active'];
    this.birthYear = json['birthYear'];
    this.bodyType = json['bodyType'];
    this.fitnessLevel = json['fitnessLevel'];
    this.goal = json['goal'];
    this.admin = json['admin'];

    this.trainer = json['trainer'];
    this.firebaseUid = json['firebaseUid'];

    this.dataPolicyAllowed = json['dataPolicyAllowed'];
    this.emailSubscription = json['emailSubscription'];
    this.sportId = json['sportId'];

    this.dateAdd = json['dateAdd'] == null ? DateTime.parse("0000-00-00") : DateTime.parse(json['dateAdd']);
    this.dateChange = json['dateChange'] == null ? DateTime.parse("0000-00-00") : DateTime.parse(json['dateChange']);
  }

  Map<String, dynamic> toJson() => {
        "name": name,
        "firstname": firstname,
        "email": email,
        "age": age,
        "sex": sex,
        "active": 'Y',
        "password": password,
        "birthYear": birthYear,
        "bodyType": bodyType,
        "fitnessLevel": fitnessLevel,
        "goal": goal,
        "admin": admin,
        "trainer": trainer,
        "dataPolicyAllowed": dataPolicyAllowed,
        "dateAdd": DateFormat('yyyy-MM-dd HH:mm:ss').format(this.dateAdd!),
        "dateChange": DateFormat('yyyy-MM-dd HH:mm:ss').format(this.dateChange!),
        "emailSubscription": this.emailSubscription,
        "sportId": this.sportId
      };

  double getProperty(String propertyName) {
    if (this.properties[propertyName] == null) {
      return 0;
    } else {
      return this.properties[propertyName]!.propertyValue;
    }
  }

  setProperty(String propertyName, double value) {
    this.properties[propertyName]!.propertyValue = value;
  }
}
