import 'dart:async';
import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:initial_folder/models/detail_course_model.dart';
import 'package:initial_folder/services/course_service.dart';
import 'package:rxdart/rxdart.dart';

enum ResultState { Loading, HasData, Error, NoData }

class DetailCourseCouponProvider with ChangeNotifier {
  final CourseService courseService;
  final String id;
  final String coupon;
  DetailCourseCouponProvider(
      {required this.courseService, required this.id, required this.coupon}) {
    getDetailCourseCoupon(id, coupon);
  }

  DataDetailCourseModel? _detailCourse;

  DataDetailCourseModel? get result => _detailCourse;

  ResultState? _state;

  String _message = '';

  ResultState? get state => _state;

  String get message => _message;

  set detailCourse(DataDetailCourseModel? detail) {
    _detailCourse = detail;
    notifyListeners();
  }

  Future<dynamic> getDetailCourseCoupon(_id, String coupon) async {
    try {
      _state = ResultState.Loading;
      notifyListeners();
      DataDetailCourseModel? detail =
          await courseService.getDetailCourseCoupon(_id, coupon);

      _state = ResultState.HasData;
      print("Berhasil detail kupon di provider ${_detailCourse}");
      notifyListeners();
      return _detailCourse = detail;
    } catch (e) {
      print("Gagal detail kupon di provider ${e}");
      _state = ResultState.Error;
      notifyListeners();
      return _message = 'Error --> $e';
    }
  }
}

class DetailCouponProvider with ChangeNotifier {
  DataDetailCourseModel? _detailCourse;

  DataDetailCourseModel? get result => _detailCourse;

  StreamController<DataDetailCourseModel> streamController = BehaviorSubject();

  String _message = '';

  String get message => _message;

  set detailCourse(DataDetailCourseModel? detail) {
    _detailCourse = detail;
    notifyListeners();
  }

  Stream<DataDetailCourseModel> get detailStream {
    return streamController.stream;
  }

  Future<void> getDetail(id, coupon) async {
    try {
      print("Berhasil detail kupon di provider");
      DataDetailCourseModel? detail =
          await CourseService().getDetailCourseCoupon(id, coupon);
      streamController.add(detail);
    } catch (e) {
      print("Gagal detail kupon di provider 2 ${e}");
    }
  }
}
