import 'dart:developer';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:kmobile/config/theme_type.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'theme_state.dart';

class ThemeCubit extends Cubit<ThemeState> {
  ThemeCubit() : super(const ThemeState(themeType: ThemeType.violet)) {
    loadTheme();
  }

  Future<void> loadTheme() async {
    final prefs = await SharedPreferences.getInstance();
    final themeIndex = prefs.getInt('theme_type') ?? 0;
    final type = ThemeType.values[themeIndex];
    emit(ThemeState(themeType: type));
  }

  Future<void> changeTheme(ThemeType type) async {
    log("Attempting to change theme...");
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt('theme_type', type.index);
    emit(ThemeState(themeType: type));
  }
}
