package org.deke.risk.riskahead.helper;

import android.content.Context;
import android.util.Log;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.view.DefaultClusterRenderer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 *
 * (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 OwnIconRendered extends DefaultClusterRenderer<IncidentReport> {
    private final static String TAG = OwnIconRendered.class.getSimpleName();
    Context context;

    public OwnIconRendered(Context context, GoogleMap map,ClusterManager<IncidentReport> clusterManager) {
        super(context, map, clusterManager);
        this.context = context;
    }

    @Override
    protected void onBeforeClusterItemRendered(IncidentReport item, MarkerOptions markerOptions) {
        markerOptions.icon(item.getIcon());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        Date dateToCheck = null;

        try {
            if(item.getToDate() != null && !item.getToDate().equals("0000-00-00")){
                dateToCheck = simpleDateFormat.parse(item.getToDate());
            }else if(item.getFromDate() != null){
                dateToCheck = simpleDateFormat.parse(item.getFromDate());
            }
        } catch (ParseException e) {
            FirebaseCrashlytics.getInstance().log("ParseException caught");
            FirebaseCrashlytics.getInstance().recordException(e);
        }

        if(dateToCheck != null && !dateToCheck.after(new Date(System.currentTimeMillis()-14*24*60*60*1000))){
            markerOptions.alpha(0.5f);
        }

        markerOptions.snippet(item.getIncidentSubCategoryName(context));
        markerOptions.title(item.getIncidentCategoryName(context));
        super.onBeforeClusterItemRendered(item, markerOptions);
    }
}
