import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:hallo_doctor_client/app/utils/environment.dart';

import '../../../routes/app_pages.dart';
import '../controllers/midtrans_payment_controller.dart';
import 'package:midtrans_snap/midtrans_snap.dart';
import 'package:midtrans_snap/models.dart';

class MidtransPaymentView extends GetView<MidtransPaymentController> {
  const MidtransPaymentView({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GetBuilder<MidtransPaymentController>(
        builder: (_) {
          if (_.token == null) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          return MidtransSnap(
            mode: MidtransEnvironment.sandbox,
            token: _.token!,
            midtransClientKey: Environment.midtransClientKey,
            onPageFinished: (url) {},
            onPageStarted: (url) {
              print(url);
            },
            onResponse: (result) {
              print(result.toJson());
              if (result.statusCode == 200) {
                if (result.transactionStatus == 'settlement') {
                  Get.offNamed(Routes.PAYMENT_SUCCESS,
                      arguments: controller.selectedTimeSlot);
                }
                Get.offNamed(Routes.PAYMENT_SUCCESS,
                    arguments: controller.selectedTimeSlot);
              } else {
                Get.back();
              }
            },
          );
        },
      ),
    );
  }
}
