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.helper.IncidentReport;
import org.deke.risk.riskahead.adapter.SpinnerHintAdapter;

/**
 *
 * (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_3_Fragment extends BaseReportWFFragment {
    private final static int GANGS = 1;
    private final static int POLICE = 2;
    private final static int MOB = 3;
    private final static int PARAMILITARY = 4;
    private final static int MILITARY = 5;
    private final static int TERRORIST = 6;
    private final static int DONTKNOW = 7;
    private final static int ETC = 8;

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

    private View viewFragment;

    private Spinner suspectCategory;
    private Spinner suspectSubCategory;
    private EditText suspectEtcInput;

    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_3, container, false);

        initInputElements();
        setMainSpinnerValues(R.array.cat_suspect_main);

        return viewFragment;
    }

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

    private void initInputElements() {
        suspectCategory = viewFragment.findViewById(R.id.dd_reportwf_3_suspect);
        suspectSubCategory = viewFragment.findViewById(R.id.dd_reportwf_3_suspect_sub);
        suspectEtcInput = viewFragment.findViewById(R.id.txt_reportwf_3_suspect_etc);

        suspectSubCategory.setVisibility(View.INVISIBLE);
        suspectEtcInput.setVisibility(View.INVISIBLE);

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

                switch (position) {
                    case GANGS:
                        break;
                    case POLICE:
                        break;
                    case MOB:
                        break;
                    case PARAMILITARY:
                        stringId = R.array.cat_suspect_sub_location;
                        break;
                    case MILITARY:
                        stringId = R.array.cat_suspect_sub_location;
                        break;
                    case TERRORIST:
                        stringId = R.array.cat_suspect_sub_location;
                        break;
                    case ETC:
                        suspectEtcInput.setVisibility(View.VISIBLE);
                        break;
                    case DONTKNOW:
                        break;
                }

                if (((SpinnerHintAdapter) suspectCategory.getAdapter()).isInInitialState()) {
                    suspectSubCategory.setVisibility(View.INVISIBLE);
                } else {
                    if (stringId != 0) {
                        suspectSubCategory.setVisibility(View.VISIBLE);
                        setSubSpinnerValues(stringId);
                    } else {
                        suspectSubCategory.setVisibility(View.INVISIBLE);
                    }

                    if (position != ETC) {
                        suspectEtcInput.setVisibility(View.INVISIBLE);
                    }
                }

                checkIfFinished();
            }

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

        suspectSubCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if ((position < 1) && (incident.getFidSuspectSubCategory() != 0)) suspectSubCategory.setSelection(incident.getFidSuspectSubCategory());

                checkIfFinished();
            }

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


    private void setMainSpinnerValues(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);
            suspectCategory.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 setSubSpinnerValues(int stringId) {
        ArrayAdapter<CharSequence> adapter_sub_1 = ArrayAdapter.createFromResource(viewFragment.getContext(), stringId, android.R.layout.simple_spinner_dropdown_item);
        suspectSubCategory.setAdapter(new SpinnerHintAdapter(adapter_sub_1, R.layout.contact_spinner_row_nothing_selected, getActivity()));
    }

    private void checkIfFinished(){
        boolean isSuspectCategoryFilled = false;

        if(incident != null){
            if(suspectCategory.getSelectedItemPosition() > 0){
                isSuspectCategoryFilled = true;
            }

            if(suspectSubCategory.isShown()){
                isSuspectCategoryFilled = suspectSubCategory.getSelectedItemPosition() > 0;
            }
        }

        mCallback.onInputFinished(isSuspectCategoryFilled);
    }

    @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.getFidSuspectCategory() != 0) suspectCategory.setSelection(incident.getFidSuspectCategory());
            if(incident.getFidSuspectSubCategory() != 0) suspectSubCategory.setSelection(incident.getFidSuspectSubCategory());
            if(incident.getEtcSuspectCategory() != null && !incident.getEtcSuspectCategory().equals("")) suspectEtcInput.setText(incident.getEtcSuspectCategory());
        }

        checkIfFinished();
    }

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

        saveInput();
    }

    private void saveInput() {
        incident.setFidSuspectCategory(suspectCategory.getSelectedItemPosition());
        incident.setFidSuspectSubCategory(suspectSubCategory.getSelectedItemPosition());
        incident.setEtcSuspectCategory(suspectEtcInput.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();
    }

}

