package org.deke.risk.riskahead.helper;

import android.content.Context;
import android.graphics.Color;

import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterItem;

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

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

/**
 * Created by Dennis on 12.11.2015.
 */
public class IncidentReport implements ClusterItem {

    private int id = 0;
    private LatLng incidentPosition;
    private String incidentPositionDescription = "";

    private int fidFromUser = 0;
    private String fromUsername = "";

    private int votedScore = 0;

    private String createdAt = "";
    private String updatedAt = "";

    private int fidIncidentCategory = 0;
    private int fidIncidentSubCategory = 0;
    private String etcIncidentCategory = "";

    private int fidTimeCategory = 0;
    private int fidTimeSubCategory = 0;
    private String fromTime = "";
    private String fromDate = "";
    private String toTime = "";
    private String toDate = "";

    private int fidSuspectCategory = 0;
    private int fidSuspectSubCategory = 0;
    private String etcSuspectCategory = "";

    private int fidSuspectWeaponCategory = 0;
    private int fidSuspectWeaponSubCategory = 0;
    private String etcSuspectWeaponCategory = "";

    private int fidSuspectCountCategory = 0;
    private String etcSuspectCountCategory = "";

    private int fidVictimCategory = 0;

    private int fidVictimOriginCategory = 0;
    private String etcVictimOriginCategory = "";

    private int fidSuspectTransportationCategory = 0;
    private String etcSuspectTransportationCategory = "";

    private int fidVictimBelongingCategory = 0;
    private String etcVictimBelongingCategory = "";

    private int fidSuspectCharacteristicsCategory = 0;
    private String etcSuspectCharacteristicsCategory = "";

    private int fidOthersCategory = 0;
    private String etcOthersCategory = "";

    public IncidentReport(JSONObject mIncident, LatLng position) {
        setAllAttributes(mIncident, position);
    }

    public IncidentReport(JSONObject mIncident) {
        setAllAttributes(mIncident,null);
    }

    public IncidentReport(){

    }

    private void setAllAttributes(JSONObject mIncident, LatLng position) {
        try {
            this.id = mIncident.getInt("id");

            if(position != null) {
                this.incidentPosition = position;
            }else{
                this.incidentPosition = new LatLng(mIncident.getDouble("latitude"),mIncident.getDouble("longitude"));
            }

            if(mIncident.has("created_at")) this.createdAt = mIncident.getString("created_at");
            if(mIncident.has("updated_at")) this.updatedAt = mIncident.getString("updated_at");

            if(mIncident.has("upvoted") && mIncident.has("downvoted")) this.votedScore = mIncident.getInt("upvoted") - mIncident.getInt("downvoted");
            if(mIncident.has("username")) this.fromUsername = mIncident.getString("username");

            if(mIncident.has("fid_user")) this.fidFromUser = mIncident.getInt("fid_user");
            this.fidIncidentCategory = mIncident.getInt("fid_incident");
            this.fidIncidentSubCategory = mIncident.getInt("fid_subincident");
            if(mIncident.has("etc_incident")) this.etcIncidentCategory = mIncident.getString("etc_incident");
            this.fidTimeCategory = mIncident.getInt("fid_time");
            this.fidTimeSubCategory = mIncident.getInt("fid_subtime");
            this.fromTime = mIncident.getString("happened_from_datetime").split(" ")[1];
            this.fromDate = mIncident.getString("happened_from_datetime").split(" ")[0];
            this.toTime = mIncident.getString("happened_to_datetime").split(" ")[1];
            this.toDate = mIncident.getString("happened_to_datetime").split(" ")[0];
            if(mIncident.has("fid_suspect")) this.fidSuspectCategory = mIncident.getInt("fid_suspect");
            if(mIncident.has("fid_subsuspect")) this.fidSuspectSubCategory = mIncident.getInt("fid_subsuspect");
            if(mIncident.has("etc_suspect")) this.etcSuspectCategory = mIncident.getString("etc_suspect");
            if(mIncident.has("fid_weapon")) this.fidSuspectWeaponCategory = mIncident.getInt("fid_weapon");
            if(mIncident.has("fid_subweapon")) this.fidSuspectWeaponSubCategory = mIncident.getInt("fid_subweapon");
            if(mIncident.has("etc_weapon")) this.etcSuspectWeaponCategory = mIncident.getString("etc_weapon");
            if(mIncident.has("fid_suspectcount")) this.fidSuspectCountCategory = mIncident.getInt("fid_suspectcount");
            if(mIncident.has("etc_suspectcount")) this.etcSuspectCountCategory = mIncident.getString("etc_suspectcount");
            if(mIncident.has("fid_victim")) this.fidVictimCategory = mIncident.getInt("fid_victim");
            if(mIncident.has("fid_victimorigin")) this.fidVictimOriginCategory = mIncident.getInt("fid_victimorigin");
            if(mIncident.has("etc_victimorigin")) this.etcVictimOriginCategory = mIncident.getString("etc_victimorigin");
            if(mIncident.has("fid_suspecttransportation")) this.fidSuspectTransportationCategory = mIncident.getInt("fid_suspecttransportation");
            if(mIncident.has("etc_suspecttransportation"))  this.etcSuspectTransportationCategory = mIncident.getString("etc_suspecttransportation");
            if(mIncident.has("fid_victimbelonging")) this.fidVictimBelongingCategory = mIncident.getInt("fid_victimbelonging");
            if(mIncident.has("etc_victimbelonging")) this.etcVictimBelongingCategory = mIncident.getString("etc_victimbelonging");
            if(mIncident.has("fid_suspectcharacteristics")) this.fidSuspectCharacteristicsCategory = mIncident.getInt("fid_suspectcharacteristics");
            if(mIncident.has("etc_suspectcharacteristics")) this.etcSuspectCharacteristicsCategory = mIncident.getString("etc_suspectcharacteristics");
            if(mIncident.has("fid_others")) this.fidOthersCategory = mIncident.getInt("fid_others");
            if(mIncident.has("etc_others")) this.etcOthersCategory = mIncident.getString("etc_others");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public BitmapDescriptor getIcon() {
        return BitmapDescriptorFactory.fromResource(getIconDrawable());
    }

    public int getIconDrawable() {
        int mIcon;

        Date tmpFromDate = null;
        Date today = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            tmpFromDate = simpleDateFormat.parse(this.fromDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        switch (this.fidIncidentCategory) {
            case 1:
                if(tmpFromDate != null && tmpFromDate.after(today)){
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_general_w_f;
                    }else{
                        mIcon = R.drawable.icon_general_f;
                    }
                }else{
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_general_w;
                    }else{
                        mIcon = R.drawable.icon_general;
                    }
                }
                break;
            case 2:
                if(tmpFromDate != null && tmpFromDate.after(today)){
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_verbal_w_f;
                    }else{
                        mIcon = R.drawable.icon_verbal_f;
                    }
                }else{
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_verbal_w;
                    }else{
                        mIcon = R.drawable.icon_verbal;
                    }
                }
                break;
            case 3:
                if(tmpFromDate != null && tmpFromDate.after(today)){
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_middle_violance_w_f;
                    }else{
                        mIcon = R.drawable.icon_middle_violance_f;
                    }
                }else{
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_middle_violance_w;
                    }else{
                        mIcon = R.drawable.icon_middle_violance;
                    }
                }
                break;
            case 4:
                if(tmpFromDate != null && tmpFromDate.after(today)){
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_high_violance_w_f;
                    }else{
                        mIcon = R.drawable.icon_high_violance_f;
                    }
                }else{
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_high_violance_w;
                    }else{
                        mIcon = R.drawable.icon_high_violance;
                    }
                }
                break;
            case 5:
                if(tmpFromDate != null && tmpFromDate.after(today)){
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_nature_w_f;
                    }else{
                        mIcon = R.drawable.icon_nature_f;
                    }
                }else{
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_nature_w;
                    }else{
                        mIcon = R.drawable.icon_nature;
                    }
                }
                break;
            case 6:
                if(tmpFromDate != null && tmpFromDate.after(today)){
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_infra_v2_w_f;
                    }else{
                        mIcon = R.drawable.icon_infra_v2_f;
                    }
                }else{
                    if(fidVictimCategory == 1){
                        mIcon = R.drawable.icon_infra_v2_w;
                    }else{
                        mIcon = R.drawable.icon_infra_v2;
                    }
                }
                break;
            default:
                mIcon = R.drawable.icon_general;
                break;
        }

        return mIcon;
    }

    public float getScoreStars() {
        if(this.votedScore < 0){
            return 0.0f;
        }else if(this.votedScore < 1){
            return 0.5f;
        }else if(this.votedScore < 2){
            return 1.0f;
        }else if(this.votedScore < 3){
            return 1.5f;
        }else if(this.votedScore < 4){
            return 2.0f;
        }else if(this.votedScore < 5){
            return 2.5f;
        }else if(this.votedScore < 6){
            return 3.0f;
        }else if(this.votedScore < 7){
            return 3.5f;
        }else if(this.votedScore < 8){
            return 4.0f;
        }else if(this.votedScore < 9){
            return 4.5f;
        }else{
            return 5.0f;
        }
    }

    public void resetDatetime() {
        toTime = "";
        toDate = "";
        fromTime = "";
        fromDate = "";
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getIncidentCategoryName(Context context){
        return context.getResources().getStringArray(R.array.cat_situation_main)[this.fidIncidentCategory-1];
    }

    public String getIncidentSubCategoryName(Context context){
        String returnName = "";
        switch(this.fidIncidentCategory){
            case 1:
                returnName = context.getResources().getStringArray(R.array.cat_situation_sub_general)[this.fidIncidentSubCategory-1];
                break;
            case 2:
                returnName = context.getResources().getStringArray(R.array.cat_situation_sub_verbal)[this.fidIncidentSubCategory-1];
                break;
            case 3:
                returnName = context.getResources().getStringArray(R.array.cat_situation_sub_force)[this.fidIncidentSubCategory-1];
                break;
            case 4:
                returnName = context.getResources().getStringArray(R.array.cat_situation_sub_force_serious)[this.fidIncidentSubCategory-1];
                break;
            case 5:
                returnName = context.getResources().getStringArray(R.array.cat_situation_sub_nature)[this.fidIncidentSubCategory-1];
                break;
            case 6:
                returnName = context.getResources().getStringArray(R.array.cat_situation_sub_infrastructure)[this.fidIncidentSubCategory-1];
                break;
        }
        return returnName;
    }

    public String getTimeString(Context context){
        String returnString = "";
        switch (this.fidTimeCategory){
            case 1:
                if((this.fromDate != null) && !(this.fromDate.equals("0000-00-00"))){
                    returnString = this.fromDate+" "+this.fromTime.substring(0,5);
                }else if(this.fromDate.equals("0000-00-00")){
                    returnString = this.createdAt.substring(0,16);
                }else{
                    returnString = context.getResources().getStringArray(R.array.cat_time_main)[this.fidTimeCategory-1];
                }
                break;
            case 2:
                returnString = context.getResources().getStringArray(R.array.cat_time_sub_periodical)[this.fidTimeSubCategory-1];
                break;
            case 3:
                if((this.fromTime != null) && !(this.fromTime.equals("00:00:00"))){
                    returnString = this.fromDate+" "+this.fromTime.substring(0,5);
                }else{
                    returnString = this.fromDate;
                }
                break;
            case 4:
                if((this.fromTime != null) && !(this.fromTime.equals("00:00")) && (this.toTime != null) && !(this.toTime.equals("00:00"))){
                    returnString = this.fromDate+" "+this.fromTime.substring(0,5)+" -> "+this.toDate+" "+this.toTime.substring(0,5);
                }else{
                    returnString = this.fromDate+" -> "+this.toDate;
                }
                break;
            case 5:
                returnString = context.getResources().getStringArray(R.array.cat_time_main)[this.fidTimeCategory-1];
                break;
        }
        return returnString;
    }

    public String getSuspectString(Context context){
        if(this.fidSuspectCategory > 0){
            return context.getResources().getStringArray(R.array.cat_suspect_main)[this.fidSuspectCategory-1];
        }else{
            return "";
        }
    }

    public String getVictimString(Context context){
        if(this.fidVictimCategory > 0){
            return context.getResources().getStringArray(R.array.cat_victim)[this.fidVictimCategory-1];
        }else{
            return "";
        }
    }

    public int getCategoryColor(){
        String color = "FFFFFF";
        switch (this.fidIncidentCategory) {
            case 1:
                color = "3ac1e2";
                break;
            case 2:
                color = "3ed78d";
                break;
            case 3:
                color = "ed8d53";
                break;
            case 4:
                color = "ff3433";
                break;
            case 5:
                color = "3f7efd";
                break;
            case 6:
                color = "929292";
                break;
        }
        return Color.parseColor("#" + color);
    }

    public String getSuspectWeaponsString(Context context) {
        String returnValue = "";

        if(fidSuspectWeaponCategory != 0) returnValue = context.getResources().getStringArray(R.array.cat_suspect_weapons)[fidSuspectWeaponCategory-1];
        if(!etcSuspectWeaponCategory.equals("")) returnValue += "\n"+etcSuspectWeaponCategory;

        return returnValue;
    }

    public String getSuspectNoOfString(Context context) {
        String returnValue = "";

        if(fidSuspectCountCategory != 0) returnValue = context.getResources().getStringArray(R.array.cat_suspect_count)[fidSuspectCountCategory-1];
        if(!etcSuspectCountCategory.equals("")) returnValue += "\n"+etcSuspectCountCategory;

        return returnValue;
    }

    public String getVictimOriginString(Context context) {
        String returnValue = "";

        if(fidVictimOriginCategory != 0) returnValue = context.getResources().getStringArray(R.array.cat_victim_origin)[fidVictimOriginCategory-1];
        if(!etcVictimOriginCategory.equals("")) returnValue += "\n"+etcVictimOriginCategory;

        return returnValue;
    }

    public String getSuspectTransportString(Context context) {
        String returnValue = "";

        if(fidSuspectTransportationCategory != 0) returnValue = context.getResources().getStringArray(R.array.cat_details_vehicle)[fidSuspectTransportationCategory-1];
        if(!etcSuspectTransportationCategory.equals("")) returnValue += "\n"+etcSuspectTransportationCategory;

        return returnValue;
    }

    public String getVictimAffiliationString(Context context) {
        String returnValue = "";

        if(fidVictimBelongingCategory != 0) returnValue = context.getResources().getStringArray(R.array.cat_details_victimaff)[fidVictimBelongingCategory-1];
        if(!etcVictimBelongingCategory.equals("")) returnValue += "\n"+etcVictimBelongingCategory;

        return returnValue;
    }

    public String getSuspectRecognitionString(Context context) {
        String returnValue = "";

        if(fidSuspectCharacteristicsCategory != 0) returnValue = context.getResources().getStringArray(R.array.cat_details_suspect_recon)[fidSuspectCharacteristicsCategory-1];
        if(!etcSuspectCharacteristicsCategory.equals("")) returnValue += "\n"+etcSuspectCharacteristicsCategory;

        return returnValue;
    }



    /*
    Simple Getters and Setter below
     */

    public void setPosition(LatLng position) {
        this.incidentPosition = position;
    }

    public String getFromUsername() {
        return fromUsername;
    }

    public void setFromUsername(String fromUsername) {
        this.fromUsername = fromUsername;
    }

    public int getFidIncidentCategory() {
        return fidIncidentCategory;
    }

    public void setFidIncidentCategory(int fidIncidentCategory) {
        this.fidIncidentCategory = fidIncidentCategory;
    }

    @Override
    public LatLng getPosition() {
        return this.incidentPosition;
    }

    public int getVotedScore(){
        return this.votedScore;
    }

    public void setVotedScore(int votedScore){
        this.votedScore = votedScore;
    }

    public int getFidFromUser() {
        return fidFromUser;
    }

    public void setFidFromUser(int fidFromUser) {
        this.fidFromUser = fidFromUser;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }

    public int getFidIncidentSubCategory() {
        return fidIncidentSubCategory;
    }

    public void setFidIncidentSubCategory(int fidIncidentSubCategory) {
        this.fidIncidentSubCategory = fidIncidentSubCategory;
    }

    public String getEtcIncidentCategory() {
        return etcIncidentCategory;
    }

    public void setEtcIncidentCategory(String etcIncidentCategory) {
        this.etcIncidentCategory = etcIncidentCategory;
    }

    public int getFidTimeCategory() {
        return fidTimeCategory;
    }

    public void setFidTimeCategory(int fidTimeCategory) {
        this.fidTimeCategory = fidTimeCategory;
    }

    public int getFidTimeSubCategory() {
        return fidTimeSubCategory;
    }

    public void setFidTimeSubCategory(int fidTimeSubCategory) {
        this.fidTimeSubCategory = fidTimeSubCategory;
    }

    public String getFromTime() {
        return fromTime;
    }

    public void setFromTime(String fromTime) {
        this.fromTime = fromTime;
    }

    public String getFromDate() {
        return fromDate;
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public String getToTime() {
        return toTime;
    }

    public void setToTime(String toTime) {
        this.toTime = toTime;
    }

    public String getToDate() {
        return toDate;
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

    public int getFidSuspectCategory() {
        return fidSuspectCategory;
    }

    public void setFidSuspectCategory(int fidSuspectCategory) {
        this.fidSuspectCategory = fidSuspectCategory;
    }

    public int getFidSuspectSubCategory() {
        return fidSuspectSubCategory;
    }

    public void setFidSuspectSubCategory(int fidSuspectSubCategory) {
        this.fidSuspectSubCategory = fidSuspectSubCategory;
    }

    public String getEtcSuspectCategory() {
        return etcSuspectCategory;
    }

    public void setEtcSuspectCategory(String etcSuspectCategory) {
        this.etcSuspectCategory = etcSuspectCategory;
    }

    public int getFidSuspectWeaponCategory() {
        return fidSuspectWeaponCategory;
    }

    public void setFidSuspectWeaponCategory(int fidSuspectWeaponCategory) {
        this.fidSuspectWeaponCategory = fidSuspectWeaponCategory;
    }

    public int getFidSuspectWeaponSubCategory() {
        return fidSuspectWeaponSubCategory;
    }

    public void setFidSuspectWeaponSubCategory(int fidSuspectWeaponSubCategory) {
        this.fidSuspectWeaponSubCategory = fidSuspectWeaponSubCategory;
    }

    public String getEtcSuspectWeaponCategory() {
        return etcSuspectWeaponCategory;
    }

    public void setEtcSuspectWeaponCategory(String etcSuspectWeaponCategory) {
        this.etcSuspectWeaponCategory = etcSuspectWeaponCategory;
    }

    public int getFidSuspectCountCategory() {
        return fidSuspectCountCategory;
    }

    public void setFidSuspectCountCategory(int fidSuspectCountCategory) {
        this.fidSuspectCountCategory = fidSuspectCountCategory;
    }

    public String getEtcSuspectCountCategory() {
        return etcSuspectCountCategory;
    }

    public void setEtcSuspectCountCategory(String etcSuspectCountCategory) {
        this.etcSuspectCountCategory = etcSuspectCountCategory;
    }

    public int getFidVictimCategory() {
        return fidVictimCategory;
    }

    public void setFidVictimCategory(int fidVictimCategory) {
        this.fidVictimCategory = fidVictimCategory;
    }

    public int getFidVictimOriginCategory() {
        return fidVictimOriginCategory;
    }

    public void setFidVictimOriginCategory(int fidVictimOriginCategory) {
        this.fidVictimOriginCategory = fidVictimOriginCategory;
    }

    public String getEtcVictimOriginCategory() {
        return etcVictimOriginCategory;
    }

    public void setEtcVictimOriginCategory(String etcVictimOriginCategory) {
        this.etcVictimOriginCategory = etcVictimOriginCategory;
    }

    public int getFidSuspectTransportationCategory() {
        return fidSuspectTransportationCategory;
    }

    public void setFidSuspectTransportationCategory(int fidSuspectTransportationCategory) {
        this.fidSuspectTransportationCategory = fidSuspectTransportationCategory;
    }

    public String getEtcSuspectTransportationCategory() {
        return etcSuspectTransportationCategory;
    }

    public void setEtcSuspectTransportationCategory(String etcSuspectTransportationCategory) {
        this.etcSuspectTransportationCategory = etcSuspectTransportationCategory;
    }

    public int getFidVictimBelongingCategory() {
        return fidVictimBelongingCategory;
    }

    public void setFidVictimBelongingCategory(int fidVictimBelongingCategory) {
        this.fidVictimBelongingCategory = fidVictimBelongingCategory;
    }

    public String getEtcVictimBelongingCategory() {
        return etcVictimBelongingCategory;
    }

    public void setEtcVictimBelongingCategory(String etcVictimBelongingCategory) {
        this.etcVictimBelongingCategory = etcVictimBelongingCategory;
    }

    public int getFidSuspectCharacteristicsCategory() {
        return fidSuspectCharacteristicsCategory;
    }

    public void setFidSuspectCharacteristicsCategory(int fidSuspectCharacteristicsCategory) {
        this.fidSuspectCharacteristicsCategory = fidSuspectCharacteristicsCategory;
    }

    public String getEtcSuspectCharacteristicsCategory() {
        return etcSuspectCharacteristicsCategory;
    }

    public void setEtcSuspectCharacteristicsCategory(String etcSuspectCharacteristicsCategory) {
        this.etcSuspectCharacteristicsCategory = etcSuspectCharacteristicsCategory;
    }

    public int getFidOthersCategory() {
        return fidOthersCategory;
    }

    public void setFidOthersCategory(int fidOthersCategory) {
        this.fidOthersCategory = fidOthersCategory;
    }

    public String getEtcOthersCategory() {
        return etcOthersCategory;
    }

    public void setEtcOthersCategory(String etcOthersCategory) {
        this.etcOthersCategory = etcOthersCategory;
    }

    public String getIncidentPositionDescription() {
        return incidentPositionDescription;
    }

    public void setIncidentPositionDescription(String incidentPositionDescription) {
        this.incidentPositionDescription = incidentPositionDescription;
    }



}
