/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-14 11:56:52
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-07-28 15:22:41
 * @FilePath: \marking_app\lib\utils\fast_data.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:convert';

import 'package:marking_app/common/model/marking/do_marking_keyboard_model.dart';
import 'package:marking_app/common/model/marking/marking_history_zoom_info.dart';
import 'package:marking_app/common/model/marking/marking_keyboard_sliding_position.dart';
import 'package:marking_app/common/model/marking/marking_zoom.dart';
import 'package:marking_app/common/model/user/user_info.dart';
import 'package:marking_app/common/model/user/user_info_report.dart';
import 'package:shared_preferences/shared_preferences.dart';

class FastData {
  // token
  static const String _TOKEN = "APP:TOKEN";
  // 用户信息
  static const String _USER = "APP:USER";
  // 用户信息 报告
  static const String _USER_INFO_REPORT = "APP:USER:REPORT";
  // 用户密码
  static const String _USER_PWD_ACCOUNT = "APP:USER:PWD_ACCOUNT";
  // 键盘引导页
  static const String _INPUT_KEYBOARD_GUIDE_PAGE = "APP:MARKING:GUIDE_PAGE";
  // 阅卷 ==> 键盘下拉滚动位置
  static const String _MARKING_KEYBOARD_SLIDING_POSITION = "APP:MARKING:KEYBOARD_SLIDING_POSITION";
  // 阅卷 ==> 缩放大小历史记录
  static const String _MARKING_ZOOM_SCALE_AND_POSITION = "APP:MARKING:ZOOM_SCALE_AND_POSITION";
  // 阅卷偏好设置缓存
  static const String _MARKING_PREFERENCES = "APP:MARKING:PREFERENCES";
  // 阅卷 ==》 阅卷试题图缩放和位置
  static const String _MARKING_IMAGE_ZOOM = "APP:MARKING:IMAGE_ZOOM";
  // 阅卷偏好设置缓存 ==》 作业
  static const String _MARKING_PREFERENCES_WORK = "APP:MARKING:PREFERENCES:WORK";
  // 键盘引导页 ==》 作业
  static const String _INPUT_KEYBOARD_GUIDE_PAGE_WORK = "APP:MARKING:GUIDE_PAGE:WORK";

  static SharedPreferences? _prefs;

  // 单例模式固定格式
  FastData._();

  // 单例模式固定格式
  static FastData? _instance;

  // 单例模式固定格式
  static FastData getInstance() {
    _instance ??= FastData._();

    _instance!.getSharedInstance();
    return _instance!;
  }

  Future<SharedPreferences> getSharedInstance() async {
    _prefs ??= await SharedPreferences.getInstance();
    return _prefs!;
  }

  void cleanShared() {
    _prefs?.clear();
  }

  // token
  Future<bool> setToken(String token) async {
    SharedPreferences thePrefs = await getSharedInstance();

    return await thePrefs.setString(_TOKEN, token);
  }

  Future<String?> getToken() async {
    SharedPreferences thePrefs = await getSharedInstance();
    return thePrefs.getString(_TOKEN);
  }

  void cleanToken() async {
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_TOKEN);
  }

  //用户名
  Future<bool> setUser(UserInfo user) async {
    SharedPreferences thePrefs = await getSharedInstance();
    return await thePrefs.setString(_USER, jsonEncode(user.toJson()));
  }

  void cleanUser() async {
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_USER);
  }

  Future<String?> getUser() async {
    SharedPreferences thePrefs = await getSharedInstance();
    return thePrefs.getString(_USER);
  }

  /// 用户信息 报告
  Future<bool> setUserReport(UserInfoReport user) async {
    SharedPreferences thePrefs = await getSharedInstance();
    return await thePrefs.setString(_USER_INFO_REPORT, jsonEncode(user.toJson()));
  }

  // 清空用户信息
  void cleanUserReport() async {
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_USER_INFO_REPORT);
  }

  Future<String?> getUserReport() async {
    SharedPreferences thePrefs = await getSharedInstance();
    return thePrefs.getString(_USER_INFO_REPORT);
  }

  //用户密码和账号
  Future<bool> setUserPwdAndAccount(Map<String, String> mapData) async {
    SharedPreferences thePrefs = await getSharedInstance();

    return await thePrefs.setString(_USER_PWD_ACCOUNT, jsonEncode(mapData));
  }

  Future<String?> getUserPwd() async {
    SharedPreferences thePrefs = await getSharedInstance();
    return thePrefs.getString(_USER_PWD_ACCOUNT);
  }

  /* 输入型键盘引导页 */
  Future<bool> setInputKeyboardGuidePage(bool val) async {
    SharedPreferences thePrefs = await getSharedInstance();
    return await thePrefs.setBool(_INPUT_KEYBOARD_GUIDE_PAGE, val);
  }

  Future<bool> getInputKeyboardGuidePage() async {
    SharedPreferences thePrefs = await getSharedInstance();
    return thePrefs.getBool(_INPUT_KEYBOARD_GUIDE_PAGE) ?? true;
  }

  void cleanInputKeyboardGuidePage() async {
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_INPUT_KEYBOARD_GUIDE_PAGE);
  }

  /**  偏好设置 */
  Future<bool> setPreferences(DoMarkingKeyboardModel val) async {
    SharedPreferences thePrefs = await getSharedInstance();
    String jsonVal = jsonEncode(val.toJson());
    bool flag = await thePrefs.setString(_MARKING_PREFERENCES, jsonVal);
    return flag;
  }

  Future<DoMarkingKeyboardModel?> getPreferences() async {
    SharedPreferences thePrefs = await getSharedInstance();
    String? preferencesStr = thePrefs.getString(_MARKING_PREFERENCES);
    if (preferencesStr != null) {
      return DoMarkingKeyboardModel.fromJson(jsonDecode(preferencesStr));
    }
    return null;
  }

  void cleanPreferences() async {
    cleanInputKeyboardGuidePage();
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_MARKING_PREFERENCES);
  }

  /*  偏好设置 =》 作业 */
  Future<bool> setPreferencesForWork(DoMarkingKeyboardModel val) async {
    SharedPreferences thePrefs = await getSharedInstance();
    return await thePrefs.setString(_MARKING_PREFERENCES_WORK, jsonEncode(val.toJson()));
  }

  Future<DoMarkingKeyboardModel?> getPreferencesForWork() async {
    SharedPreferences thePrefs = await getSharedInstance();
    String? preferencesStr = thePrefs.getString(_MARKING_PREFERENCES_WORK);
    if (preferencesStr != null) {
      return DoMarkingKeyboardModel.fromJson(jsonDecode(preferencesStr));
    }
    return null;
  }

  void cleanPreferencesForWork() async {
    cleanInputKeyboardGuidePageForWork();
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_MARKING_PREFERENCES_WORK);
  }

  /* 输入型键盘引导页 ==》作业 */
  Future<bool> setInputKeyboardGuidePageForWork(bool val) async {
    SharedPreferences thePrefs = await getSharedInstance();
    return await thePrefs.setBool(_INPUT_KEYBOARD_GUIDE_PAGE_WORK, val);
  }

  Future<bool> getInputKeyboardGuidePageForWork() async {
    SharedPreferences thePrefs = await getSharedInstance();
    return thePrefs.getBool(_INPUT_KEYBOARD_GUIDE_PAGE_WORK) ?? true;
  }

  void cleanInputKeyboardGuidePageForWork() async {
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_INPUT_KEYBOARD_GUIDE_PAGE_WORK);
  }

  /* 阅卷 ==》 题型图片缩放比例和位置 */
  Future<bool> setMarkingPapersImageZoom(MarkingZoom zoomIamge) async {
    SharedPreferences thePrefs = await getSharedInstance();
    return await thePrefs.setString(_MARKING_IMAGE_ZOOM, jsonEncode(zoomIamge.toJson()));
  }

  Future<MarkingZoom?> getMarkingPapersImageZoom([SharedPreferences? thePrefs]) async {
    if (thePrefs == null) thePrefs = await getSharedInstance();
    String? valueStr = thePrefs.getString(_MARKING_IMAGE_ZOOM);
    if (valueStr != null) {
      try {
        return MarkingZoom.fromJson(jsonDecode(valueStr));
      } catch (e) {}
    }
    return null;
  }

  void cleanMarkingPapersImageZoom() async {
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_MARKING_IMAGE_ZOOM);
  }

  /* 阅卷 ==》 键盘滑动位置 */
  Future<bool> setMarkingKeyboardSlidingPosition(MarkingKeyboardSlidingPosition position) async {
    SharedPreferences thePrefs = await getSharedInstance();
    return await thePrefs.setString(_MARKING_KEYBOARD_SLIDING_POSITION, jsonEncode(position.toJson()));
  }

  Future<MarkingKeyboardSlidingPosition?> getMarkingKeyboardSlidingPosition([SharedPreferences? thePrefs]) async {
    if (thePrefs == null) thePrefs = await getSharedInstance();
    String? valueStr = thePrefs.getString(_MARKING_KEYBOARD_SLIDING_POSITION);
    if (valueStr != null) {
      try {
        return MarkingKeyboardSlidingPosition.fromJson(jsonDecode(valueStr));
      } catch (e) {}
    }
    return null;
  }

  void cleanMarkingKeyboardSlidingPosition() async {
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_MARKING_KEYBOARD_SLIDING_POSITION);
  }

  /* 阅卷 ==> 缩放组件历史位置 */
  Future<bool> setMarkingZoomInfo(MarkingHistoryZoomInfo info) async {
    SharedPreferences thePrefs = await getSharedInstance();
    return await thePrefs.setString(_MARKING_ZOOM_SCALE_AND_POSITION, jsonEncode(info.toJson()));
  }

  Future<MarkingHistoryZoomInfo?> getMarkingZoomInfo([SharedPreferences? thePrefs]) async {
    if (thePrefs == null) thePrefs = await getSharedInstance();
    String? valueStr = thePrefs.getString(_MARKING_ZOOM_SCALE_AND_POSITION);
    if (valueStr != null) {
      try {
        return MarkingHistoryZoomInfo.fromJson(jsonDecode(valueStr));
      } catch (e) {}
    }
    return null;
  }

  void cleanMarkingMarkingZoomInfo() async {
    SharedPreferences thePrefs = await getSharedInstance();
    thePrefs.remove(_MARKING_ZOOM_SCALE_AND_POSITION);
  }
}
