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;
  DateTime? syncedDate;
  DateTime? trialDate;
  String? firebaseRegToken;
  String? lang;
  int? lifeLong;

  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.lifeLong = json['lifeLong'];

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

    this.dataPolicyAllowed = json['dataPolicyAllowed'];
    this.emailSubscription = json['emailSubscription'];
    this.sportId = json['sportId'];
    this.syncedDate = json['syncedDate'] == null ? null : DateTime.parse(json['syncedDate']);
    this.trialDate = json['trialDate'] == null ? null : DateTime.parse(json['trialDate']);

    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,
        "syncedDate": this.syncedDate == null ? null : DateFormat('yyyy-MM-dd HH:mm:ss').format(this.syncedDate!),
        "trialDate": this.trialDate == null ? null : DateFormat('yyyy-MM-dd HH:mm:ss').format(this.trialDate!),
        "firebaseRegToken": this.firebaseRegToken,
        "lang": this.lang,
        "lifeLong": this.lifeLong,
      };

  @override
  String toString() => this.toJson().toString();

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