import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:demo_package_ui_semantic/helpers/helpers.dart';

@immutable
class AuthModel {
  final String? status;
  final ResultAuthModel? data;

  AuthModel({
    this.status,
    this.data,
  });

  AuthModel copyWith(ResultAuthModel data) =>
    AuthModel(
      status: this.status,
      data: data
    );

  Map<String, dynamic> toMap() => {
    "status": status,
    "data": data == null ? null : data!.toMap(),
  };

  factory AuthModel.fromMap(Map<String, dynamic> json) {
    Map checkResult = checkAPIResultStatus(json);
    if (checkResult['error']) {
      return AuthModel(
        status: checkResult['message'],
        data: null,
      );
    } else {
      return AuthModel(
        status: json['status'],
        data: ResultAuthModel.fromJson(json["data"]),
      );
    }
  }

  String toJson() => json.encode(toMap());

  factory AuthModel.fromJson(String source) => AuthModel.fromMap(json.decode(source));

  @override
  String toString() {
    return 'AuthModel(status: $status, data: ${data!.toJson()})';
  }
}

@immutable
class ResultAuthModel {
  final int? userId;
  final String? userName;
  final String? isSa;
  final String? active;
  final String? token;
  final String? email;

  ResultAuthModel({
    this.userId,
    this.userName,
    this.isSa,
    this.active,
    this.token,
    this.email
  });

  ResultAuthModel copyWith(String email) =>
    ResultAuthModel(
      userId: this.userId,
      userName: this.userName,
      isSa: this.isSa,
      active: this.active,
      token: this.token,
      email: this.email
    );

  Map<String, dynamic> toMap() => {
    "user_id": userId,
    "user_name": userName,
    "is_sa": isSa,
    "active": active,
    "token": token,
    "email": email
  };

  factory ResultAuthModel.fromMap(Map<dynamic, dynamic> json) => ResultAuthModel(
    userId: json["user_id"],
    userName: json["user_name"],
    isSa: json["is_sa"],
    active: json["active"],
    token: json["token"],
    email: json["email"]
  );

  String toJson() => json.encode(toMap());

  factory ResultAuthModel.fromJson(Map<String, dynamic> source) => ResultAuthModel.fromMap(source);

  @override
  String toString() {
    return 'ResultAuthModel(user_id: $userId, user_name: $userName, is_sa: $isSa, active: $active, token: $token, email: $email)';
  }
}

class LoginParam {
  final String? email;
  final String? password;
  final String? token;
  final int? userId;
  LoginParam({
    this.email,
    this.password,
    this.token,
    this.userId
  });
}