package org.deke.risk.riskahead.fragment;

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

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;

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

    private final static int VIOLANCE_GENERAL = 1;
    private final static int VIOLANCE_VERBAL = 2;
    private final static int VIOLANCE_LIGHT = 3;
    private final static int VIOLANCE_HARD = 4;
    private final static int VIOLANCE_NATURE = 5;
    private final static int VIOLANCE_INFRASTRUCTURE = 6;

    private View viewFragment;

    private Spinner crimeCategory;
    private Spinner crimeSubCategory;
    private EditText crimeSubOther;

    private RelativeLayout timeLayout;
    private TextView txtPositionInformation;

    private IncidentReport incident;

    private OnInputFinishedListener mCallbackFinished;

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

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

        return viewFragment;
    }

    private void initInputElements() {
        crimeCategory = (Spinner) viewFragment.findViewById(R.id.dd_reportwf_1_cat_main);
        crimeSubCategory = (Spinner) viewFragment.findViewById(R.id.dd_reportwf_1_cat_sub);
        crimeSubOther = (EditText) viewFragment.findViewById(R.id.txt_reportwf_1_cat_etc);
        timeLayout = (RelativeLayout) viewFragment.findViewById(R.id.ll_pos);
        txtPositionInformation = (TextView) viewFragment.findViewById(R.id.lbl_reportwf_position_detail);

        crimeSubCategory.setVisibility(View.INVISIBLE);
        crimeSubOther.setVisibility(View.INVISIBLE);
        timeLayout.setVisibility(View.INVISIBLE);
        ((ReportWFActivity)getActivity()).setMapButtonVisibility(false);

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

                switch (position) {
                    case 1:
                        stringId = R.array.cat_situation_sub_general;
                        break;
                    case 2:
                        stringId = R.array.cat_situation_sub_verbal;
                        break;
                    case 3:
                        stringId = R.array.cat_situation_sub_force;
                        break;
                    case 4:
                        stringId = R.array.cat_situation_sub_force_serious;
                        break;
                    case 5:
                        stringId = R.array.cat_situation_sub_nature;
                        break;
                    case 6:
                        stringId = R.array.cat_situation_sub_infrastructure;
                        break;
                    default:
                        stringId = R.array.cat_situation_sub_general;
                        break;
                }

                if (((SpinnerHintAdapter) parent.getAdapter()).isInInitialState()) {
                    crimeSubCategory.setVisibility(View.INVISIBLE);
                } else {
                    crimeSubCategory.setVisibility(View.VISIBLE);
                    incident.setFidIncidentCategory(position);
                    setSubSpinnerValues(stringId);
                }

                checkIfFinished();
            }

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

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

                switch (((Spinner) viewFragment.findViewById(R.id.dd_reportwf_1_cat_main)).getSelectedItemPosition()) {
                    case VIOLANCE_GENERAL:
                        i = 7;
                        break;
                    case VIOLANCE_VERBAL:
                        i = 5;
                        break;
                    case VIOLANCE_LIGHT:
                        i = 6;
                        break;
                    case VIOLANCE_HARD:
                        i = 7;
                        break;
                    case VIOLANCE_NATURE:
                        i = 7;
                        break;
                    case VIOLANCE_INFRASTRUCTURE:
                        i = 8;
                        break;
                }

                if (((SpinnerHintAdapter) parent.getAdapter()).isInInitialState()) {
                    crimeSubOther.setVisibility(View.INVISIBLE);
                } else {
                    timeLayout.setVisibility(View.VISIBLE);

                    if (position == i){
                        crimeSubOther.setVisibility(View.VISIBLE);
                    }else{
                        crimeSubOther.setVisibility(View.INVISIBLE);
                        crimeSubOther.setText("");
                    }

                    ((ReportWFActivity)getActivity()).setMapButtonVisibility(true);
                    incident.setFidIncidentSubCategory(position);
                }

                checkIfFinished();
            }

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

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

    private void setSubSpinnerValues(int stringId) {
        ArrayAdapter<CharSequence> adapter_sub_1 = ArrayAdapter.createFromResource(viewFragment.getContext(),stringId, android.R.layout.simple_spinner_dropdown_item);
        crimeSubCategory.setAdapter(new SpinnerHintAdapter(adapter_sub_1, R.layout.contact_spinner_row_nothing_selected, getActivity()));

        if(incident.getFidIncidentSubCategory() > 0) crimeSubCategory.setSelection(incident.getFidIncidentSubCategory());
    }

    private void checkIfFinished(){
        boolean mainCategoryIsFilled = false;
        boolean subCategoryIsFilled = false;
        boolean positionIsFilled = false;

        if(incident != null){
            mainCategoryIsFilled = (crimeCategory.getSelectedItemPosition() > 0) && (incident.getFidIncidentCategory() > 0);

            subCategoryIsFilled = (crimeSubCategory.isShown()) && (crimeSubCategory.getSelectedItemPosition() > 0) && (incident.getFidIncidentSubCategory() > 0);

            positionIsFilled = (timeLayout.isShown()) && (incident.getPosition() != null);
        }

        boolean isFinished = mainCategoryIsFilled && subCategoryIsFilled && positionIsFilled;

        mCallbackFinished.onInputFinished(isFinished);
    }

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

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

        if(incident != null){
            if(incident.getFidIncidentCategory() > 0){
                crimeCategory.setSelection(incident.getFidIncidentCategory());
                // The IncidentSubCategory gets automatically initialized
            }

            if (!incident.getEtcIncidentCategory().equals("")){
                crimeSubOther.setText(incident.getEtcIncidentCategory());
            }

            if(incident.getPosition() != null){
                timeLayout.setVisibility(View.VISIBLE);
                txtPositionInformation.setText(incident.getIncidentPositionDescription());
                ((ReportWFActivity)getActivity()).setMapButtonVisibility(true);
            }
        }

        checkIfFinished();
    }

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

        incident.setFidIncidentCategory(crimeCategory.getSelectedItemPosition());
        incident.setFidIncidentSubCategory(crimeSubCategory.getSelectedItemPosition());
        incident.setEtcIncidentCategory(crimeSubOther.getText().toString());

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

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            mCallbackFinished = (OnInputFinishedListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement all Listeners");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
