import 'package:flutter/material.dart';
import 'package:initial_folder/models/qna_model.dart';
import 'package:initial_folder/services/qna_service.dart';

enum ResultState { uninitilized, loading, failed, success }

class PostingQnaProvider with ChangeNotifier {
  QnaDataModel? _qnaDataModel;
  QnaDataModel? get wishlistPostModel => _qnaDataModel;

  setwishlistPostModel(QnaDataModel? qnaDataModel) {
    _qnaDataModel = qnaDataModel;
    notifyListeners();
  }

  ResultState _state = ResultState.uninitilized;

  ResultState get state => _state;

  //Posting QNA
  Future<bool> postingQna(
    String title,
    String quest,
    String idCourse,
    String idLesson,
  ) async {
    try {
      _state = ResultState.loading;
      notifyListeners();

      bool response =
          await QnaService().postingQna(title, quest, idCourse, idLesson);
      if (response) {
        _state = ResultState.success;
        notifyListeners();
        return true;
      } else {
        _state = ResultState.failed;
        notifyListeners();
        return false;
      }
    } catch (e) {
      _state = ResultState.failed;
      notifyListeners();
      return false;
    }
  }

  //Update QNA
  Future<bool> editQna(String idCourse, String quest, int idQna, String title, String idLesson) async {
    try {
      _state = ResultState.loading;
      notifyListeners();

      bool response = await QnaService().updateQna(idCourse, quest, idQna, title, idLesson);
      if (response) {
        _state = ResultState.success;
        notifyListeners();
        return true;
      } else {
        _state = ResultState.failed;
        notifyListeners();
        return false;
      }
    } catch (e) {
      _state = ResultState.failed;
      notifyListeners();
      return false;
    }
  }

  //Delete QNA
  Future<bool> deleteQna(int idQna) async {
    try {
      _state = ResultState.loading;
      notifyListeners();

      bool response = await QnaService().deleteQna(idQna);
      if (response) {
        _state = ResultState.success;
        notifyListeners();
        return true;
      } else {
        _state = ResultState.failed;
        notifyListeners();
        return false;
      }
    } catch (e) {
      _state = ResultState.failed;
      notifyListeners();
      return false;
    }
  }
}
