package org.stepic.droid.ui.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import org.jetbrains.annotations.NotNull;
import org.stepic.droid.R;
import org.stepic.droid.model.FillBlankComponent;
import org.stepic.droid.ui.custom.ProgressLatexView;
import org.stepic.droid.ui.util.NothingSelectedSpinnerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FillBlanksAdapter extends RecyclerView.Adapter<FillBlanksAdapter.FillBlankViewHolderBase> {
    final static int TEXT_TYPE = 0;
    final static int INPUT_TYPE = 1;
    final static int SELECT_TYPE = 2;

    private List<FillBlankComponent> componentList;
    private boolean isAllEnabled = true;

    public FillBlanksAdapter(@NotNull List<FillBlankComponent> componentList) {
        this.componentList = componentList;
    }

    @Override
    public FillBlankViewHolderBase onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        if (viewType == TEXT_TYPE) {
            View view = layoutInflater.inflate(R.layout.fill_blanks_text_option, parent, false);
            return new TextViewHolder(view);
        } else if (viewType == INPUT_TYPE) {
            View view = layoutInflater.inflate(R.layout.fill_blanks_input_option, parent, false);
            return new InputViewHolder(view);
        } else if (viewType == SELECT_TYPE) {
            View view = layoutInflater.inflate(R.layout.fill_blanks_select_option, parent, false);
            return new SelectViewHolder(view);
        } else {
            throw new IllegalStateException("view type of fill blanks was illegal");
        }
    }

    @Override
    public int getItemViewType(int position) {
        FillBlankComponent fillBlankComponent = componentList.get(position);
        switch (fillBlankComponent.getType()) {
            case text:
                return TEXT_TYPE;
            case input:
                return INPUT_TYPE;
            case select:
                return SELECT_TYPE;
            default:
                throw new IllegalStateException("Illegal type of fill blank component");
        }
    }

    @Override
    public void onBindViewHolder(FillBlankViewHolderBase holder, int position) {
        FillBlankComponent fillBlankComponent = componentList.get(position);
        holder.bindData(fillBlankComponent);
        holder.makeEnabled(isAllEnabled);
    }

    @Override
    public int getItemCount() {
        return componentList.size();
    }

    public void setAllItemsEnabled(boolean isAllEnabled) {
        this.isAllEnabled = isAllEnabled;
        notifyItemRangeChanged(0, getItemCount());
    }

    abstract class FillBlankViewHolderBase extends RecyclerView.ViewHolder {

        public FillBlankViewHolderBase(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public abstract void bindData(FillBlankComponent fillBlankComponent);

        public final void makeEnabled(boolean isEnabled) {
            getRoot().setEnabled(isEnabled);
        }

        public abstract View getRoot();
    }


    class TextViewHolder extends FillBlankViewHolderBase {

        @BindView(R.id.latex_text_fill_blanks)
        ProgressLatexView progressLatexView;


        public TextViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void bindData(FillBlankComponent fillBlankComponent) {
            progressLatexView.setPlainOrLaTeXText(fillBlankComponent.getText()); //// FIXME: 23.01.17 HTML is allowed here
        }

        @Override
        public View getRoot() {
            return progressLatexView;
        }
    }

    class SelectViewHolder extends FillBlankViewHolderBase {

        @BindView(R.id.spinner_fill_blanks)
        Spinner spinner;

        ArrayAdapter<String> dataAdapter;

        public SelectViewHolder(View itemView) {
            super(itemView);
            dataAdapter = new ArrayAdapter<>(itemView.getContext(), R.layout.stepik_spinner_item, new ArrayList<String>());
            NothingSelectedSpinnerAdapter wrappedAdapter = new NothingSelectedSpinnerAdapter(dataAdapter, R.layout.fill_blanks_prompt, itemView.getContext());
            dataAdapter.setDropDownViewResource(R.layout.stepik_spinner_dropdown_item);
            spinner.setAdapter(wrappedAdapter);
        }

        @Override
        public void bindData(FillBlankComponent fillBlankComponent) {
            List<String> options = fillBlankComponent.getOptions();
            if (options == null) {
                throw new IllegalArgumentException("options in select of fill blank cannot be null");
            }
            dataAdapter.clear();
            dataAdapter.addAll(options);
            dataAdapter.notifyDataSetChanged();
        }

        @Override
        public View getRoot() {
            return spinner;
        }
    }

    class InputViewHolder extends FillBlankViewHolderBase {

        @BindView(R.id.input_view_fill_blanks)
        EditText editText;

        public InputViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void bindData(FillBlankComponent fillBlankComponent) {
            //// TODO: 23.01.17 we should restore data
        }

        @Override
        public View getRoot() {
            return editText;
        }
    }

}

@@ -620,16 +620,42 @@
 erKnife;
+%0Aimport timber.log.Timber;
 %0A%0Apublic
@@ -4751,32 +4751,90 @@
 ankComponent) %7B%0A
+            Timber.d(%22Bind view, allocate some strings%22);%0A
             List
@@ -4842,17 +4842,20 @@
 String%3E 
-o
+rawO
 ptions =
@@ -4904,17 +4904,20 @@
     if (
-o
+rawO
 ptions =
@@ -5038,24 +5038,197 @@
           %7D%0A
+            List%3CString%3E options = new ArrayList%3C%3E(rawOptions.size());%0A            for (String item : rawOptions) %7B%0A                options.add(item.trim());%0A            %7D%0A%0A
             

trim strings for view model