import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:grocery_app/src/core/network_services/service_locator.dart';
import 'package:grocery_app/src/data/myOrder.dart';
import 'package:grocery_app/src/logic/repo/order_repo.dart';
import 'package:grocery_app/utils/extensions/extensions.dart';

class OrderProvider extends ChangeNotifier {
  final _orderRepo = getIt<OrderRepo>();

  List<Datum> orderList = [];
  bool isloading = true;

  Future<void> getMyOrder(BuildContext context) async {
    var data = {};
    try {
      var result = await _orderRepo.myOrder(data);

      return result.fold(
        (error) {
          isloading = false;

          notifyListeners();
        },
        (response) {
          orderList = response.data!;

          isloading = false;
          notifyListeners();
        },
      );
    } catch (e) 
    {
      print("sfddsfdfff  $e");

      isloading = false;
      notifyListeners();
    }
  }

  bool isSendOtp = false;

  Future<void> getAssignedOtp(BuildContext context, String assignmentId) async {
    context.showLoader(show: true);
    notifyListeners();
    var data = {"orderItemId": assignmentId};

    print("ldflkgfkgjdh   ${data}");

    try {
      var result = await _orderRepo.getAssignedOtp(data);

      return result.fold(
        (error) {
          context.showLoader(show: false);
        },
        (response) {
          _showOtpPopup(context, response.code!); //

          context.showLoader(show: false);
        },
      );
    } catch (e) {
      context.showLoader(show: false);
      Fluttertoast.showToast(
        msg: "Something went wrong",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 14.0,
      );
    }
  }

  void _showOtpPopup(BuildContext context, String otp) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Center(child: Text("Your OTP")), // Center the title
        content: Column(
          mainAxisSize: MainAxisSize.min, // Prevent excessive height
          children: [
            Center(
              child: Text(
                otp,
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                  color: Colors.blue,
                ),
              ),
            ),
          ],
        ),
        actions: [
          Center(
            child: TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text("OK"),
            ),
          ),
        ],
      ),
    );
  }

  Future<bool> updateStatus(
      BuildContext context, String orderStatus, String orderItemId) async {
    context.showLoader(show: true);

    var data = {"status": orderStatus, "comment": "string"};
    try {
      var result = await _orderRepo.updateStatus(data, orderItemId);

      return result.fold(
        (error) {
          context.showLoader(show: false);
          return false;
        },
        (response) {
          context.showLoader(show: false);
          getMyOrder(context);
          Fluttertoast.showToast(
            msg: "Order cancelled successfully!",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
            backgroundColor: Colors.green,
            textColor: Colors.white,
            fontSize: 14.0,
          );
          Navigator.pop(context);

          notifyListeners();

          return true;
        },
      );
    } catch (e) {
      context.showLoader(show: false);

      print("lkjdgkfjhfdkg");

      Fluttertoast.showToast(
        msg: "Status already updated",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 14.0,
      );

      return false;
    }
  }
}
