package com.example.bdkipoc.transaction.managers;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;

import com.example.bdkipoc.R;

/**
 * ModalManager - Handles modal UI operations
 */
public class ModalManager {
    private static final String TAG = "ModalManager";
    
    private FrameLayout modalOverlay;
    private TextView modalText;
    private ImageView modalIcon;
    private Animation fadeIn;
    private Animation fadeOut;
    private boolean isModalShowing = false;
    
    public ModalManager(FrameLayout modalOverlay, TextView modalText, ImageView modalIcon) {
        this.modalOverlay = modalOverlay;
        this.modalText = modalText;
        this.modalIcon = modalIcon;
        initAnimations();
    }
    
    private void initAnimations() {
        fadeIn = AnimationUtils.loadAnimation(modalOverlay.getContext(), android.R.anim.fade_in);
        fadeOut = AnimationUtils.loadAnimation(modalOverlay.getContext(), android.R.anim.fade_out);
        
        fadeIn.setDuration(300);
        fadeOut.setDuration(300);
    }
    
    public void showScanCardModal() {
        if (isModalShowing) return;
        
        modalOverlay.post(() -> {
            modalText.setText("Silakan Tempelkan / Gesekkan / Masukkan Kartu ke Perangkat");
            modalIcon.setImageResource(R.drawable.ic_card_insert);
            
            modalOverlay.setVisibility(View.VISIBLE);
            modalOverlay.startAnimation(fadeIn);
            
            isModalShowing = true;
            Log.d(TAG, "Modal scan card shown");
        });
    }
    
    public void showProcessingModal(String message) {
        if (!isModalShowing) {
            modalOverlay.post(() -> {
                modalText.setText(message);
                modalIcon.setImageResource(R.drawable.ic_card_insert);
                
                modalOverlay.setVisibility(View.VISIBLE);
                modalOverlay.startAnimation(fadeIn);
                
                isModalShowing = true;
                Log.d(TAG, "Modal processing shown: " + message);
            });
        } else {
            // Just update text if modal already showing
            modalOverlay.post(() -> {
                modalText.setText(message);
                Log.d(TAG, "Modal text updated: " + message);
            });
        }
    }
    
    public void hideModal() {
        if (!isModalShowing) return;
        
        modalOverlay.post(() -> {
            fadeOut.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {}

                @Override
                public void onAnimationEnd(Animation animation) {
                    modalOverlay.setVisibility(View.GONE);
                    isModalShowing = false;
                }

                @Override
                public void onAnimationRepeat(Animation animation) {}
            });
            
            modalOverlay.startAnimation(fadeOut);
            Log.d(TAG, "Modal hidden");
        });
    }
    
    public boolean isShowing() {
        return isModalShowing;
    }
    
    public void updateText(String text) {
        if (isModalShowing) {
            modalOverlay.post(() -> {
                modalText.setText(text);
                Log.d(TAG, "Modal text updated: " + text);
            });
        }
    }
    
    public void updateIcon(int iconResource) {
        if (isModalShowing) {
            modalOverlay.post(() -> {
                modalIcon.setImageResource(iconResource);
                Log.d(TAG, "Modal icon updated");
            });
        }
    }
}