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_5_Fragment extends BaseReportWFFragment {
    private final static int SINGLE_PERSON = 1;
    private final static int SMALL_GROUP = 2;
    private final static int BIG_GROUP = 3;
    private final static int ETC = 5;
    private final static int DONTKNOW = 4;

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

    private View viewFragment;

    private Spinner suspectCount;
    private EditText suspectCountEtcInput;

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

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

        return viewFragment;
    }

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

    private void initInputElements() {
        suspectCount = viewFragment.findViewById(R.id.dd_reportwf_5_count);
        suspectCountEtcInput = viewFragment.findViewById(R.id.txt_reportwf_5_count_etc);

        suspectCountEtcInput.setVisibility(View.INVISIBLE);

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

                switch (position) {
                    case SINGLE_PERSON:
                        suspectCountEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case SMALL_GROUP:
                        suspectCountEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case BIG_GROUP:
                        suspectCountEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case ETC:
                        suspectCountEtcInput.setVisibility(View.VISIBLE);
                        break;
                    case DONTKNOW:
                        suspectCountEtcInput.setVisibility(View.INVISIBLE);
                        break;
                }

                checkIfFinished();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // 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);
            suspectCount.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 isMainCategoryFilled = false;

        if(incident != null){
            isMainCategoryFilled = (suspectCount.getSelectedItemPosition() > 0);
        }

        mCallback.onInputFinished(isMainCategoryFilled);
    }

    @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.getFidSuspectCountCategory() != 0) suspectCount.setSelection(incident.getFidSuspectCountCategory());
            if(incident.getEtcSuspectCountCategory() != null && !incident.getEtcSuspectCountCategory().equals("")) suspectCountEtcInput.setText(incident.getEtcSuspectCountCategory());
        }

        checkIfFinished();
    }

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

        saveInput();
    }

    private void saveInput(){
        incident.setFidSuspectCountCategory(suspectCount.getSelectedItemPosition());
        incident.setEtcSuspectCountCategory(suspectCountEtcInput.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();
    }


}
