import 'dart:async';
import 'dart:convert';

import 'package:Intaleq/constant/links.dart';
import 'package:Intaleq/controller/functions/crud.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class TripMonitorController extends GetxController {
  bool isLoading = false;
  Map tripData = {};
  late String rideId;
  late String driverId;
  GoogleMapController? mapController;
  List myListString = [];
  late Timer timer;
  late LatLng parentLocation;
  BitmapDescriptor carIcon = BitmapDescriptor.defaultMarker;
  BitmapDescriptor motoIcon = BitmapDescriptor.defaultMarker;
  BitmapDescriptor ladyIcon = BitmapDescriptor.defaultMarker;
  double rotation = 0;
  double speed = 0;

  getLocationParent() async {
    var res = await CRUD().get(
        link: AppLink.getLocationParents, payload: {"driver_id": driverId});
    if (res != 'failure') {
      tripData = jsonDecode(res);
      parentLocation = LatLng(
          double.parse(tripData['message'][0]['latitude'].toString()),
          double.parse(tripData['message'][0]['longitude'].toString()));
      rotation = double.parse(tripData['message'][0]['heading'].toString());
      speed = double.parse(tripData['message'][0]['speed'].toString());
      update();
    }
  }

  void onMapCreated(GoogleMapController controller) async {
    mapController = controller;
    controller.getVisibleRegion();
    controller.animateCamera(
      CameraUpdate.newLatLng(parentLocation),
    );
    update();
    // Set up a timer or interval to trigger the marker update every 3 seconds.
    timer = Timer.periodic(const Duration(seconds: 10), (_) async {
      await getLocationParent();
      mapController?.animateCamera(CameraUpdate.newLatLng(parentLocation));
      update();
    });
  }

  // init() async {
  //   final arguments = Get.arguments;
  //   driverId = arguments['driverId'];
  //   rideId = arguments['rideId'];
  //   await getLocationParent();
  // }

  Future<void> init({String? rideId, String? driverId}) async {
    this.driverId = driverId!;
    this.rideId = rideId!;
    await getLocationParent();
    update();
  }

  void addCustomCarIcon() {
    ImageConfiguration config = ImageConfiguration(
        size: const Size(30, 30), devicePixelRatio: Get.pixelRatio);
    BitmapDescriptor.fromAssetImage(config, 'assets/images/car.png',
            mipmaps: false)
        .then((value) {
      carIcon = value;
      update();
    });
    void addCustomMotoIcon() {
      ImageConfiguration config = ImageConfiguration(
          size: const Size(30, 30), devicePixelRatio: Get.pixelRatio);
      BitmapDescriptor.fromAssetImage(config, 'assets/images/moto1.png',
              mipmaps: false)
          .then((value) {
        motoIcon = value;
        update();
      });
    }

    void addCustomLadyIcon() {
      ImageConfiguration config = ImageConfiguration(
          size: const Size(30, 30), devicePixelRatio: Get.pixelRatio);
      BitmapDescriptor.fromAssetImage(config, 'assets/images/lady1.png',
              mipmaps: false)
          .then((value) {
        ladyIcon = value;
        update();
      });
    }
  }

  @override
  void onInit() {
    addCustomCarIcon();
    super.onInit();
  }

  @override
  void onClose() {
    timer.cancel();
    mapController?.dispose();

    super.onClose();
  }
}
