package com.example.bdkipoc;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class HistoryDetailActivity extends AppCompatActivity {
    
    private RecyclerView recyclerView;
    private HistoryDetailAdapter adapter;
    private List<HistoryItem> detailList;
    private ImageView btnBack;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_detail);
        
        initViews();
        setupRecyclerView();
        loadData();
        setupClickListeners();
    }
    
    private void initViews() {
        recyclerView = findViewById(R.id.recycler_view);
        btnBack = findViewById(R.id.btn_back);
        detailList = new ArrayList<>();
    }
    
    private void setupRecyclerView() {
        adapter = new HistoryDetailAdapter(detailList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }
    
    private void setupClickListeners() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    
    private void loadData() {
        try {
            // Get data from HistoryActivity
            List<HistoryItem> fullData = HistoryActivity.getFullHistoryData();
            
            if (fullData != null && !fullData.isEmpty()) {
                detailList.clear();
                detailList.addAll(fullData);
                adapter.notifyDataSetChanged();
            } else {
                loadSampleDetailData();
            }
        } catch (Exception e) {
            e.printStackTrace();
            loadSampleDetailData();
        }
    }
    
    private void loadSampleDetailData() {
        detailList.clear();
        
        // Create sample detail data
        HistoryItem[] sampleData = {
            new HistoryItem("03:44", "11-05-2025", 2018619, "Kredit", "FAILED", "197870"),
            new HistoryItem("03:10", "12-05-2025", 3974866, "QRIS", "SUCCESS", "053059"),
            new HistoryItem("15:17", "13-05-2025", 2418167, "QRIS", "FAILED", "668320"),
            new HistoryItem("12:09", "11-05-2025", 3429230, "Debit", "FAILED", "454790"),
            new HistoryItem("08:39", "10-05-2025", 4656447, "QRIS", "FAILED", "454248"),
            new HistoryItem("00:35", "12-05-2025", 3507704, "QRIS", "FAILED", "301644"),
            new HistoryItem("22:43", "13-05-2025", 4277904, "Debit", "SUCCESS", "388709"),
            new HistoryItem("18:16", "11-05-2025", 4456904, "Debit", "FAILED", "986861"),
            new HistoryItem("12:51", "10-05-2025", 3027953, "Kredit", "SUCCESS", "771339"),
            new HistoryItem("19:50", "14-05-2025", 4399035, "QRIS", "FAILED", "103478")
        };
        
        for (HistoryItem item : sampleData) {
            detailList.add(item);
        }
        
        adapter.notifyDataSetChanged();
    }
}

// HistoryDetailAdapter class - simplified for stability
class HistoryDetailAdapter extends RecyclerView.Adapter<HistoryDetailAdapter.DetailViewHolder> {
    
    private List<HistoryItem> detailList;
    
    public HistoryDetailAdapter(List<HistoryItem> detailList) {
        this.detailList = detailList;
    }
    
    @NonNull
    @Override
    public DetailViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_history_detail, parent, false);
        return new DetailViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull DetailViewHolder holder, int position) {
        HistoryItem item = detailList.get(position);
        
        try {
            holder.tvReferenceId.setText("Ref: " + item.getReferenceId());
            holder.tvAmount.setText("Rp. " + formatCurrency(item.getAmount()));
            holder.tvChannel.setText(item.getChannelName());
            holder.tvMerchant.setText("TEST MERCHANT");
            holder.tvTime.setText(formatDateTime(item.getTime(), item.getDate()));
            holder.tvIssuer.setText("BANK MANDIRI");
            
            // Set status color
            String status = item.getStatus();
            if ("SUCCESS".equals(status)) {
                holder.tvStatus.setText("Berhasil");
                holder.tvStatus.setTextColor(0xFF4CAF50); // Green
            } else if ("FAILED".equals(status)) {
                holder.tvStatus.setText("Gagal");
                holder.tvStatus.setTextColor(0xFFF44336); // Red
            } else {
                holder.tvStatus.setText("Tertunda");
                holder.tvStatus.setTextColor(0xFFFF9800); // Orange
            }
        } catch (Exception e) {
            e.printStackTrace();
            // Set default values if error occurs
            holder.tvReferenceId.setText("Ref: " + position);
            holder.tvAmount.setText("Rp. 0");
            holder.tvChannel.setText("Unknown");
            holder.tvMerchant.setText("TEST MERCHANT");
            holder.tvTime.setText("00:00, 01-01-2025");
            holder.tvIssuer.setText("UNKNOWN");
            holder.tvStatus.setText("Tidak Diketahui");
            holder.tvStatus.setTextColor(0xFF666666);
        }
    }
    
    @Override
    public int getItemCount() {
        return detailList != null ? detailList.size() : 0;
    }
    
    private String formatCurrency(long amount) {
        try {
            NumberFormat formatter = NumberFormat.getNumberInstance(new Locale("id", "ID"));
            return formatter.format(amount);
        } catch (Exception e) {
            return String.valueOf(amount);
        }
    }
    
    private String formatDateTime(String time, String date) {
        try {
            return time + ", " + date;
        } catch (Exception e) {
            return "00:00, 01-01-2025";
        }
    }
    
    static class DetailViewHolder extends RecyclerView.ViewHolder {
        TextView tvReferenceId;
        TextView tvAmount;
        TextView tvChannel;
        TextView tvMerchant;
        TextView tvTime;
        TextView tvIssuer;
        TextView tvStatus;
        
        public DetailViewHolder(@NonNull View itemView) {
            super(itemView);
            try {
                tvReferenceId = itemView.findViewById(R.id.tv_reference_id);
                tvAmount = itemView.findViewById(R.id.tv_amount);
                tvChannel = itemView.findViewById(R.id.tv_channel);
                tvMerchant = itemView.findViewById(R.id.tv_merchant);
                tvTime = itemView.findViewById(R.id.tv_time);
                tvIssuer = itemView.findViewById(R.id.tv_issuer);
                tvStatus = itemView.findViewById(R.id.tv_status);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}