import 'package:aman_kassa_flutter/core/models/money.dart';
import 'package:aman_kassa_flutter/core/models/smena.dart';
import 'package:aman_kassa_flutter/core/models/user.dart';
import 'package:meta/meta.dart';

@immutable
class UserState {
  final bool? isError;
  final bool? isLoading;
  final bool? isAuthenticated;
  final String? authenticateType;
  final String? login;
  final String? password;
  final LoginFormMessage? loginFormMessage;
  final User? user;
  final Smena? smena;
  final Money? money;


  UserState(
      {this.isError,
      this.isLoading,
      this.isAuthenticated,
      this.authenticateType,
      this.login,
      this.password,
      this.user,
      this.loginFormMessage,
      this.smena,
      this.money,
      });

  factory UserState.initial(UserState? payload) => UserState(
        isLoading: false,
        isError: false,
        isAuthenticated: false,
        loginFormMessage: LoginFormMessage(),
        smena: Smena(),
        user: payload?.user ?? User(),
        authenticateType: payload?.authenticateType ?? null,
        login: payload?.login ?? null,
        password: payload?.password ?? null,
        money: Money(loading: false),
      );

  UserState copyWith({
    @required bool? isError,
    @required bool? isLoading,
    @required User? user,
    @required bool? isAuthenticated,
    @required LoginFormMessage? loginFormMessage,
    @required Smena? smena,
    @required String? authenticateType,
    @required String? login,
    @required String? password,
    @required Money? money,
  }) {
    return UserState(
      isError: isError ?? this.isError,
      isLoading: isLoading ?? this.isLoading,
      isAuthenticated: isAuthenticated ?? this.isAuthenticated,
      user: user ?? this.user,
      loginFormMessage: loginFormMessage ?? this.loginFormMessage,
      smena: smena ?? this.smena,
      authenticateType: authenticateType ?? this.authenticateType,
      login: login ?? this.login,
      password: password ?? this.password,
      money: money ?? this.money,
    );
  }

  static UserState? fromJson(dynamic json) {
    return json != null
        ? UserState(
            user: User.fromJson(json['user']),
            authenticateType: json['authenticateType'],
            login: json['login'],
            password: json['password'],
    )
        : null;
  }

  dynamic toJson() {
    return {
      "user": user != null ? user!.toJson() : null,
      "authenticateType": authenticateType,
      "login": login,
      "password": password,
    };
  }
}

class LoginFormMessage {
  final String? email;
  final String? password;
  final String? message;

  LoginFormMessage({this.email, this.password, this.message});
}
