import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:mobdr/old/login.dart';

class LocalStorage {
//  static Future<SharedPreferences> get securePref async =>
//      await SharedPreferences.getInstance();

  //static FlutterSecureStorage get securePref => FlutterSecureStorage();
  /*
  static Future eraseProfileData() async {
    var loginData = (await getLoginData());
    loginData?.guid = '';
    //await saveLoginData(LoginModel: loginData);
  }
  */

  /*
  static Future<LoginModel?> getLoginData() async {
    try {
      //var loginData = await securePref.read(key: 'loginData');
      //return LoginModel.fromJson(json.decode(loginData!));
      return null;
    } catch (e) {
      return null;
    }
  }
  */

  static Future<LoginModel?> getLoginData() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();

    Map<String, dynamic> loginMap = {};
    final String? loginStr = prefs.getString('loginData');
    if (loginStr != null) {
      loginMap = jsonDecode(loginStr) as Map<String, dynamic>;
    }

    final LoginModel login = LoginModel.fromJson(loginMap);
    print(login);
    return login;
  }

  static Future saveLoginData(LoginModel loginModel) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    bool result = await prefs.setString('loginData', jsonEncode(loginModel));
    print(result);
  }

  /*
  static Future saveLoginData(LoginModel loginModel) async {
    return securePref.write(
        key: 'loginData', value: json.encode(loginModel.toJson()));
  }
  */

  /*
  static Future<UserProfileModel?> getProfileData() async {
    try {
      var profileData = await securePref.read(key: 'profileData');
      return UserProfileModel.fromJson(json.decode(profileData!));
    } catch (e) {
      return null;
    }
  }

  static Future saveProfileData(UserProfileModel profileModel) async {
    try {
      return securePref.write(
          key: 'profileData', value: json.encode(profileModel.toJson()));
    } catch (e) {
      return null;
    }
  }

  static Future saveItem({@required item, @required key}) async {
    securePref.write(key: key.toString(), value: item);
  }

  static Future eraseItem({@required key}) async {
    securePref.delete(key: '$key');
  }

  static Future<bool> keyExists(String key) async {
    return securePref.containsKey(key: key);
  }
  */

  /*
  static eraseAll() async {
    var savedData = await getSavedBooks();
    for (var item in savedData) {
      try {
        await File(await downloadDir(item.id)).delete();
      } catch (e) {}
    }
    securePref.deleteAll();
    (await prefs).clear();
    return;
  }

  static Future<dynamic> getItemData({@required key}) async {
    return await securePref.containsKey(key: '$key')
        ? securePref.read(key: '$key')
        : null;
  }
  */
}
