package com.example.bdkipoc;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
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 HistoryActivity extends AppCompatActivity {
    
    private TextView tvTotalAmount;
    private TextView tvTotalTransactions;
    private TextView btnLihatDetailTop;
    private Button btnLihatDetailBottom;
    private RecyclerView recyclerView;
    private HistoryAdapter adapter;
    private List<HistoryItem> historyList;
    private ImageView btnBack;
    
    // Store full data for detail view
    private static List<HistoryItem> fullHistoryData = new ArrayList<>();
    
    private String API_URL = "https://be-edc.msvc.app/transactions?page=0&limit=50&sortOrder=DESC&from_date=2025-05-10&to_date=2025-05-21&location_id=0&merchant_id=0&sortColumn=id";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        
        initViews();
        setupRecyclerView();
        fetchApiData();
        setupClickListeners();
    }
    
    private void initViews() {
        tvTotalAmount = findViewById(R.id.tv_total_amount);
        tvTotalTransactions = findViewById(R.id.tv_total_transactions);
        btnLihatDetailTop = findViewById(R.id.btn_lihat_detail);
        btnLihatDetailBottom = findViewById(R.id.btn_lihat_detail_bottom);
        recyclerView = findViewById(R.id.recycler_view);
        btnBack = findViewById(R.id.btn_back);
        
        historyList = new ArrayList<>();
    }
    
    private void setupRecyclerView() {
        adapter = new HistoryAdapter(historyList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }
    
    private void setupClickListeners() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        View.OnClickListener detailClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(HistoryActivity.this, HistoryDetailActivity.class);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(HistoryActivity.this, "Error opening detail", Toast.LENGTH_SHORT).show();
                }
            }
        };
        
        btnLihatDetailTop.setOnClickListener(detailClickListener);
        btnLihatDetailBottom.setOnClickListener(detailClickListener);
    }
    
    private void fetchApiData() {
        new ApiTask().execute(API_URL);
    }
    
    private void processApiData(JSONArray dataArray) {
        try {
            historyList.clear();
            fullHistoryData.clear(); // Clear static data
            
            final long[] totalAmountArray = {0};
            final int[] totalTransactionsArray = {0};
            
            for (int i = 0; i < dataArray.length(); i++) {
                JSONObject item = dataArray.getJSONObject(i);
                
                String channelCode = item.getString("channel_code");
                String amount = item.getString("amount");
                String status = item.getString("status");
                String transactionDate = item.getString("transaction_date");
                String referenceId = item.getString("reference_id");
                
                // Parse amount safely
                double amountValue = 0;
                try {
                    amountValue = Double.parseDouble(amount);
                } catch (NumberFormatException e) {
                    amountValue = 0;
                }
                
                // Create history item
                HistoryItem historyItem = new HistoryItem();
                historyItem.setTime(formatTime(transactionDate));
                historyItem.setDate(formatDate(transactionDate));
                historyItem.setAmount((long) amountValue);
                historyItem.setChannelName(formatChannelName(channelCode));
                historyItem.setStatus(status);
                historyItem.setReferenceId(referenceId);
                historyItem.setFullDate(transactionDate);
                historyItem.setChannelCode(channelCode);
                
                // Add to full data
                fullHistoryData.add(historyItem);
                
                // Add first 10 to display list
                if (i < 10) {
                    historyList.add(historyItem);
                }
                
                totalAmountArray[0] += (long) amountValue;
                totalTransactionsArray[0]++;
            }
            
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    updateSummary(totalAmountArray[0], totalTransactionsArray[0]);
                    adapter.notifyDataSetChanged();
                }
            });
            
        } catch (JSONException e) {
            e.printStackTrace();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(HistoryActivity.this, "Error parsing data", Toast.LENGTH_SHORT).show();
                    loadSampleData();
                }
            });
        }
    }
    
    private void updateSummary(long totalAmount, int totalTransactions) {
        tvTotalAmount.setText("RP " + formatCurrency(totalAmount));
        tvTotalTransactions.setText(String.valueOf(totalTransactions));
    }
    
    private void loadSampleData() {
        historyList.clear();
        fullHistoryData.clear();
        
        // Create sample 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")
        };
        
        for (HistoryItem item : sampleData) {
            historyList.add(item);
            fullHistoryData.add(item);
        }
        
        tvTotalAmount.setText("RP 36.166.829");
        tvTotalTransactions.setText("10");
        
        adapter.notifyDataSetChanged();
    }
    
    private String formatChannelName(String channelCode) {
        switch (channelCode) {
            case "DEBIT":
                return "Debit";
            case "QRIS":
                return "QRIS";
            case "OTHER":
                return "Kredit";
            default:
                return channelCode.substring(0, 1).toUpperCase() + 
                       channelCode.substring(1).toLowerCase();
        }
    }
    
    private String formatTime(String isoDate) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
            Date date = inputFormat.parse(isoDate);
            return outputFormat.format(date);
        } catch (ParseException e) {
            return "00:00";
        }
    }
    
    private String formatDate(String isoDate) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
            Date date = inputFormat.parse(isoDate);
            return outputFormat.format(date);
        } catch (ParseException e) {
            return "01-01-2025";
        }
    }
    
    private String formatCurrency(long amount) {
        NumberFormat formatter = NumberFormat.getNumberInstance(new Locale("id", "ID"));
        return formatter.format(amount);
    }
    
    // Public static method to get full data for detail activity
    public static List<HistoryItem> getFullHistoryData() {
        return new ArrayList<>(fullHistoryData);
    }
    
    // AsyncTask for API call
    private class ApiTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            try {
                URL url = new URL(urls[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(10000);
                connection.setReadTimeout(10000);
                
                int responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder response = new StringBuilder();
                    String line;
                    
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    reader.close();
                    return response.toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        
        @Override
        protected void onPostExecute(String result) {
            if (result != null) {
                try {
                    JSONObject jsonResponse = new JSONObject(result);
                    if (jsonResponse.getInt("status") == 200) {
                        JSONObject results = jsonResponse.getJSONObject("results");
                        JSONArray dataArray = results.getJSONArray("data");
                        processApiData(dataArray);
                    } else {
                        Toast.makeText(HistoryActivity.this, "API Error", Toast.LENGTH_SHORT).show();
                        loadSampleData();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(HistoryActivity.this, "JSON Parse Error", Toast.LENGTH_SHORT).show();
                    loadSampleData();
                }
            } else {
                Toast.makeText(HistoryActivity.this, "Network Error", Toast.LENGTH_SHORT).show();
                loadSampleData();
            }
        }
    }
}

// HistoryItem class - enhanced with more fields
class HistoryItem {
    private String time;
    private String date;
    private long amount;
    private String channelName;
    private String status;
    private String referenceId;
    private String fullDate;
    private String channelCode;
    
    public HistoryItem() {}
    
    public HistoryItem(String time, String date, long amount, String channelName, String status, String referenceId) {
        this.time = time;
        this.date = date;
        this.amount = amount;
        this.channelName = channelName;
        this.status = status;
        this.referenceId = referenceId;
    }
    
    // Getters and Setters
    public String getTime() { return time; }
    public void setTime(String time) { this.time = time; }
    public String getDate() { return date; }
    public void setDate(String date) { this.date = date; }
    public long getAmount() { return amount; }
    public void setAmount(long amount) { this.amount = amount; }
    public String getChannelName() { return channelName; }
    public void setChannelName(String channelName) { this.channelName = channelName; }
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }
    public String getReferenceId() { return referenceId; }
    public void setReferenceId(String referenceId) { this.referenceId = referenceId; }
    public String getFullDate() { return fullDate; }
    public void setFullDate(String fullDate) { this.fullDate = fullDate; }
    public String getChannelCode() { return channelCode; }
    public void setChannelCode(String channelCode) { this.channelCode = channelCode; }
}

// HistoryAdapter class - simplified and stable
class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryViewHolder> {
    
    private List<HistoryItem> historyList;
    
    public HistoryAdapter(List<HistoryItem> historyList) {
        this.historyList = historyList;
    }
    
    @NonNull
    @Override
    public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_history, parent, false);
        return new HistoryViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull HistoryViewHolder holder, int position) {
        HistoryItem item = historyList.get(position);
        
        holder.tvTime.setText(item.getTime() + ", " + item.getDate());
        holder.tvAmount.setText("Rp. " + formatCurrency(item.getAmount()));
        holder.tvChannel.setText(item.getChannelName());
        
        // 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
        }
    }
    
    @Override
    public int getItemCount() {
        return historyList != null ? historyList.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);
        }
    }
    
    static class HistoryViewHolder extends RecyclerView.ViewHolder {
        TextView tvTime;
        TextView tvAmount;
        TextView tvChannel;
        TextView tvStatus;
        
        public HistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvAmount = itemView.findViewById(R.id.tv_amount);
            tvChannel = itemView.findViewById(R.id.tv_channel);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }
    }
}