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<String?> getTenantCode() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.TENANT_CODE);
  }

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

    return await prefs.getString(AppKey.OUTLET_ID);
  }

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

    return await prefs.getString(AppKey.OUTLET_NAME);
  }

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

    return await prefs.getString(AppKey.OUTLET_PHONE);
  }

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

    return await prefs.getString(AppKey.OUTLET_LOCATION);
  }

  static Future<LatLng?> 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<void> 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<bool> isLoggedIn() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getBool(AppKey.CUSTOMER_LOGIN) ?? false;
  }

  static Future<void> setIsLoggedIn() async {
    final prefs = await SharedPreferences.getInstance();

    await prefs.setBool(AppKey.CUSTOMER_LOGIN, true);
  }

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

    return prefs.getString(AppKey.CUSTOMER_ID);
  }

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

    return prefs.getString(AppKey.CUSTOMER_NAME);
  }

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

    return prefs.getString(AppKey.CUSTOMER_EMAIL);
  }

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

    return prefs.getString(AppKey.CUSTOMER_PHONE);
  }

  static Future<void> 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<List<CartItem>> 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<void> saveCartItemByOutlet(List<CartItem> 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<void> clear() async {
    final prefs = await SharedPreferences.getInstance();

    await prefs.clear();
  }
}
