import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:siro_rider/constant/box_name.dart';
import 'package:siro_rider/constant/links.dart';
import 'package:siro_rider/constant/style.dart';
import 'package:siro_rider/controller/functions/crud.dart';
import 'package:siro_rider/controller/home/map/ride_lifecycle_controller.dart';
import 'package:siro_rider/main.dart';
import 'package:siro_rider/views/home/map_page_passenger.dart';
import 'package:siro_rider/views/widgets/elevated_btn.dart';

import '../firebase/notification_service.dart';
import '../payment/payment_controller.dart';

// import '../home/captin/home_captain_controller.dart';

class RateController extends GetxController {
  double selectedRateItemId = -1;
  TextEditingController comment = TextEditingController();
  String? rideId, passengerId, driverId, driverName, price;
  late GlobalKey<FormState> formKey;
  @override
  void onInit() {
    formKey = GlobalKey<FormState>();
    passengerId = Get.arguments['passengerId'];
    rideId = Get.arguments['rideId'];
    driverId = Get.arguments['driverId'];
    driverName = Get.arguments['driverName'];
    price = Get.arguments['price'];
    box.write(BoxName.tipPercentage, '0');
    super.onInit();
  }

  void selectRateItem(double id) {
    selectedRateItemId = id;
    update();
  }

  addRateToDriver() async {
    if (selectedRateItemId < 1) {
      Get.defaultDialog(
          title: 'You Should choose rate figure'.tr,
          titleStyle: AppStyle.title,
          middleText: '',
          confirm: MyElevatedButton(title: 'Ok', onPressed: () => Get.back()));
    } else if (Get.find<PaymentController>().isWalletChecked == true) {
      double tip = 0;
      tip = double.parse(Get.find<RideLifecycleController>().totalPassenger.toString()) *
          (double.parse(box.read(BoxName.tipPercentage).toString()));

      if (tip > 0) {
        var res = await CRUD().post(link: AppLink.addTips, payload: {
          'passengerID': box.read(BoxName.passengerID).toString(),
          'driverID': Get.find<RideLifecycleController>().driverId.toString(),
          'rideID': Get.find<RideLifecycleController>().rideId.toString(),
          'tipAmount': tip.toString(),
          'country_code': box.read(BoxName.countryCode).toString(),
        });
        if (res != 'failure') {
          await NotificationService.sendNotification(
            category: 'You Have Tips',
            target: Get.find<RideLifecycleController>().driverToken.toString(),
            title: 'You Have Tips'.tr,
            body:
                '${'${tip.toString()}\$${' tips\nTotal is'.tr}'} ${tip + double.parse(Get.find<RideLifecycleController>().totalPassenger.toString())}',
            isTopic: false, // Important: this is a token
            tone: 'ding',
            driverList: [],
          );
        }
      }
    }
    await CRUD().post(
      link: "${AppLink.server}/ride/rate/addRateToDriver.php",
      payload: {
        'passenger_id': box.read(BoxName.passengerID).toString(),
        'driver_id': driverId.toString(),
        'ride_id': rideId.toString(),
        'rating': selectedRateItemId.toString(),
        'comment': comment.text,
      },
    );

    Get.find<RideLifecycleController>().restCounter();
    Get.offAll(const MapPagePassenger());
  }
}
