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

class HomeProvider extends ChangeNotifier {
  final DateTime currentDate = DateTime.now();

  num _dhtHumidity = 0;
  num get dhtHumidity => _dhtHumidity;

  num _dhtTemperature = 0;
  num get dhtTemperature => _dhtTemperature;

  num _dhtLuminosity = 0;
  num get dhtLuminosity => _dhtLuminosity;

  num _npk1SoilMoisture = 0;
  num get npk1SoilMoisture => _npk1SoilMoisture;

  num _npk1Temperature = 0;
  num get npk1Temperature => _npk1Temperature;

  num _npk1SoilPh = 0;
  num get npk1SoilPh => _npk1SoilPh;

  num _npk1SoilEc = 0;
  num get npk1SoilEc => _npk1SoilEc;

  num _npk1SoilNitrogen = 0;
  num get npk1SoilNitrogen => _npk1SoilNitrogen;

  num _npk1SoilPhosphorus = 0;
  num get npk1SoilPhosphorus => _npk1SoilPhosphorus;

  num _npk1SoilPotassium = 0;
  num get npk1SoilPotassium => _npk1SoilPotassium;

  num _npk2SoilMoisture = 0;
  num get npk2SoilMoisture => _npk2SoilMoisture;

  num _npk2Temperature = 0;
  num get npk2Temperature => _npk2Temperature;

  num _npk2SoilPh = 0;
  num get npk2SoilPh => _npk2SoilPh;

  num _npk2SoilEc = 0;
  num get npk2SoilEc => _npk2SoilEc;

  num _npk2SoilNitrogen = 0;
  num get npk2SoilNitrogen => _npk2SoilNitrogen;

  num _npk2SoilPhosphorus = 0;
  num get npk2SoilPhosphorus => _npk2SoilPhosphorus;

  num _npk2SoilPotassium = 0;
  num get npk2SoilPotassium => _npk2SoilPotassium;

  HomeProvider() {
    getLatestData();
  }

  ResultState dataState = ResultState.initial;

  Future<void> getLatestData() async {
    dataState = ResultState.loading;
    notifyListeners();
    try {
      final result = await AppService().getLatestData();
      if (result.data == null) {
        dataState = ResultState.noData;
        notifyListeners();
      } else {
        final data = result.data!;
        _dhtHumidity = data.dht?[0].vicihumidity ?? 0;
        _dhtTemperature = data.dht?[0].vicitemperature ?? 0;
        _dhtLuminosity = data.dht?[0].viciluminosity ?? 0;

        _npk1SoilMoisture = data.npk1?[0].soilhumidity ?? 0;
        _npk1Temperature = data.npk1?[0].soiltemperature ?? 0;
        _npk1SoilPh = data.npk1?[0].soilph ?? 0;
        _npk1SoilEc = data.npk1?[0].soilconductivity ?? 0;
        _npk1SoilNitrogen = data.npk1?[0].soilnitrogen ?? 0;
        _npk1SoilPhosphorus = data.npk1?[0].soilphosphorus ?? 0;
        _npk1SoilPotassium = data.npk1?[0].soilpotassium ?? 0;

        _npk2SoilMoisture = data.npk2?[0].soilhumidity ?? 0;
        _npk2Temperature = data.npk2?[0].soiltemperature ?? 0;
        _npk2SoilPh = data.npk2?[0].soilph ?? 0;
        _npk2SoilEc = data.npk2?[0].soilconductivity ?? 0;
        _npk2SoilNitrogen = data.npk2?[0].soilnitrogen ?? 0;
        _npk2SoilPhosphorus = data.npk2?[0].soilphosphorus ?? 0;
        _npk2SoilPotassium = data.npk2?[0].soilpotassium ?? 0;
        dataState = ResultState.hasData;
        notifyListeners();
      }
    } catch (e) {
      if (kDebugMode) {
        print('Get Latest Error: $e');
      }
      dataState = ResultState.hasData;
      notifyListeners();
    }
  }

  @override
  void dispose() {
    dataState = ResultState.initial;
    super.dispose();
  }
}
