// To parse this JSON data, do
//
//     final user = userFromJson(jsonString);

import 'dart:convert';
import 'dart:io';

import '../../classes/app_state.dart';
import '../../utils/enums.dart';

User userFromJson(String str) => User.fromJson(json.decode(str));

String userToJson(User data) => json.encode(data.toJson());

class User {
  User({
    this.totalItemsCount,
    this.data,
    this.messageStatus,
    this.message,
  });

  dynamic totalItemsCount;
  UserData? data;
  int? messageStatus;
  String? message;

  factory User.fromJson(Map<String, dynamic> json) => User(
        totalItemsCount: json["totalItemsCount"],
        data: json["data"] == null ? null : UserData.fromJson(json["data"]),
        messageStatus: json["messageStatus"],
        message: json["message"],
      );

  Map<String, dynamic> toJson() => {
        "totalItemsCount": totalItemsCount,
        "data": data?.toJson(),
        "messageStatus": messageStatus,
        "message": message,
      };
}

class UserData {
  UserData({
    this.accessToken,
    this.refreshToken,
    this.expiryDate,
    this.userInfo,
  });

  String? accessToken;
  String? refreshToken;
  DateTime? expiryDate;
  UserInfo? userInfo;

  factory UserData.fromJson(Map<String, dynamic> json) => UserData(
        accessToken: json["accessToken"],
        refreshToken: json["refreshToken"],
        expiryDate: json["expiryDate"] == null ? null : DateTime.parse(json["expiryDate"]),
        userInfo: json["userInfo"] == null ? null : UserInfo.fromJson(json["userInfo"]),
      );

  Map<String, dynamic> toJson() => {
        "accessToken": accessToken,
        "refreshToken": refreshToken,
        "expiryDate": expiryDate?.toIso8601String(),
        "userInfo": userInfo?.toJson(),
      };
}

class UserInfo {
  UserInfo(
      {this.id,
      this.userId,
      this.firstName,
      this.lastName,
      this.mobileNo,
      this.email,
      this.userImageUrl,
      this.roleId,
      this.roleName,
      this.isEmailVerified,
      this.serviceProviderBranch,
      this.isVerified,
      this.userRoles,
      this.isCustomer,
      this.isProviderDealership,
      this.isDealershipUser,
      this.providerId,
      this.customerId,
      this.countryId,
      this.cityId,
      this.dealershipId,
      this.userLocalImage});

  int? id;
  String? userId;
  String? firstName;
  String? lastName;
  String? mobileNo;
  String? email;
  dynamic userImageUrl;
  int? roleId;
  String? roleName;
  bool? isEmailVerified;
  List<dynamic>? serviceProviderBranch;
  bool? isVerified;
  List<dynamic>? userRoles;
  bool? isCustomer;
  bool? isProviderDealership;
  bool? isDealershipUser;
  dynamic providerId;
  int? customerId;
  int? cityId;
  int? countryId;
  dynamic dealershipId;
  File? userLocalImage;

  UserInfo.fromJson(Map<String, dynamic> json) {
    if (json["roleID"] == 5) {
      AppState().userType = UserType.providerDealer;
    } else if (json["roleID"] == 6) {
      AppState().userType = UserType.providerIndividual;
    } else if (json["roleID"] == 7) {
      AppState().userType = UserType.providerIndividual;
    } else {
      AppState().userType = UserType.customer;
    }
    id = json["id"];
    userId = json["userID"];
    firstName = json["firstName"];
    lastName = json["lastName"];
    mobileNo = json["mobileNo"];
    email = json["email"];
    userImageUrl = json["userImageUrl"];
    roleId = json["roleID"];
    roleName = json["roleName"];
    isEmailVerified = json["isEmailVerified"];
    serviceProviderBranch = json["serviceProviderBranch"] == null ? null : List<dynamic>.from(json["serviceProviderBranch"].map((x) => x));
    isVerified = json["isVerified"];
    userRoles = json["userRoles"] == null ? null : List<dynamic>.from(json["userRoles"].map((x) => x));
    isCustomer = json["isCustomer"];
    isProviderDealership = json["isProviderDealership"];
    isDealershipUser = json["isDealershipUser"];
    providerId = json["providerID"];
    customerId = json["customerID"];
    countryId = json["countryId"] ?? 1;
    cityId = json["cityId"] ?? 1;
    dealershipId = json["dealershipID"];
    userLocalImage = null;
  }

  // factory UserInfo.fromJson(Map<String, dynamic> json) => UserInfo(
  //       id: json["id"],
  //       userId: json["userID"],
  //       firstName: json["firstName"],
  //       lastName: json["lastName"],
  //       mobileNo: json["mobileNo"],
  //       email: json["email"],
  //       userImageUrl: json["userImageUrl"],
  //       roleId: json["roleID"],
  //       roleName: json["roleName"],
  //       isEmailVerified: json["isEmailVerified"],
  //       serviceProviderBranch: json["serviceProviderBranch"] == null ? null : List<dynamic>.from(json["serviceProviderBranch"].map((x) => x)),
  //       isVerified: json["isVerified"],
  //       userRoles: json["userRoles"] == null ? null : List<dynamic>.from(json["userRoles"].map((x) => x)),
  //       isCustomer: json["isCustomer"],
  //       isProviderDealership: json["isProviderDealership"],
  //       isDealershipUser: json["isDealershipUser"],
  //       providerId: json["providerID"],
  //       customerId: json["customerID"],
  //       dealershipId: json["dealershipID"],
  //     );

  Map<String, dynamic> toJson() => {
        "id": id,
        "userID": userId,
        "firstName": firstName,
        "lastName": lastName,
        "mobileNo": mobileNo,
        "email": email,
        "userImageUrl": userImageUrl,
        "roleID": roleId,
        "roleName": roleName,
        "isEmailVerified": isEmailVerified,
        "serviceProviderBranch": serviceProviderBranch == null ? null : List<dynamic>.from(serviceProviderBranch!.map((x) => x)),
        "isVerified": isVerified,
        "userRoles": userRoles == null ? null : List<dynamic>.from(userRoles!.map((x) => x)),
        "isCustomer": isCustomer,
        "isProviderDealership": isProviderDealership,
        "isDealershipUser": isDealershipUser,
        "providerID": providerId,
        "customerID": customerId,
        "dealershipID": dealershipId,
      };

  @override
  String toString() {
    return 'UserInfo{id: $id, userId: $userId, firstName: $firstName, lastName: $lastName, mobileNo: $mobileNo, email: $email, userImageUrl: $userImageUrl, roleId: $roleId, roleName: $roleName, isEmailVerified: $isEmailVerified, serviceProviderBranch: $serviceProviderBranch, isVerified: $isVerified, userRoles: $userRoles, isCustomer: $isCustomer, isProviderDealership: $isProviderDealership, isDealershipUser: $isDealershipUser, providerId: $providerId, customerId: $customerId, dealershipId: $dealershipId, userLocalImage: $userLocalImage}';
  }
}
