package com.maxmoney.maxcddconsole.Adapter;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.maxmoney.maxcddconsole.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class TopUpLedgerAdapter extends BaseAdapter {

    private ArrayList<Map<String, String>> data = null;
    private Context context = null;
    private LayoutInflater inflater;

    private class ViewHolder {
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        TextView tv5;
        TextView tv6;
        TextView tv7;
        TextView tv8;
        TextView tv9;
        LinearLayout linear_status;
    }

    public TopUpLedgerAdapter(Context context, ArrayList<Map<String, String>> data) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return (data == null) ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return (data == null) ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;

        if (view == null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_adapter_topap_ledger, null);

            holder = new ViewHolder();

            holder.linear_status = view.findViewById(R.id.linear_status);
            holder.tv1 = view.findViewById(R.id.textView_mobile);
            holder.tv2 = view.findViewById(R.id.textView_status);
            holder.tv3 = view.findViewById(R.id.textView_country);
            holder.tv4 = view.findViewById(R.id.textView_product);
            holder.tv5 = view.findViewById(R.id.textView_debit);
            holder.tv6 = view.findViewById(R.id.textView_balance);
            holder.tv7 = view.findViewById(R.id.textView_profit);
            holder.tv8 = view.findViewById(R.id.textView_time);
            holder.tv9 = view.findViewById(R.id.textView_des);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if(data.get(position).get("msg2").equals("FAILURE")){
            holder.linear_status.setBackgroundColor(Color.RED);
        }else{
            holder.linear_status.setBackgroundColor(Color.parseColor("#39B449"));
        }

        holder.tv1.setText(data.get(position).get("msg1"));
        holder.tv2.setText(data.get(position).get("msg2"));
        holder.tv3.setText(data.get(position).get("msg3"));
        holder.tv4.setText(data.get(position).get("msg4"));
        holder.tv5.setText(data.get(position).get("msg5"));
        holder.tv6.setText(data.get(position).get("msg6"));
        holder.tv7.setText(data.get(position).get("msg7"));
        holder.tv8.setText(data.get(position).get("msg8"));
        holder.tv9.setText(data.get(position).get("msg9"));

        return view;
    }

    public void refreshData(ArrayList<Map<String, String>> newData) {
        this.data = newData;
        this.notifyDataSetChanged();
    }

    public CharSequence createDate(long timestamp) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        Date d = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(d);
    }

}