package org.deke.risk.riskahead.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.annotation.NonNull;

import com.google.firebase.crashlytics.FirebaseCrashlytics;

import org.deke.risk.riskahead.R;
import org.deke.risk.riskahead.ReportWFActivity;
import org.deke.risk.riskahead.adapter.SpinnerHintAdapter;
import org.deke.risk.riskahead.helper.IncidentReport;

/**
 *
 * (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 ReportWF_8_Fragment extends BaseReportWFFragment {
    private final static int FEET = 1;
    private final static int MOTORBIKE = 2;
    private final static int CAR = 3;
    private final static int BUS = 4;
    private final static int TANK = 5;
    private final static int PLANE = 6;
    private final static int ETC_1 = 7;

    private final static int RELIGION = 1;
    private final static int ETHNICS = 2;
    private final static int POLITICS = 3;
    private final static int SOCIAL_GROUP = 4;
    private final static int JOB = 5;
    private final static int LANGUAGE = 6;
    private final static int ETC_2 = 7;

    private final static int MEN = 1;
    private final static int WOMEN = 2;
    private final static int CHILDREN = 3;
    private final static int ANIMALS = 4;
    private final static int ETC_3 = 5;

    private final static String TAG = ReportWF_8_Fragment.class.getSimpleName();

    private View viewFragment;

    private Spinner suspectVehicle;
    private EditText suspectVehicleEtcInput;

    private Spinner victimAffiliation;
    private EditText victimAffiliationEtcInput;

    private Spinner suspectRecongnition;
    private EditText suspectRecongnitionEtcInput;

    private EditText moreEtcInput;

    private IncidentReport incident;

    private OnInputFinishedListener mCallback;

    public interface OnInputFinishedListener {
        void onInputFinished(boolean finished);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        viewFragment = inflater.inflate(R.layout.fragment_reportwf_8, container, false);

        initSuspectVehicleInputElements();
        setSuspectVehicleMainSpinnerValues(R.array.cat_details_vehicle);

        initVictimAffiliationInputElements();
        setVictimAffiliationMainSpinnerValues(R.array.cat_details_victimaff);

        initSuspectRecognitionInputElements();
        setSuspectRecognitionMainSpinnerValues(R.array.cat_details_suspect_recon);

        moreEtcInput = viewFragment.findViewById(R.id.txt_reportwf_8_etc);

        return viewFragment;
    }

    @Override
    public IncidentReport getCurrentInput() {
        saveInput();
        return incident;
    }

    private void initSuspectVehicleInputElements() {
        suspectVehicle = viewFragment.findViewById(R.id.dd_reportwf_8_suspectvehicle);
        suspectVehicleEtcInput = viewFragment.findViewById(R.id.txt_reportwf_8_suspectvehicle);

        suspectVehicleEtcInput.setVisibility(View.INVISIBLE);

        suspectVehicle.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case FEET:
                        suspectVehicleEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case MOTORBIKE:
                        suspectVehicleEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case CAR:
                        suspectVehicleEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case BUS:
                        suspectVehicleEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case TANK:
                        suspectVehicleEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case PLANE:
                        suspectVehicleEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case ETC_1:
                        suspectVehicleEtcInput.setVisibility(View.VISIBLE);
                        break;
                }

                checkIfFinished();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // do nothing
            }
        });

    }

    private void setSuspectVehicleMainSpinnerValues(int stringId){
        ReportWFActivity activity = (ReportWFActivity) getActivity();
        if(activity != null){
            ArrayAdapter<CharSequence> adapter_main_1 = ArrayAdapter.createFromResource(activity, stringId, android.R.layout.simple_spinner_dropdown_item);
            suspectVehicle.setAdapter(new SpinnerHintAdapter(adapter_main_1, R.layout.contact_spinner_row_nothing_selected, activity));
        }else{
            FirebaseCrashlytics.getInstance().log("Could not get parent Activity");
        }
    }

    private void initVictimAffiliationInputElements() {
        victimAffiliation = viewFragment.findViewById(R.id.dd_reportwf_8_victimaff);
        victimAffiliationEtcInput = viewFragment.findViewById(R.id.txt_reportwf_8_victimaff);

        victimAffiliationEtcInput.setVisibility(View.INVISIBLE);

        victimAffiliation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case RELIGION:
                        victimAffiliationEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case ETHNICS:
                        victimAffiliationEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case POLITICS:
                        victimAffiliationEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case SOCIAL_GROUP:
                        victimAffiliationEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case JOB:
                        victimAffiliationEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case LANGUAGE:
                        victimAffiliationEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case ETC_2:
                        victimAffiliationEtcInput.setVisibility(View.VISIBLE);
                        break;
                }

                checkIfFinished();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // do nothing
            }
        });

    }

    private void setVictimAffiliationMainSpinnerValues(int stringId){
        ReportWFActivity activity = (ReportWFActivity) getActivity();
        if(activity != null){
            ArrayAdapter<CharSequence> adapter_main_1 = ArrayAdapter.createFromResource(activity, stringId, android.R.layout.simple_spinner_dropdown_item);
            victimAffiliation.setAdapter(new SpinnerHintAdapter(adapter_main_1, R.layout.contact_spinner_row_nothing_selected, activity));
        }else{
            FirebaseCrashlytics.getInstance().log("Could not get parent Activity");
        }
    }

    private void initSuspectRecognitionInputElements() {
        suspectRecongnition = viewFragment.findViewById(R.id.dd_reportwf_8_recon);
        suspectRecongnitionEtcInput = viewFragment.findViewById(R.id.txt_reportwf_8_recon);

        suspectRecongnitionEtcInput.setVisibility(View.INVISIBLE);

        suspectRecongnition.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case MEN:
                        suspectRecongnitionEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case WOMEN:
                        suspectRecongnitionEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case CHILDREN:
                        suspectRecongnitionEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case ANIMALS:
                        suspectRecongnitionEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case ETC_3:
                        suspectRecongnitionEtcInput.setVisibility(View.VISIBLE);
                        break;
                }

                checkIfFinished();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // do nothing
            }
        });

    }

    private void setSuspectRecognitionMainSpinnerValues(int stringId){
        ReportWFActivity activity = (ReportWFActivity) getActivity();
        if(activity != null){
            ArrayAdapter<CharSequence> adapter_main_1 = ArrayAdapter.createFromResource(activity, stringId, android.R.layout.simple_spinner_dropdown_item);
            suspectRecongnition.setAdapter(new SpinnerHintAdapter(adapter_main_1, R.layout.contact_spinner_row_nothing_selected, activity));
        }else{
            FirebaseCrashlytics.getInstance().log("Could not get parent Activity");
        }
    }

    private void checkIfFinished(){
        boolean isSuspectVehicleFilled = false;
        boolean isVictimAffiliationFilled = false;
        boolean isSuspectRecoginitionFilled = false;

        if(incident != null){
            isSuspectVehicleFilled = (suspectVehicle.getSelectedItemPosition() > 0);
            isVictimAffiliationFilled = (victimAffiliation.getSelectedItemPosition() > 0);
            isSuspectRecoginitionFilled = (suspectRecongnition.getSelectedItemPosition() > 0);
        }

        mCallback.onInputFinished(isSuspectVehicleFilled && isVictimAffiliationFilled && isSuspectRecoginitionFilled);
    }

    @Override
    public void onResume() {
        super.onResume();

        ReportWFActivity activity = (ReportWFActivity) getActivity();
        if(activity != null){
            incident = activity.getIncident();
        }else{
            FirebaseCrashlytics.getInstance().log("Could not get parent Activity");
        }

        if(incident != null){
            if(incident.getFidSuspectTransportationCategory() != 0) suspectVehicle.setSelection(incident.getFidSuspectTransportationCategory());
            if(incident.getEtcSuspectTransportationCategory() != null && !incident.getEtcSuspectTransportationCategory().equals("")) suspectVehicleEtcInput.setText(incident.getEtcSuspectTransportationCategory());

            if(incident.getFidVictimBelongingCategory() != 0) victimAffiliation.setSelection(incident.getFidVictimBelongingCategory());
            if(incident.getEtcVictimBelongingCategory() != null && !incident.getEtcVictimBelongingCategory().equals("")) victimAffiliationEtcInput.setText(incident.getEtcVictimBelongingCategory());

            if(incident.getFidSuspectCharacteristicsCategory() != 0) suspectRecongnition.setSelection(incident.getFidSuspectCharacteristicsCategory());
            if(incident.getEtcSuspectCharacteristicsCategory() != null && !incident.getEtcSuspectCharacteristicsCategory().equals("")) suspectRecongnitionEtcInput.setText(incident.getEtcSuspectCharacteristicsCategory());

            if(incident.getEtcOthersCategory() != null && !incident.getEtcOthersCategory().equals("")) moreEtcInput.setText(incident.getEtcOthersCategory());
        }

        checkIfFinished();
    }

    @Override
    public void onPause() {
        super.onPause();

        saveInput();
    }

    private void saveInput(){
        incident.setFidSuspectTransportationCategory(suspectVehicle.getSelectedItemPosition());
        incident.setEtcSuspectTransportationCategory(suspectVehicleEtcInput.getText().toString());

        incident.setFidVictimBelongingCategory(victimAffiliation.getSelectedItemPosition());
        incident.setEtcVictimBelongingCategory(victimAffiliationEtcInput.getText().toString());

        incident.setFidSuspectCharacteristicsCategory(suspectRecongnition.getSelectedItemPosition());
        incident.setEtcSuspectCharacteristicsCategory(suspectRecongnitionEtcInput.getText().toString());

        incident.setEtcOthersCategory(moreEtcInput.getText().toString());

        ReportWFActivity activity = (ReportWFActivity) getActivity();
        if(activity != null){
            activity.setIncident(incident);
        }else{
            FirebaseCrashlytics.getInstance().log("Could not get parent Activity");
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        try {
            mCallback = (OnInputFinishedListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement OnInputFinishedListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}

