package org.deke.risk.riskahead.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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 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;

/**
 * @author Dennis Thießen
 */
public class ReportWF_7_Fragment extends Fragment {


    private final static int LOCAL = 1;
    private final static int TOURIST = 2;
    private final static int FOREIGNER = 3;
    private final static int ETC = 4;
    private final static int DONTKNOW = 5;

    private View viewFragment;

    private Spinner victimOrigin;
    private EditText victimOriginEtcInput;

    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(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        viewFragment = inflater.inflate(R.layout.fragment_reportwf_7, container, false);

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

        return viewFragment;
    }

    private void initInputElements() {
        victimOrigin = (Spinner) viewFragment.findViewById(R.id.dd_reportwf_7_victimorigin);
        victimOriginEtcInput = (EditText) viewFragment.findViewById(R.id.txt_reportwf_7_victimorigin_etc);

        victimOriginEtcInput.setVisibility(View.INVISIBLE);

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

                switch (position) {
                    case LOCAL:
                        victimOriginEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case TOURIST:
                        victimOriginEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case FOREIGNER:
                        victimOriginEtcInput.setVisibility(View.INVISIBLE);
                        break;
                    case ETC:
                        victimOriginEtcInput.setVisibility(View.VISIBLE);
                        break;
                    case DONTKNOW:
                        victimOriginEtcInput.setVisibility(View.INVISIBLE);
                        break;
                }

                if (position > 0) {
                    incident.setFidVictimOriginCategory(position);
                }

                checkIfFinished();
            }

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

    private void setMainSpinnerValues(int stringId){
        ArrayAdapter<CharSequence> adapter_main_1 = ArrayAdapter.createFromResource(getActivity(), stringId, android.R.layout.simple_spinner_dropdown_item);
        victimOrigin.setAdapter(new SpinnerHintAdapter(adapter_main_1, R.layout.contact_spinner_row_nothing_selected, getActivity()));
    }


    private void checkIfFinished(){
        boolean isMainCategoryFilled = false;

        if(incident != null){
            isMainCategoryFilled = (victimOrigin.getSelectedItemPosition() > 0) && (incident.getFidVictimOriginCategory() > 0);
        }

        mCallback.onInputFinished(isMainCategoryFilled);
    }

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

        incident = ((ReportWFActivity)getActivity()).getIncident();

        if(incident != null){
            if(incident.getFidVictimOriginCategory() != 0) victimOrigin.setSelection(incident.getFidVictimOriginCategory());
            if(incident.getEtcVictimOriginCategory() != null && !incident.getEtcVictimOriginCategory().equals("")) victimOriginEtcInput.setText(incident.getEtcVictimOriginCategory());
        }

        checkIfFinished();
    }

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

        incident.setFidVictimOriginCategory(victimOrigin.getSelectedItemPosition());
        incident.setEtcVictimOriginCategory(victimOriginEtcInput.getText().toString());

        ((ReportWFActivity)getActivity()).setIncident(incident);
    }

    @Override
    public void onAttach(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();
    }


}
