import 'package:test_sa/controllers/notification/firebase_notification_manger.dart';
import 'package:test_sa/models/department.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/hospital.dart';

class User {
  String id;
  String userName;
  String password;
  String email;
  Hospital hospital;
  Department department;
  UsersTypes type;
  String phoneNumber;
  String whatsApp;
  String token;
  bool isActive;
  DateTime tokenLife;

  User(
      {this.id,
      this.userName = "",
      this.email = "",
      this.password = "",
      this.phoneNumber = "",
      this.hospital,
      this.department,
      this.type,
      this.whatsApp,
      this.token,
      this.tokenLife,
      this.isActive = false});

  Future<Map<String, dynamic>> toLoginJson() async {
    if (FirebaseNotificationManger.token == null) await FirebaseNotificationManger.getToken();
    return {
      "username": userName,
      "password": password,
      "firebase_token": FirebaseNotificationManger?.token ?? "",
    };
  }

  Map<String, dynamic> toUpdateProfileJson() {
    Map<String, dynamic> jsonObject = {};
    if (department?.id != null) jsonObject["department"] = department.id;
    if (whatsApp != null && whatsApp.isNotEmpty) jsonObject["whatsapp"] = whatsApp;
    if (phoneNumber != null && phoneNumber.isNotEmpty) jsonObject["phone"] = phoneNumber;
    return jsonObject;
  }

  Future<Map<String, dynamic>> toRegisterJson() async {
    if (FirebaseNotificationManger.token == null) await FirebaseNotificationManger.getToken();
    return {
      "username": userName,
      "email": email,
      "whatsapp": whatsApp,
      "client": hospital.id,
      "department": department?.id,
      "phone": phoneNumber,
      "pass": password,
      "firebase_token": FirebaseNotificationManger?.token ?? "",
    };
  }

  Map<String, dynamic> toJson() {
    return {
      "userID": id,
      "username": userName,
      "email": email,
      "token": token,
      "phoneNumber": phoneNumber,
      "whatsapp": whatsApp,
      "client_id": hospital?.id,
      "client_name": hospital?.name,
      "department_id": department?.id,
      "department_name": department?.name,
      //"password":password,
      "tokenlife": tokenLife.toIso8601String(),
      "active": isActive,
      "userRoles": type == UsersTypes.engineer ? "value: R-6" : "value: R-5",
      //  "token":token, pass is token
    };
  }

  factory User.fromJson(Map<String, dynamic> parsedJson) {
    UsersTypes type;
    if (parsedJson["userRoles"].toString().contains("value: R-4") || parsedJson["userRoles"].toString().contains("value: R-5") || parsedJson["userRoles"].toString().contains("value: R-7")) {
      type = UsersTypes.normal_user;
    } else {
      type = UsersTypes.engineer;
    }
    return User(
        id: parsedJson["userID"],
        userName: parsedJson["username"],
        email: parsedJson["email"],
        hospital: Hospital(id: parsedJson["client_id"], name: parsedJson["client_name"]),
        department: Department(
          id: parsedJson["department_id"],
          name: parsedJson["department_name"],
        ),
        phoneNumber: parsedJson["phoneNumber"],
        whatsApp: parsedJson["phoneNumber"],
        token: parsedJson["token"],
        isActive: parsedJson["isAuthenticated"],
        tokenLife: DateTime.tryParse(parsedJson["tokenlife"] ?? ""),
        type: type);
  }
}
