import 'dart:convert'; import 'package:latlong2/latlong.dart'; import 'package:niogu_ecommerce_v1/core/constant/app_key.dart'; import 'package:niogu_ecommerce_v1/features/cart/domain/entities/cart.dart'; import 'package:shared_preferences/shared_preferences.dart'; class SystemSetting { static Future getTenantCode() async { final prefs = await SharedPreferences.getInstance(); return await prefs.getString(AppKey.TENANT_CODE); } static Future getCurrentOutletId() async { final prefs = await SharedPreferences.getInstance(); return await prefs.getString(AppKey.OUTLET_ID); } static Future getCurrentOutletName() async { final prefs = await SharedPreferences.getInstance(); return await prefs.getString(AppKey.OUTLET_NAME); } static Future getCurrentOutletPhone() async { final prefs = await SharedPreferences.getInstance(); return await prefs.getString(AppKey.OUTLET_PHONE); } static Future getCurrentOutletLocation() async { final prefs = await SharedPreferences.getInstance(); return await prefs.getString(AppKey.OUTLET_LOCATION); } static Future getCurrentOutletCoordinate() async { final prefs = await SharedPreferences.getInstance(); final latitude = await prefs.getDouble(AppKey.OUTLET_LATITUDE); final longitude = await prefs.getDouble(AppKey.OUTLET_LONGITUDE); if (latitude == null || longitude == null) return null; return LatLng(latitude, longitude); } static Future switchOutlet({ required String outletId, required String outletName, String? outletPhone, String? outletLocation, LatLng? outletCoordinate, }) async { final prefs = await SharedPreferences.getInstance(); prefs.setString(AppKey.OUTLET_ID, outletId); prefs.setString(AppKey.OUTLET_NAME, outletName); if (outletPhone != null) { prefs.setString(AppKey.OUTLET_PHONE, outletPhone); } if (outletLocation != null && outletCoordinate != null) { prefs.setString(AppKey.OUTLET_LOCATION, outletLocation); prefs.setDouble(AppKey.OUTLET_LATITUDE, outletCoordinate.latitude); prefs.setDouble(AppKey.OUTLET_LONGITUDE, outletCoordinate.longitude); } } static Future isLoggedIn() async { final prefs = await SharedPreferences.getInstance(); return await prefs.getBool(AppKey.CUSTOMER_LOGIN) ?? false; } static Future setIsLoggedIn() async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(AppKey.CUSTOMER_LOGIN, true); } static Future getCurrentCustomerId() async { final prefs = await SharedPreferences.getInstance(); return prefs.getString(AppKey.CUSTOMER_ID); } static Future getCurrentCustomerName() async { final prefs = await SharedPreferences.getInstance(); return prefs.getString(AppKey.CUSTOMER_NAME); } static Future getCurrentCustomerEmail() async { final prefs = await SharedPreferences.getInstance(); return prefs.getString(AppKey.CUSTOMER_EMAIL); } static Future getCurrentCustomerPhone() async { final prefs = await SharedPreferences.getInstance(); return prefs.getString(AppKey.CUSTOMER_PHONE); } static Future setCustomerInfo({ String? id, String? name, String? email, String? phoneNumber, }) async { final prefs = await SharedPreferences.getInstance(); if (id != null) { prefs.setString(AppKey.CUSTOMER_ID, id); } if (name != null) { prefs.setString(AppKey.CUSTOMER_NAME, name); } if (email != null) { prefs.setString(AppKey.CUSTOMER_EMAIL, email); } if (phoneNumber != null) { prefs.setString(AppKey.CUSTOMER_PHONE, phoneNumber); } } static Future> getCartItemByOutlet() async { final prefs = await SharedPreferences.getInstance(); final jsonList = prefs.getStringList(AppKey.CART_ITEM_BY_OUTLET); if (jsonList == null) return []; return jsonList.map((json) => CartItem.fromJson(jsonDecode(json))).toList(); } static Future saveCartItemByOutlet(List cartItems) async { final prefs = await SharedPreferences.getInstance(); final data = cartItems .map((cartItem) => jsonEncode(cartItem.toJson())) .toList(); prefs.setStringList(AppKey.CART_ITEM_BY_OUTLET, data); } static Future clear() async { final prefs = await SharedPreferences.getInstance(); await prefs.clear(); } }