import 'package:cheminova/models/Daily_Task_Response.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

import '../services/api_client.dart';
import '../services/api_urls.dart';

class DailyTaskProvider extends ChangeNotifier {
  final _apiClient = ApiClient();
  List<Tasks> newTasksList = [];
  List<Tasks> pendingTasksList = [];
  List<Tasks> completedTasksList = [];

  bool _isLoading = false;

  bool get isLoading => _isLoading;

  void setLoading(bool loading) {
    _isLoading = loading;
    notifyListeners();
  }

  Future<void> getTask(
      {required String type, bool isAddPending = false}) async {
    setLoading(true);
    try {
      Response response = await _apiClient.get(ApiUrls.dailyTaskUrl + type);
      setLoading(false);
      if (response.statusCode == 200) {
        final data = DailyTasksResponse.fromJson(response.data);

        if (type == 'New') {
          newTasksList.clear();
          newTasksList.addAll(data.tasks ?? []);
        } else if (type == 'Pending') {
          if (isAddPending) {
            newTasksList.addAll(data.tasks ?? []);
          } else {
            pendingTasksList = data.tasks ?? [];
          }
        } else {
          completedTasksList = data.tasks ?? [];
        }
        notifyListeners();
      }
    } catch (e) {
      setLoading(false);
      if (kDebugMode) {
        print("Error occurred while fetching notifications: $e");
      }
    }
  }
}
