import 'package:flutter/material.dart';
import 'package:initial_folder/models/banners_model.dart';
import 'package:initial_folder/services/banners_service.dart';

enum ResultState { Loading, NoData, HasData, Error }

class BannersProvider with ChangeNotifier {
  final BannersService bannersService;
  BannersProvider({required this.bannersService}) {
    getAllBanners();
  }
  List<BannersModel> _banners = [];

  ResultState? _state;

  String _message = '';
  String _status = '';

  List<BannersModel> get result => _banners;

  ResultState? get state => _state;

  String get message => _message;
  String get status => _status;

  set banners(List<BannersModel> banners) {
    _banners = banners;
    notifyListeners();
  }

  Future<dynamic> getAllBanners() async {
    try {
      _state = ResultState.Loading;
      notifyListeners();
      var result = await bannersService.getAllBanners();
      if (result['status'] == 404) {
        _state = ResultState.NoData;
        print("Harusnya ke sini");
        notifyListeners();
        return _message = 'Banner kosong';
      } else {
        _state = ResultState.HasData;
        notifyListeners();
        return _banners = result['data'];
      }
    } catch (e) {
      _state = ResultState.Error;
      print('Gagal banner provider $e');
      notifyListeners();
      return _message = 'Error --> $e';
    }
  }
}
