import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:marking_app/common/model/marking/do_marking_keyboard_model.dart';
import 'package:marking_app/utils/fast_data.dart';
import 'package:marking_app/utils/index.dart';

/// 偏好设置主页
class UsePreference {
  ValueNotifier<DoMarkingKeyboardModel> preferenceState;

  UsePreference._({required this.preferenceState});

  // 工厂构造函数
  factory UsePreference.use({DoMarkingKeyboardModel? moel}) {
    return UsePreference._(preferenceState: useState(moel ?? DoMarkingKeyboardModel()));
  }

  /// 初始化偏好设置
  Future<DoMarkingKeyboardModel> initPreference() async {
    DoMarkingKeyboardModel? localModel = await FastData.getInstance().getPreferencesForWork();
    toPrint(val: '初始化偏好设置' + (localModel != null).toString());
    if (localModel != null) {
      preferenceState.value = localModel;
    }
    return preferenceState.value;
  }

  /// 保存偏好设置
  Future<void> savePreference({required DoMarkingKeyboardModel tempModel}) async {
    preferenceState.value = tempModel;
    bool flag = await FastData.getInstance().setPreferencesForWork(tempModel);
    toPrint(val: flag);
  }
}

/// 临时偏好设置
class UseTemPreference {
  ValueNotifier<DoMarkingKeyboardModel> preferenceTemState;

  UseTemPreference._({required this.preferenceTemState});

  // 工厂构造函数
  factory UseTemPreference.use() => UseTemPreference._(preferenceTemState: useState(DoMarkingKeyboardModel()));

  // 初始化
  init(DoMarkingKeyboardModel model) {
    preferenceTemState.value = DoMarkingKeyboardModel.fromJson(model.toJson());
  }
}
