package com.brisco.meatwholesaler;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class OrderConfirmationActivity extends AppCompatActivity {

    private TextView textViewOrderNumber;
    private TextView textViewCustomerName;
    private TextView textViewDeliveryAddress;
    private TextView textViewOrderTotal;
    private TextView textViewPaymentMethod;
    private TextView textViewEstimatedDelivery;
    private Button buttonContinueShopping;
    private Button buttonTrackOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_confirmation);

        // Get order data from intent
        Intent intent = getIntent();
        double orderTotal = intent.getDoubleExtra("ORDER_TOTAL", 0.0);
        String paymentMethod = intent.getStringExtra("PAYMENT_METHOD");
        String customerName = intent.getStringExtra("CUSTOMER_NAME");
        String deliveryAddress = intent.getStringExtra("DELIVERY_ADDRESS");

        // Setup toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            getSupportActionBar().setTitle("Order Confirmed");
        }

        // Initialize views
        initializeViews();

        // Load order data
        loadOrderData(orderTotal, paymentMethod, customerName, deliveryAddress);

        // Setup click listeners
        setupClickListeners();
    }

    private void initializeViews() {
        textViewOrderNumber = findViewById(R.id.text_order_number);
        textViewCustomerName = findViewById(R.id.text_customer_name);
        textViewDeliveryAddress = findViewById(R.id.text_delivery_address);
        textViewOrderTotal = findViewById(R.id.text_order_total);
        textViewPaymentMethod = findViewById(R.id.text_payment_method);
        textViewEstimatedDelivery = findViewById(R.id.text_estimated_delivery);
        buttonContinueShopping = findViewById(R.id.btn_continue_shopping);
        buttonTrackOrder = findViewById(R.id.btn_track_order);
    }

    private void loadOrderData(double orderTotal, String paymentMethod, String customerName, String deliveryAddress) {
        // Generate random order number
        String orderNumber = "BR" + System.currentTimeMillis() % 100000;
        textViewOrderNumber.setText(orderNumber);

        // Set customer information
        textViewCustomerName.setText(customerName != null ? customerName : "Guest Customer");
        textViewDeliveryAddress.setText(deliveryAddress != null ? deliveryAddress : "Address not provided");
        textViewOrderTotal.setText(String.format("R %.2f", orderTotal));
        textViewPaymentMethod.setText(paymentMethod != null ? paymentMethod : "Card on Delivery");

        // Set estimated delivery (2-3 business days from now)
        textViewEstimatedDelivery.setText("2-3 business days");
    }

    private void setupClickListeners() {
        buttonContinueShopping.setOnClickListener(v -> {
            Intent intent = new Intent(OrderConfirmationActivity.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
        });

        buttonTrackOrder.setOnClickListener(v -> {
            Intent intent = new Intent(OrderConfirmationActivity.this, OrdersActivity.class);
            startActivity(intent);
        });
    }

    @Override
    public void onBackPressed() {
        // Prevent going back to checkout
        Intent intent = new Intent(OrderConfirmationActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }
}
