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_4_Fragment extends BaseReportWFFragment {
    private final static int COMMON_OBJECTS = 1;
    private final static int FIREARM = 2;
    private final static int EXPLOSIONS = 3;
    private final static int MIL= 4;
    private final static int ETC = 6;
    private final static int DONTKNOW = 5;

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

    private View viewFragment;

    private Spinner suspectWeapon;
    private EditText suspectWeaponEtcInput;

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

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

        return viewFragment;
    }

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


    private void initInputElements() {
        suspectWeapon = viewFragment.findViewById(R.id.dd_reportwf_4_weapon);
        suspectWeaponEtcInput = viewFragment.findViewById(R.id.txt_reportwf_4_weapon_etc);

        suspectWeaponEtcInput.setVisibility(View.INVISIBLE);

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

                switch (position) {
                    case COMMON_OBJECTS:
                        suspectWeaponEtcInput.setVisibility(View.VISIBLE);
                        break;
                    case FIREARM:
                        suspectWeaponEtcInput.setVisibility(View.VISIBLE);
                        break;
                    case EXPLOSIONS:
                        suspectWeaponEtcInput.setVisibility(View.VISIBLE);
                        break;
                    case MIL:
                        suspectWeaponEtcInput.setVisibility(View.VISIBLE);
                        break;
                    case ETC:
                        suspectWeaponEtcInput.setVisibility(View.VISIBLE);
                        break;
                    case DONTKNOW:
                        suspectWeaponEtcInput.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);
            suspectWeapon.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 = (suspectWeapon.getSelectedItemPosition() > 0);
        }

        saveInput();
        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.getFidSuspectWeaponCategory() != 0) suspectWeapon.setSelection(incident.getFidSuspectWeaponCategory());
            if(incident.getEtcSuspectWeaponCategory() != null && !incident.getEtcSuspectWeaponCategory().equals("")) suspectWeaponEtcInput.setText(incident.getEtcSuspectWeaponCategory());
        }

        checkIfFinished();
    }

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

        saveInput();
    }

    private void saveInput(){
        incident.setFidSuspectWeaponCategory(suspectWeapon.getSelectedItemPosition());
        incident.setEtcSuspectWeaponCategory(suspectWeaponEtcInput.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();
    }


}
