import 'package:SEFER/controller/home/captin/map_driver_controller.dart';
import 'package:SEFER/views/home/home_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/constant/links.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/controller/functions/crud.dart';
import 'package:SEFER/controller/home/map_passenger_controller.dart';
import 'package:SEFER/main.dart';
import 'package:SEFER/views/home/Captin/home_captain/home_captin.dart';
import 'package:SEFER/views/home/map_page_passenger.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';

import '../firebase/firbase_messge.dart';

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

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

    super.onInit();
  }

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

  void addRateToPassenger() async {
    // HomeCaptainController homeCaptainController =
    //     Get.find<HomeCaptainController>();
    // Get.put(MapDriverController());
    if (selectedRateItemId < 1) {
      Get.defaultDialog(
          title: 'You Should choose rate figure'.tr,
          titleStyle: AppStyle.title,
          middleText: '',
          confirm: MyElevatedButton(title: 'Ok', onPressed: () => Get.back()));
    } else {
      await CRUD().post(link: AppLink.addRateToPassenger, payload: {
        'passenger_id': passengerId,
        'driverID': box.read(BoxName.driverID).toString(),
        'rideId': rideId,
        'rating': selectedRateItemId.toString(),
        'comment': comment.text,
      });
      await CRUD().sendEmail(AppLink.sendEmailToPassengerForTripDetails, {
        'startLocation':
            Get.find<MapDriverController>().passengerLocation.toString(),
        'endLocation':
            Get.find<MapDriverController>().passengerDestination.toString(),
        'name': Get.find<MapDriverController>().passengerName.toString(),
        'timeOfTrip': Get.find<MapDriverController>().timeOfOrder.toString(),
        'fee': Get.find<MapDriverController>().totalPassenger.toString(),
        'duration': Get.find<MapDriverController>().duration.toString(),
        'phone': Get.find<MapDriverController>().passengerPhone.toString(),
        'email': Get.find<MapDriverController>().passengerEmail.toString(),
      });
      // homeCaptainController.isActive = true;
      // update();
      // homeCaptainController.getPaymentToday();
      Get.offAll(HomeCaptain());
    }
  }

  void 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 {
      var tip = (Get.find<MapPassengerController>().totalPassenger) *
          (double.parse(box.read(BoxName.tipPercentage.toString())));
      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 CRUD().post(link: AppLink.addPassengersWallet, payload: {
        'passenger_id': box.read(BoxName.passengerID).toString(),
        'balance': ((-1) * tip).toString()
      });

      await CRUD().post(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',
      });

      if (res != 'failure') {
        FirebaseMessagesController().sendNotificationToAnyWithoutData(
          'You Have Tips',
          '${'${tip.toString()}\$${' tips\nTotal is'.tr}'} ${tip + (Get.find<MapPassengerController>().totalPassenger)}',
          Get.find<MapPassengerController>().driverToken.toString(),
        );
      }

      await CRUD().post(link: AppLink.addRateToDriver, payload: {
        'passenger_id': box.read(BoxName.passengerID).toString(),
        'driver_id': driverId,
        'ride_id': rideId,
        'rating': selectedRateItemId.toString(),
        'comment': comment.text,
      }).then((value) {
        Get.find<MapPassengerController>().restCounter();
        Get.offAll(const MapPagePassenger());
      });
    }
  }
}
