import 'package:bloc/bloc.dart';
import 'package:e_commerce_training/src/model/db/dbhelper.dart';
import 'package:e_commerce_training/src/model/preference/app_preference.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/util/const.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:equatable/equatable.dart';

part 'setting_state.dart';

class SettingCubit extends Cubit<SettingState> {
  final AppRepository repository;
  bool isSwitched = true;

  int get point => appPreference.getIntData(Const.POINT) ?? 10;

  String? get name => appPreference.getData(Const.FULL_NAME);

  String? get email => appPreference.getData(Const.EMAIL);

  String? get avatar => appPreference.getData(Const.AVATAR);

  String? get joinAt => appPreference.getData(Const.DATE_TIME_CREATE_SV_FORMAT);

  bool get isLoggedIn => !Util.isEmpty(appPreference.getData(Const.TOKEN));

  SettingCubit(this.repository) : super(SettingInitial());

  void refreshSetting() {
    emit(SettingLoading());
    emit(SettingInitial());
  }

  void getSettingTask({bool isRefresh = false, bool isLoadMore = false}) {
    emit(!isRefresh && !isLoadMore? SettingLoading():SettingInitial()  );
  }

  void changeSwitched(bool isSwitched) {
    emit(SettingLoading());
    this.isSwitched = isSwitched;
    emit(SettingInitial());
  }

  Future<void> logOut() async {
    emit(SettingLoading());
    await DatabaseHelper().deleteAllProduct();
    appPreference.clearData();
    emit(LogoutSuccess());
  }
}
