package com.anly.githubapp.ui.module.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
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 com.anly.githubapp.R;
import com.anly.githubapp.data.model.Repo;
import com.anly.githubapp.di.component.MainComponent;
import com.anly.githubapp.presenter.main.SearchPresenter;
import com.anly.githubapp.ui.base.LceFragment;
import com.anly.githubapp.ui.module.main.adapter.RepoListRecyclerAdapter;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by mingjun on 16/7/19.
 */
public class SearchFragment extends LceFragment<ArrayList<Repo>> {

    @BindView(R.id.language_spinner)
    Spinner mLanguageSpinner;
    @BindView(R.id.search_key)
    EditText mSearchKeyText;
    @BindView(R.id.repo_list)
    RecyclerView mRepoListView;

    private RepoListRecyclerAdapter mAdapter;

    @Inject
    SearchPresenter mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent(MainComponent.class).inject(this);

        mPresenter.attachView(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, null);
        ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }

    private void initViews() {
        mPresenter.bindSearchView(mSearchKeyText);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
                R.array.languages, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mLanguageSpinner.setAdapter(adapter);

        mAdapter = new RepoListRecyclerAdapter(null);
        mRepoListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRepoListView.setAdapter(mAdapter);
    }

    @NonNull
    @Override
    public String getLoadingMessage() {
        return getString(R.string.load_searching);
    }

    @Override
    public void showContent(ArrayList<Repo> data) {
        mAdapter.setNewData(data);
    }

    @Override
    public void showError(Throwable e) {

    }
}

@@ -344,16 +344,51 @@
 wGroup;%0A
+import android.widget.AdapterView;%0A
 import a
@@ -411,24 +411,24 @@
 rayAdapter;%0A
-
 import andro
@@ -783,16 +783,71 @@
 Adapter;
+%0Aimport com.jakewharton.rxbinding.widget.RxAdapterView;
 %0A%0Aimport
@@ -958,16 +958,45 @@
 erKnife;
+%0Aimport rx.functions.Action1;
 %0A%0A/**%0A *
@@ -2115,16 +2115,22 @@
         
+final 
 ArrayAda
@@ -2364,32 +2364,32 @@
 dropdown_item);%0A
-
         mLanguag
@@ -2417,16 +2417,336 @@
 dapter);
+%0A        RxAdapterView.itemSelections(mLanguageSpinner)%0A                .subscribe(new Action1%3CInteger%3E() %7B%0A                    @Override%0A                    public void call(Integer position) %7B%0A                        mPresenter.setLanguage((String) adapter.getItem(position));%0A                    %7D%0A                %7D);
 %0A%0A      

add language selected listener