import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:ride/constant/links.dart';
import 'package:ride/constant/style.dart';
import 'package:ride/controller/functions/crud.dart';
import 'package:ride/controller/functions/toast.dart';
import 'package:ride/controller/payment/payment_controller.dart';
import 'package:ride/views/widgets/my_scafold.dart';
import 'package:flutter_paypal/flutter_paypal.dart';
import '../../../constant/box_name.dart';
import '../../../constant/colors.dart';
import '../../../constant/info.dart';
import '../../../controller/functions/secure_storage.dart';
import '../../../controller/home/payment/credit_card_controller.dart';
import '../../../main.dart';
import '../../widgets/elevated_btn.dart';
import '../map_widget.dart/payment_method.page.dart';

class PassengerWallet extends StatelessWidget {
  const PassengerWallet({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(PaymentController());
    Get.put(CreditCardController());
    return MyScafolld(
      title: 'My Wallet',
      isleading: true,
      body: [
        GetBuilder<PaymentController>(
          builder: (controller) => Column(
            children: [
              Column(
                children: [
                  Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Container(
                            decoration: const BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(12)),
                                color: AppColor.secondaryColor,
                                boxShadow: [
                                  BoxShadow(
                                      color: AppColor.accentColor,
                                      offset: Offset(-1, -1),
                                      blurRadius: 0,
                                      spreadRadius: 0,
                                      blurStyle: BlurStyle.normal),
                                  BoxShadow(
                                      color: AppColor.accentColor,
                                      offset: Offset(3, 3),
                                      blurRadius: 1,
                                      spreadRadius: 0,
                                      blurStyle: BlurStyle.normal)
                                ]),
                            child: Padding(
                              padding: const EdgeInsets.all(10),
                              child:
                                  box.read(BoxName.passengerWalletTotal) == null
                                      ? Text(
                                          'You Dont Have Any amount in'.tr +
                                              ' ${AppInformation.appName}' +
                                              'Wallet!'.tr,
                                          style: AppStyle.title,
                                        )
                                      : Text(
                                          'You Have'.tr +
                                              ' ${box.read(BoxName.passengerWalletTotal).toString()} JD in ${AppInformation.appName} Wallet',
                                          style: AppStyle.title,
                                        ),
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(
                        height: 30,
                      ),
                      const CreditCardWidget(),
                      const SizedBox(
                        height: 10,
                      ),
                      GetBuilder<CreditCardController>(
                          builder: (reditCardControllerc) => Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  MyElevatedButton(
                                    title: 'Save Credit Card'.tr,
                                    onPressed: () async {
                                      if (controller.formKey.currentState!
                                          .validate()) {
                                        SecureStorage().saveData(
                                            BoxName.cardNumber,
                                            reditCardControllerc
                                                .cardNumberController.text);
                                        SecureStorage().saveData(
                                            BoxName.cardHolderName,
                                            reditCardControllerc
                                                .cardHolderNameController.text);
                                        SecureStorage().saveData(
                                            BoxName.cvvCode,
                                            reditCardControllerc
                                                .cvvCodeController.text);
                                        SecureStorage().saveData(
                                            BoxName.expiryDate,
                                            reditCardControllerc
                                                .expiryDateController.text);
                                      }
                                    },
                                  ),
                                ],
                              )),
                      // MyElevatedButton(
                      //   title: 'Charge your Wallet',
                      //   onPressed: () {
                      //     Get.defaultDialog(
                      //         title: 'Choose amount you will Charge?'.tr,
                      //         content: Column(
                      //           crossAxisAlignment: CrossAxisAlignment.center,
                      //           children: [
                      //             Row(
                      //               children: [
                      //                 Radio(
                      //                   value: 10,
                      //                   groupValue: controller.selectedAmount,
                      //                   onChanged: (value) {
                      //                     controller.selectedAmount =
                      //                         value as String?;
                      //                   },
                      //                 ),
                      //                 Text('10 and get 4% discount'.tr),
                      //               ],
                      //             ),
                      //             Row(
                      //               children: [
                      //                 Radio(
                      //                   value: 20,
                      //                   groupValue: controller.selectedAmount,
                      //                   onChanged: (value) {
                      //                     controller.selectedAmount =
                      //                         value as String?;
                      //                   },
                      //                 ),
                      //                 Text('20 and get 6% discount'.tr),
                      //               ],
                      //             ),
                      //             Row(
                      //               children: [
                      //                 Radio(
                      //                   value: 40,
                      //                   groupValue: controller.selectedAmount,
                      //                   onChanged: (value) {
                      //                     controller.selectedAmount =
                      //                         value as String?;
                      //                   },
                      //                 ),
                      //                 Text('40 and get 8% discount'.tr),
                      //               ],
                      //             ),
                      //             Row(
                      //               children: [
                      //                 Radio(
                      //                   value: 100,
                      //                   groupValue: controller.selectedAmount,
                      //                   onChanged: (value) {
                      //                     controller.selectedAmount =
                      //                         value as String?;
                      //                   },
                      //                 ),
                      //                 Text('100 and get 11% discount'.tr),
                      //               ],
                      //             ),
                      //           ],
                      //         ));
                      //   },
                      // )
                    ],
                  ),
                ],
              ),
            ],
          ),
        ),
        GetBuilder<PaymentController>(
            builder: (controller) => controller.isPromoSheetDialogue
                ? Container(
                    color: AppColor.accentColor.withOpacity(.91),
                  )
                : const SizedBox()),
        GetBuilder<PaymentController>(
            builder: (controller) => Positioned(
                  bottom: Get.height * .3,
                  left: Get.width * .3,
                  right: Get.width * .3,
                  child: MyElevatedButton(
                    title: 'Show Promos'.tr,
                    onPressed: () {
                      controller.changePromoSheetDialogue();
                    },
                  ),
                )),
        GetBuilder<PaymentController>(
          builder: (controller) => Positioned(
              top: Get.height * .2,
              right: Get.width * .15,
              left: Get.width * .15,
              bottom: Get.height * .2,
              child: controller.isPromoSheetDialogue
                  ? Container(
                      decoration: const BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(12)),
                          color: AppColor.secondaryColor,
                          boxShadow: [
                            BoxShadow(
                                color: AppColor.accentColor,
                                offset: Offset(-1, -1),
                                blurRadius: 0,
                                spreadRadius: 0,
                                blurStyle: BlurStyle.normal),
                            BoxShadow(
                                color: AppColor.accentColor,
                                offset: Offset(3, 3),
                                blurRadius: 1,
                                spreadRadius: 0,
                                blurStyle: BlurStyle.normal)
                          ]),
                      child: Padding(
                        padding: const EdgeInsets.all(6),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            GestureDetector(
                              onTap: () {
                                controller.updateSelectedAmount(10);
                              },
                              child: Row(
                                children: [
                                  Radio(
                                    value: 10,
                                    groupValue: controller.selectedAmount,
                                    onChanged: (value) {
                                      controller
                                          .updateSelectedAmount(value as int);
                                    },
                                  ),
                                  Text(
                                    '10 and get 4% discount'.tr,
                                    style: AppStyle.title,
                                  ),
                                ],
                              ),
                            ),
                            GestureDetector(
                                onTap: () {
                                  controller.updateSelectedAmount(20);
                                },
                                child: Row(
                                  children: [
                                    Radio(
                                      value: 20,
                                      groupValue: controller.selectedAmount,
                                      onChanged: (value) {
                                        controller
                                            .updateSelectedAmount(value as int);
                                      },
                                    ),
                                    Text(
                                      '20 and get 6% discount'.tr,
                                      style: AppStyle.title,
                                    ),
                                  ],
                                )),
                            GestureDetector(
                                onTap: () {
                                  controller.updateSelectedAmount(40);
                                },
                                child: Row(
                                  children: [
                                    Radio(
                                      value: 40,
                                      groupValue: controller.selectedAmount,
                                      onChanged: (value) {
                                        controller
                                            .updateSelectedAmount(value as int);
                                      },
                                    ),
                                    Text(
                                      '40 and get 8% discount'.tr,
                                      style: AppStyle.title,
                                    ),
                                  ],
                                )),
                            GestureDetector(
                                onTap: () {
                                  controller.updateSelectedAmount(100);
                                },
                                child: Row(
                                  children: [
                                    Radio(
                                      value: 100,
                                      groupValue: controller.selectedAmount,
                                      onChanged: (value) {
                                        controller
                                            .updateSelectedAmount(value as int);
                                      },
                                    ),
                                    Text(
                                      '100 and get 11% discount'.tr,
                                      style: AppStyle.title,
                                    ),
                                  ],
                                )),
                            const Spacer(),
                            MyElevatedButton(
                              title: 'Pay with Your PayPal'.tr,
                              onPressed: () {
                                if (controller.selectedAmount != 0) {
                                  print(controller.selectedAmount);
                                  controller.changePromoSheetDialogue();
                                  Navigator.of(context).push(
                                    MaterialPageRoute(
                                      builder: (BuildContext context) =>
                                          UsePaypal(
                                              sandboxMode: true,
                                              clientId:
                                                  "AW1TdvpSGbIM5iP4HJNI5TyTmwpY9Gv9dYw8_8yW5lYIbCqf326vrkrp0ce9TAqjEGMHiV3OqJM_aRT0",
                                              secretKey:
                                                  "EHHtTDjnmTZATYBPiGzZC_AZUfMpMAzj2VZUeqlFUrRJA_C0pQNCxDccB5qoRQSEdcOnnKQhycuOWdP9",
                                              returnURL:
                                                  "https://samplesite.com/return",
                                              cancelURL:
                                                  "https://samplesite.com/cancel",
                                              transactions: [
                                                {
                                                  "amount": {
                                                    //sb-opsju26682403@personal.example.com
                                                    "total":
                                                        '${controller.selectedAmount}',
                                                    "currency": "USD",
                                                    "details": {
                                                      "subtotal":
                                                          '${controller.selectedAmount}',
                                                      "shipping": '0',
                                                      "shipping_discount": 0
                                                    }
                                                  },
                                                  "description":
                                                      "The payment transaction description.",
                                                  "payment_options": const {
                                                    "allowed_payment_method":
                                                        "INSTANT_FUNDING_SOURCE"
                                                  },
                                                  "item_list": {
                                                    "items": [
                                                      {
                                                        "name":
                                                            "${AppInformation.appName} Wallet ",
                                                        "quantity": 1,
                                                        "price":
                                                            '${controller.selectedAmount}',
                                                        "currency": "USD"
                                                      }
                                                    ],

                                                    // shipping address is not required though
                                                    "shipping_address": const {
                                                      "recipient_name":
                                                          "${AppInformation.appName} Wallet",
                                                      "line1": "Shafa Badran",
                                                      "line2": "",
                                                      "city": "Amman",
                                                      "country_code": "JO",
                                                      "postal_code": "13112",
                                                      "phone": "+962798583052",
                                                      "state": "Amman"
                                                    },
                                                  }
                                                }
                                              ],
                                              note:
                                                  "Contact us for any questions on your order.",
                                              onSuccess: (Map params) async {
                                                print("onSuccess: $params");
                                                await CRUD().post(
                                                    link: AppLink
                                                        .addPassengersWallet,
                                                    payload: {
                                                      'passenger_id': box
                                                          .read(BoxName
                                                              .passengerID)
                                                          .toString(),
                                                      'balance': controller
                                                          .selectedAmount
                                                          .toString()
                                                    });
                                                controller
                                                    .changePromoSheetDialogue();
                                                await controller
                                                    .getPassengerWallet();
                                              },
                                              onError: (error) {
                                                print("onError: $error");
                                                Toast.show(
                                                    context,
                                                    ' $error'.tr,
                                                    AppColor.redColor);
                                              },
                                              onCancel: (params) {
                                                print('cancelled: $params');
                                                Toast.show(
                                                    context,
                                                    'Pyament Cancelled .'.tr,
                                                    AppColor.yellowColor);
                                              }),
                                    ),
                                  );
                                } else {
                                  Toast.show(
                                      context,
                                      'You will choose one of above !'.tr,
                                      AppColor.redColor);
                                }
                                // controller.changePromoSheetDialogue();
                                // Get.to(() {
                                //   UsePaypal(
                                //       sandboxMode: true,
                                //       clientId:
                                //           "AXE7bR3WzFEfAInUA2PBDOsW4zolx11Qr3jVOOjjJ7KGDLY3tnMIhZgyFT5Qkvj1NKWPaE8VZbG4UrZs",
                                //       secretKey:
                                //           "EJPW01FKqOm-SKKP16LQ_2cr3b124aZgmE0vi3JfbkWgLffv2xIjT4iCIfQv8RYn3W8h4DzxgZu_UxZC",
                                //       returnURL:
                                //           "https://samplesite.com/return",
                                //       cancelURL:
                                //           "https://samplesite.com/cancel",
                                //       transactions: const [
                                //         {
                                //           "amount": {
                                //             "total": '10.12',
                                //             "currency": "USD",
                                //             "details": {
                                //               "subtotal": '10.12',
                                //               "shipping": '0',
                                //               "shipping_discount": 0
                                //             }
                                //           },
                                //           "description":
                                //               "The payment transaction description.",
                                //           // "payment_options": {
                                //           //   "allowed_payment_method":
                                //           //       "INSTANT_FUNDING_SOURCE"
                                //           // },
                                //           "item_list": {
                                //             "items": [
                                //               {
                                //                 "name": "A demo product",
                                //                 "quantity": 1,
                                //                 "price": '10.12',
                                //                 "currency": "USD"
                                //               }
                                //             ],

                                //             // shipping address is not required though
                                //             "shipping_address": {
                                //               "recipient_name": "Jane Foster",
                                //               "line1": "Travis County",
                                //               "line2": "",
                                //               "city": "Austin",
                                //               "country_code": "US",
                                //               "postal_code": "73301",
                                //               "phone": "+00000000",
                                //               "state": "Texas"
                                //             },
                                //           }
                                //         }
                                //       ],
                                //       note:
                                //           "Contact us for any questions on your order.",
                                //       onSuccess: (Map params) async {
                                //         print("onSuccess: $params");
                                //       },
                                //       onError: (error) {
                                //         print("onError: $error");
                                //       },
                                //       onCancel: (params) {
                                //         print('cancelled: $params');
                                //       });
                                // });
                              },
                            ),
                            MyElevatedButton(
                              title: 'Cancel'.tr,
                              kolor: AppColor.redColor,
                              onPressed: () {
                                controller.changePromoSheetDialogue();
                              },
                            ),
                          ],
                        ),
                      ))
                  : const SizedBox()),
        )
      ],
    );
  }
}
