import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:Intaleq/constant/box_name.dart';
import 'package:Intaleq/constant/links.dart';
import 'package:Intaleq/constant/style.dart';
import 'package:Intaleq/controller/functions/crud.dart';
import 'package:Intaleq/controller/home/map_passenger_controller.dart';
import 'package:Intaleq/main.dart';
import 'package:Intaleq/views/home/map_page_passenger.dart';
import 'package:Intaleq/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 = (Get.find<MapPassengerController>().totalPassenger) *
          (double.parse(box.read(BoxName.tipPercentage).toString()));

      if (tip > 0) {
        var res = await CRUD().post(link: AppLink.addTips, payload: {
          'passengerID': box.read(BoxName.passengerID),
          'driverID': Get.find<MapPassengerController>().driverId.toString(),
          'rideID': Get.find<MapPassengerController>().rideId.toString(),
          'tipAmount': tip.toString(),
        });
        await Get.find<PaymentController>()
            .addPassengersWallet(((-1) * tip).toString());
        var token1 = await Get.find<PaymentController>().generateTokenDriver(
            box.read(BoxName.countryCode) == 'Egypt'
                ? tip.toStringAsFixed(0)
                : (tip * 100).toString());
        await CRUD().postWallet(link: AppLink.addDriversWalletPoints, payload: {
          'driverID': Get.find<MapPassengerController>().driverId.toString(),
          'paymentID': '${Get.find<MapPassengerController>().rideId}tip',
          'amount': box.read(BoxName.countryCode) == 'Egypt'
              ? tip.toStringAsFixed(0)
              : (tip * 100).toString(),
          'paymentMethod': 'visa-tip',
          'token': token1,
        });
        if (res != 'failure') {
          await NotificationService.sendNotification(
            category: 'You Have Tips',
            target: Get.find<MapPassengerController>().driverToken.toString(),
            title: 'You Have Tips'.tr,
            body:
                '${'${tip.toString()}\$${' tips\nTotal is'.tr}'} ${tip + (Get.find<MapPassengerController>().totalPassenger)}',
            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<MapPassengerController>().restCounter();
    Get.offAll(const MapPagePassenger());
  }
}
