import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:resume/constants.dart';

class ContentProvider {
  ContentProvider._();

  static const String _baseJsonPath = 'assets/content/content';

  static Future<bool> init(BuildContext context) async {
    try {
      // Get the current locale
      final String currentLocale = Localizations.localeOf(context).languageCode;

      // Construct the path with the locale
      final String localizedPath = '${_baseJsonPath}_$currentLocale.json';

      // Try to load the localized version first
      try {
        String file = await rootBundle.loadString(localizedPath);
        _content = json.decode(file);
        return true;
      } catch (e) {
        // If localized version fails, fall back to default English
        String file = await rootBundle.loadString('${_baseJsonPath}_de.json');
        _content = json.decode(file);
        return true;
      }
    } catch (e) {
      print('Error loading content: $e');
      return false;
    }
  }

  static Map<String, dynamic> _content = {
    'experience': <List<dynamic>>[],
    'education': <List<dynamic>>[],
    'skills': <List<dynamic>>[],
    'text': <String>[],
    'general_skills': <String>[],
  };

  static T getContent<T>(ContentType contentType) {
    switch (contentType) {
      case ContentType.experience:
        return _content['experience'] as T;
      case ContentType.education:
        return _content['education'] as T;
      case ContentType.skills:
        return _content['skills'] as T;
      case ContentType.text:
        return _content['text'] as T;
      case ContentType.generalSkills:
        return _content['general_skills'] as T;
      default:
        return [] as T;
    }
  }
}
