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.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_6_Fragment extends BaseReportWFFragment {
    private final static int WOMEN = 1;
    private final static int CHILDREN = 2;
    private final static int MEN = 3;
    private final static int ANIMALS = 4;
    private final static int INFRASTRUCTURE = 5;

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

    private View viewFragment;
    private Spinner victimType;
    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_6, container, false);

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

        return viewFragment;
    }

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

    private void initInputElements() {
        victimType = viewFragment.findViewById(R.id.dd_reportwf_6_victim);

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

                switch (position) {
                    case WOMEN:
                        break;
                    case CHILDREN:
                        break;
                    case MEN:
                        break;
                    case ANIMALS:
                        break;
                    case INFRASTRUCTURE:
                        break;
                }

                checkIfFinished();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {

            }
        });
    }

    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);
            victimType.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 = (victimType.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.getFidVictimCategory() != 0) victimType.setSelection(incident.getFidVictimCategory());
        }

        checkIfFinished();
    }

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

        saveInput();
    }

    private void saveInput(){
        incident.setFidVictimCategory(victimType.getSelectedItemPosition());

        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();
    }


}
