import 'package:agrilink_vocpro/core/state/result_state.dart';
import 'package:agrilink_vocpro/data/model/latest_data_response.dart';
import 'package:agrilink_vocpro/domain/service/app_service.dart';
import 'package:flutter/foundation.dart';

class NitrogenProvider extends ChangeNotifier {
  NitrogenProvider() {
    getSoilNitrogenNpk1Data();
    getSoilNitrogenNpk2Data();
  }
  ResultState dataState = ResultState.initial;

  List<Npk> dataFetchedNpk1 = [];
  List<Npk> dataFetchedNpk2 = [];

  Future<void> getSoilNitrogenNpk1Data() async {
    dataState = ResultState.loading;
    notifyListeners();
    try {
      final result =
          await AppService().getGraphicDataNpk1(metric: 'soilNitrogen');
      if (result.data == null || result.data!.npk1!.isEmpty) {
        dataState = ResultState.noData;
      } else {
        dataFetchedNpk1 = result.data!.npk1 ?? [];
        dataState = ResultState.hasData;
      }
    } catch (e) {
      if (kDebugMode) {
        print('Get Grafik Soil Temp Error: $e');
      }
      dataState = ResultState.error;
    }
    notifyListeners();
  }

  Future<void> getSoilNitrogenNpk2Data() async {
    dataState = ResultState.loading;
    notifyListeners();
    try {
      final result =
          await AppService().getGraphicDataNpk2(metric: 'soilNitrogen');
      if (result.data == null || result.data!.npk2!.isEmpty) {
        dataState = ResultState.noData;
      } else {
        dataFetchedNpk2 = result.data!.npk2 ?? [];
        dataState = ResultState.hasData;
      }
    } catch (e) {
      if (kDebugMode) {
        print('Get Grafik Soil Temp Error: $e');
      }
      dataState = ResultState.error;
    }
    notifyListeners();
  }
}
