
import 'package:aman_kassa_flutter/core/models/halyk/halyk_post_session.dart';
import 'package:aman_kassa_flutter/core/models/forte/forte_post_session.dart';
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
import 'package:meta/meta.dart';

@immutable
class BankState {
  final String? login;
  final String? password;
  final dynamic session;
  final String? sessionType;
  final bool? loading;

  BankState({this.login, this.password, this.session,this.loading,this.sessionType});

  //read hive
  factory BankState.initial(BankState? payload) {
    return BankState(
        login: payload?.login,
        password: payload?.password,
        session: payload?.session,
        loading: payload?.loading,
        sessionType: payload?.sessionType,
    );
  }

  //write hive
  BankState copyWith({
    @required login,
    @required password,
    @required session,
    @required loading,
    @required sessionType,
  }) {
    return BankState(
      login: login ?? this.login,
      password: password ?? this.password,
      session: session ?? this.session,
      loading: loading ?? this.loading,
      sessionType: sessionType ?? this.sessionType
    );
  }

  static BankState? fromJson(Map<String, dynamic> json) {
    if (json == null) return null;

    // Определяем тип сессии
    dynamic session;
    final String? sessionType = json['sessionType'] as String?;
    if (sessionType == "Halyk") {
      session = HalykPosSession.fromJson(json['session']);
    } else if (sessionType == "Forte") {
      session = FortePosSession.fromJson(json['session']);
    }

    return BankState(
      login: json['login'],
      password: json['password'],
      session: session,
      sessionType: sessionType,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      "login": login,
      "password": password,
      "sessionType": sessionType,
      "session": session?.toJson(),
    };
  }

  @override
  String toString() {
    return 'BankState(login: $login, password: $password, sessionType: $sessionType, session: $session)';
  }
}
