import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

Future<String?> getToken() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString("auth_token");
}

Future<String?> getTripUserId() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString("trip_planned_user");
}

Future<String?> getLayoutColor() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString("layout_color");
}

Future<String?> getSecondaryColor() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString("secondary_color");
}

Future<String?> getTernaryColor() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString("ternary_color");
}

Future<String?> getBodyColor() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString("body_color");
}

Future<String?> getUserId() async {
  final prefs = await SharedPreferences.getInstance();
  final String? userDataString = prefs.getString('user_data');

  if (userDataString != null) {
    try {
      final Map<String, dynamic> userData = jsonDecode(userDataString);
      return userData["user_id"]?.toString();
    } catch (e) {
      return null;
    }
  }
  return null;
}

Future<String?> getOrgId() async {
  final prefs = await SharedPreferences.getInstance();
  final String? userDataString = prefs.getString('user_data');

  if (userDataString != null) {
    try {
      final Map<String, dynamic> userData = jsonDecode(userDataString);
      return userData["org_id"]?.toString();
    } catch (e) {
      return null;
    }
  }
  return null;
}

Future<String?> getRoleUser() async {
  final prefs = await SharedPreferences.getInstance();
  final String? userDataString = prefs.getString('user_data');

  print('getRoleUser');

  // print("UserDataSTr - $userDataString");

  if (userDataString != null) {
    try {
      final Map<String, dynamic> userData = jsonDecode(userDataString);
      return userData["role"]?.toString();
    } catch (e) {
      return null;
    }
  }
  return null;
}

Future<List<Map<String, dynamic>>> getOrgServicesName() async {
  final prefs = await SharedPreferences.getInstance();
  final jsonString = prefs.getString("selectedOrgServiceName");

  if (jsonString != null) {
    return List<Map<String, dynamic>>.from(jsonDecode(jsonString));
  }
  return [];
}

Future<String?> getForexCardNumber() async {
  final prefs = await SharedPreferences.getInstance();
  final String? userDataString = prefs.getString('user_data');
  // final String? userDataString = prefs.getString('user_data');

  if (userDataString != null) {
    try {
      final Map<String, dynamic> userData = jsonDecode(userDataString);
      return userData["forex_pre_paid_card_number"]?.toString();
    } catch (e) {
      return null;
    }
  }
  return null;
}

Future<String?> getTripPlanAction() async {
  final prefs = await SharedPreferences.getInstance();
  final String? userDataString = prefs.getString('user_data');

  if (userDataString != null) {
    try {
      final Map<String, dynamic> userData = jsonDecode(userDataString);
      return userData["plan_action"]?.toString();
    } catch (e) {
      return null;
    }
  }
  return null;
}

Future<String?> getTripPlanActionFromSelectedUsr() async {
  final prefs = await SharedPreferences.getInstance();
  // final String? userDataString = prefs.getString('user_data');
  final String? userDataString = prefs.getString('trip_user_data');

  if (userDataString != null) {
    try {
      final Map<String, dynamic> userData = jsonDecode(userDataString);
      print("TRIPUSRDATAT - $userData");
      return userData["plan_action"]?.toString();
    } catch (e) {
      return null;
    }
  }
  return null;
}

Future<List<Map<String, dynamic>>?> getUserServices() async {
  final prefs = await SharedPreferences.getInstance();
  final String? userDataString = prefs.getString('user_data');

  if (userDataString != null) {
    try {
      final Map<String, dynamic> userData = jsonDecode(userDataString);
      final List<dynamic>? services = userData["service"];

      if (services != null) {
        return services.cast<Map<String, dynamic>>();
      }
    } catch (e) {
      return null;
    }
  }
  return null;
}

Future<List<dynamic>?> fetchTripUserServices() async {
  String? orgId = await getOrgId();

  final prefs = await SharedPreferences.getInstance();
  final String? userDataString = prefs.getString('trip_user_data');

  if (userDataString != null) {
    try {
      final Map<String, dynamic> userData = jsonDecode(userDataString);
      print("TRIPUSRSERVICE - $userData");
      return userData["service"]; // this is a List<dynamic>
    } catch (e) {
      print("Error decoding trip_user_data: $e");
      return null;
    }
  }
  return null;
}
