package org.deke.risk.riskahead.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.google.firebase.crashlytics.FirebaseCrashlytics;

import org.deke.risk.riskahead.R;
import org.deke.risk.riskahead.helper.IncidentReport;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 *
 * (C) Copyright 2016 RiskAhead (http://www.riskahead.net)
 * Developer & Contributor: Dennis Thießen (dennis.thiessen@riskahead.net)
 * Date of Release: 10.02.2016
 *
 */
public class ReportListAdapter extends BaseAdapter {
    private final static String TAG = ReportListAdapter.class.getSimpleName();

    private List<JSONObject> data;

    private static LayoutInflater inflater = null;

    public ReportListAdapter(Context context, List<JSONObject> data) {
        this.data = data;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null) vi = inflater.inflate(R.layout.fragment_reportlist_row, parent, false);

        TextView itemText = vi.findViewById(R.id.txt_reportlist_lvrow_text);
        TextView titleText = vi.findViewById(R.id.txt_reportlist_lvrow_title);
        TextView categoryText = vi.findViewById(R.id.txt_reportlist_lvrow_category);
        LinearLayout ll = vi.findViewById(R.id.lay_reportlist_color);
        ImageView iv = vi.findViewById(R.id.ic_reportlist_row);
        LinearLayout bglayout = vi.findViewById(R.id.lay_reportlist_bgcolor);

        boolean status = true;

        try {
            if(data.get(position).getInt("status") == 0){
               status = false;
            }
        } catch (JSONException e) {
            FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
            crashlytics.log("JSONException caught: " + e);
        }

        if(!status){
            bglayout.setBackgroundColor(Color.argb(120, 255, 0, 0));

            titleText.setText("");
            itemText.setText(R.string.txt_reportlist_deleted);
            itemText.setGravity(Gravity.CENTER);
            itemText.setPadding(10,0,0,0);
            categoryText.setText("");

            ll.setBackgroundColor(Color.argb(150, 255, 0, 0));
            iv.setVisibility(View.GONE);
        }else{
            bglayout.setBackgroundColor(Color.argb(0, 0, 0, 0));
            IncidentReport item = new IncidentReport(data.get(position));
            titleText.setText(item.getIncidentCategoryName(vi.getContext()));
            itemText.setText(item.getIncidentSubCategoryName(vi.getContext()));
            itemText.setGravity(Gravity.START);
            itemText.setPadding(0,0,0,0);
            categoryText.setText(item.getTimeString(vi.getContext()));


            titleText.setTypeface(null, Typeface.BOLD);
            ll.setBackgroundColor(item.getCategoryColor());
            iv.setVisibility(View.VISIBLE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                iv.setBackground(ContextCompat.getDrawable(parent.getContext(), item.getIconDrawable()));
            }
        }
        return vi;
    }
}