package com.dspread.pos.ui.pinpad;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.dspread.pos.common.base.BaseFragment;
import com.dspread.pos.TitleProviderListener;
import com.dspread.pos.posAPI.POS;
import com.dspread.pos.ui.payment.PaymentActivity;
import com.dspread.pos_android_app.BR;
import com.dspread.pos_android_app.R;
import com.dspread.pos_android_app.databinding.FragmentPinpadBinding;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class PinpadFragment extends BaseFragment<FragmentPinpadBinding, PinpadViewModel> implements TitleProviderListener {
    private static final String TAG = "PinpadFragment";
    private boolean canShow = true;
    private CountDownTimer showTimer;
    private String amountToPass;

    @Override
    public int initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return R.layout.fragment_pinpad;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        initTimer();

        // Get amount from arguments if passed
        if (getArguments() != null) {
            amountToPass = getArguments().getString("amount", "100");
            Log.d(TAG, "Received amount: " + amountToPass);
            viewModel.setInitialAmount(amountToPass);
        } else {
            amountToPass = "1000"; // default value
            Log.d(TAG, "No arguments, using default amount: " + amountToPass);
        }

        // REMOVED: Auto-navigation timer - we'll use signal instead
    }

    private void initTimer() {
        showTimer = new CountDownTimer(500, 500) {
            @Override
            public void onTick(long millisUntilFinished) {}

            @Override
            public void onFinish() {
                canShow = true;
            }
        };
    }

    private void navigateToPayment(String amount) {
        if (!canShow) {
            Log.d(TAG, "Navigation blocked - canShow is false");
            return;
        }

        if (getActivity() == null || getActivity().isFinishing()) {
            Log.d(TAG, "Navigation blocked - activity is null or finishing");
            return;
        }

        if (!POS.getInstance().isPOSReady()) {
            ToastUtils.showShort("Please connect your device first");
            Log.d(TAG, "POS not ready, cannot navigate to payment");
            return;
        }

        canShow = false;
        showTimer.start();

        Log.d(TAG, "Navigating to PaymentActivity with amount: " + amount);

        Intent intent = new Intent(getActivity(), PaymentActivity.class);
        intent.putExtra("amount", amount);
        startActivity(intent);

        // Optional: add animation
        getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    public void initViewObservable() {
        // Observe the payment start event (signal-based)
        viewModel.paymentStartEvent.observe(this, amount -> {
            Log.d(TAG, "Payment start signal received with amount: " + amount);
            navigateToPayment(amount);
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy called");

        if (showTimer != null) {
            showTimer.cancel();
        }
    }

    @Override
    public String getTitle() {
        return "Pin Pad";
    }
}