import 'dart:convert';

import 'package:mohem_flutter_app/classes/consts.dart';
import 'package:shared_preferences/shared_preferences.dart';

class PrivilegeListModel {
  int? iD;
  String? serviceName;
  bool? previlege;

  PrivilegeListModel({this.iD, this.serviceName, this.previlege});

  PrivilegeListModel.fromJson(Map<String, dynamic> json) {
    iD = json['ID'];
    serviceName = json['ServiceName'];
    previlege = json['Previlege'];
  }

  Map<String, dynamic> toJson() {
    Map<String, dynamic> data = new Map<String, dynamic>();
    data['ID'] = this.iD;
    data['ServiceName'] = this.serviceName;
    data['Previlege'] = this.previlege;
    return data;
  }

  static Future<List<PrivilegeListModel>> getFromPrefs() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List encodedList = prefs.getStringList(SharedPrefsConsts.privilegeList) ?? [];
    return encodedList.map((e) => PrivilegeListModel.fromJson(jsonDecode(e))).toList();
  }

  static void saveToPrefs(List<PrivilegeListModel> list) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<String> encodedList = list.map((e) => jsonEncode(e.toJson())).toList();
    await prefs.setStringList(SharedPrefsConsts.privilegeList, encodedList);
  }
}
