import 'dart:convert';

import 'package:fl_chart/fl_chart.dart';
import 'package:get/get.dart';

import '../../constant/links.dart';
import '../../models/model/admin/monthly_ride.dart';
import '../functions/crud.dart';

class RideAdminController extends GetxController {
  bool isLoading = false;
  late List<MonthlyDataModel> rideData;
  late Map<String, dynamic> jsonResponse;
  List<dynamic> ridesDetails = [];
  var chartData;
  // late List<ChartDataS> chartDatasync;
  Future getRidesAdminDash() async {
    isLoading = true;
    update();
    var res = await CRUD().get(link: AppLink.getRidesPerMonth, payload: {});
    jsonResponse = jsonDecode(res);
    rideData = (jsonResponse['message'] as List)
        .map((item) => MonthlyDataModel.fromJson(item))
        .toList();

    chartData = rideData
        .map((data) => FlSpot(data.day.toDouble(), data.ridesCount.toDouble()))
        .toList();

    // chartDatasync = (jsonResponse['message'] as List)
    //     .map((item) => ChartDataS(
    //           item['year'],
    //           item['month'],
    //           item['day'],
    //           item['rides_count'],
    //         ))
    //     .toList();
    isLoading = false;
    update();
  }

  Future getRidesDetails() async {
    // isLoading = true;
    // update();
    var res = await CRUD().get(link: AppLink.getRidesDetails, payload: {});

    var d = jsonDecode(res);
    ridesDetails = d['message'];

    // isLoading = false;
    // update();
  }

  @override
  void onInit() async {
    List<Future> initializationTasks = [
      getRidesAdminDash(),
      getRidesDetails(),
    ];
    // cameras = await availableCameras();
    await Future.wait(initializationTasks);
    super.onInit();
  }
}

// class ChartDataS {
//   ChartDataS(this.year, this.month, this.day, this.ridesCount);

//   final int year;
//   final int month;
//   final int day;
//   final int ridesCount;
// }
