import 'package:aman_kassa_flutter/core/models/bluetooth_device.dart';
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
import 'package:meta/meta.dart';

@immutable
class SettingState {
  final String? mode;
  final String? tradeType;
  final String? pinCode;
  final bool? pinLocked;
  final bool? pinSkip;
  final BluetoothDevice? printerBT;
  final String? printerEncoding;
  final String? printerPaperSize;


  SettingState({this.mode, this.tradeType, this.pinCode, this.pinLocked, this.pinSkip, this.printerBT,
      this.printerEncoding,
      this.printerPaperSize});

  //read hive
  factory SettingState.initial(SettingState? payload) {
    return SettingState(
        mode: payload?.mode ?? SettingModeKassa,
        tradeType: payload?.tradeType ?? SettingTradeTypeGood,
        pinCode: payload?.pinCode ?? null,
        pinLocked: true,
        pinSkip: false,
		printerBT: payload?.printerBT ?? null,
        printerEncoding:
            payload?.printerEncoding ?? SettingPrinterEncodingCp866,
        printerPaperSize: payload?.printerPaperSize ?? SettingPrinterPaperM58
    );
  }

  //write hive
  SettingState copyWith({
    @required mode,
    @required tradeType,
    @required pinCode,
    @required pinLocked,
    @required pinSkip,
	@required printerBT,
    @required printerEncoding,
    @required printerPaperSize,
  }) {
    return SettingState(
      mode: mode ?? this.mode,
      tradeType: tradeType ?? this.tradeType,
      pinCode: pinCode ?? this.pinCode,
      pinLocked: pinLocked ?? this.pinLocked,
      pinSkip: pinSkip ?? this.pinSkip,
		printerBT: printerBT ?? this.printerBT,
        printerEncoding: printerEncoding ?? this.printerEncoding,
        printerPaperSize: printerPaperSize ?? this.printerPaperSize
    );
  }

  static SettingState? fromJson(dynamic json) {
    return json != null
        ? SettingState(
            tradeType: json['tradeType'],
            mode: json['mode'],
            pinCode: json['pinCode'],
            pinLocked: json['pinLocked'],
            pinSkip: json['pinSkip'],
 			printerEncoding: json['printerEncoding'],
            printerPaperSize: json['printerPaperSize'],
            printerBT: json['printerBT'] != null
                ? BluetoothDevice.fromJson(json['printerBT'])
                : null,
          )
        : null;
  }

  dynamic toJson() {
    return {
      "tradeType": tradeType,
      "mode": mode,
      "pinCode": pinCode,
      "pinLocked" : pinLocked,
      "pinSkip" : pinSkip,
	  "printerBT": printerBT != null ? printerBT!.toJson() : null,
      "printerEncoding": printerEncoding,
      "printerPaperSize": printerPaperSize,
    };
  }
}
