import '../controllers/notification/firebase_notification_manger.dart';
import 'department.dart';
import 'enums/user_types.dart';
import 'hospital.dart';

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

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

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

  Map<String, dynamic> toUpdateProfileJson() {
    Map<String, dynamic> jsonObject = {};
    if (department?.id != null && (department?.id?.isNotEmpty ?? false)) jsonObject["department"] = department?.id ?? "";
    if (whatsApp != null && (whatsApp?.isNotEmpty ?? false)) jsonObject["whatsapp"] = whatsApp;
    if (phoneNumber != null && (phoneNumber?.isNotEmpty ?? false)) 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 {
      "uid": id,
      "name": userName,
      "email": email,
      "token": token,
      "phone": phoneNumber,
      "whatsapp": whatsApp,
      "client": hospital?.id,
      "client_name": hospital?.name,
      "department": department?.id,
      "department_name": department?.name,
      "password": password,
      "picture": image,
      "active": isActive ? 1 : 0,
      "role": type == UsersTypes.engineer ? "field_engineer" : "normal_user",
      //  "token":token, pass is token
    };
  }

  factory User.fromJson(Map<String, dynamic> parsedJson) {
    UsersTypes type;
    switch (parsedJson["role"]) {
      case "field_engineer":
        type = UsersTypes.engineer;
        break;
      default:
        type = UsersTypes.normal_user;
        break;
    }
    return User(
        id: parsedJson["uid"],
        userName: parsedJson["name"] ?? parsedJson["title"],
        email: parsedJson["mail"] ?? parsedJson["email"],
        hospital: Hospital(id: parsedJson["client"], name: parsedJson["client_name"]),
        department: Department(
          id: parsedJson["department"],
          name: parsedJson["department_name"],
        ),
        image: parsedJson["picture"],
        phoneNumber: parsedJson["phone"],
        whatsApp: parsedJson["whatsapp"],
        token: parsedJson["token"],
        isActive: parsedJson["active"] == "1",
        type: type);
  }
}
