import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:initial_folder/services/coupon_service.dart';

enum ResultState { Loading, NoData, HasData, Error }

class CouponCourseProvider with ChangeNotifier {
  final CouponService couponService;
  String coupon;
  CouponCourseProvider({required this.couponService, required this.coupon}) {
    getDiscountCourse(coupon);
  }
  dynamic _data;

  ResultState? _state;

  String _message = '';

  dynamic get result => _data;

  ResultState? get state => _state;

  String get message => _message;

  set data(dynamic data) {
    _data = data;
    notifyListeners();
  }

  Future<dynamic> getDiscountCourse(coupon) async {
    try {
      _state = ResultState.Loading;
      notifyListeners();
      dynamic data = await couponService.getDiscountCourse(coupon);
      if (data.isEmpty) {
        _state = ResultState.NoData;
        notifyListeners();
        return _message = 'Empty Data';
      } else {
        _state = ResultState.HasData;
        print("ini berhasil provider kupon ${_data}");
        notifyListeners();
        return _data = data;
      }
    } catch (e) {
      _state = ResultState.Error;
      print("Gagal kupon ${e}");
      notifyListeners();
      return _message = 'Error --> $e';
    }
  }
}
