package com.zhilin.patrol.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.zhilin.patrol.R;
import com.zhilin.patrol.bean.AddressAreaEntity;
import com.zhilin.patrol.tool.DensityUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 定位选择
 */
public class AddressSelectActivity extends BaseActivity {

    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.searview)
    SearchView mSearchView;
    private ArrayList<AddressAreaEntity.InfoListBean> mAddressAreaList;
    private ArrayList<AddressAreaEntity.InfoListBean> mCopyAddressAreaList;
    private CommonAdapter mAdapter;
    private AddressAreaEntity.InfoListBean mListBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_type);
        ButterKnife.bind(this);
        mSearchView.setVisibility(View.VISIBLE);
        TextView txt_search = mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        //设置字体大小为14sp
        txt_search.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);//14sp
        mAddressAreaList = (ArrayList<AddressAreaEntity.InfoListBean>) getIntent().getSerializableExtra("AddressAreaList");
        if (mAddressAreaList == null) {
            mAddressAreaList = new ArrayList<>();
        }
        mCopyAddressAreaList = new ArrayList<>(mAddressAreaList);
        setTitle("附近巡查小区");
        init();
    }

    private void init() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .color(getResources().getColor(R.color.line_gray))
                .size(DensityUtil.dip2px(this, 1))
                .build());

        mAdapter = new CommonAdapter<AddressAreaEntity.InfoListBean>(this, R.layout.item_address_select, mAddressAreaList) {
            @Override
            protected void convert(ViewHolder holder, final AddressAreaEntity.InfoListBean infoListBean, int position) {
                    holder.setText(R.id.tv_address, infoListBean.getAddress());
                    holder.setText(R.id.tv_distance, "距此" + infoListBean.getDistance() + "m");
                    holder.setVisible(R.id.iv_select, infoListBean.isSelected());
                    if (infoListBean.isSelected()) {
                        holder.setTextColorRes(R.id.tv_address, R.color.text_orange);
                    } else {
                        holder.setTextColorRes(R.id.tv_address, R.color.text_black);
                    }
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            for (AddressAreaEntity.InfoListBean bean : mAddressAreaList) {
                                bean.setSelected(false);
                            }
                            infoListBean.setSelected(true);
                            mListBean = infoListBean;

//                        mAdapter.notifyDataSetChanged();
                            finish();
                        }
                    });
                }

        };
        mRecyclerView.setAdapter(mAdapter);

        //根据输入框输入值的改变来过滤搜索
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                filterData(newText);
                return false;
            }
        });
    }

    /**
     * 根据输入框中的值来过滤数据并更新RecyclerView
     *
     * @param filterStr
     */
    private void filterData(String filterStr) {
        if (mCopyAddressAreaList == null) return;
        mAddressAreaList.clear();
        if (TextUtils.isEmpty(filterStr)) {
            mAddressAreaList.addAll(mCopyAddressAreaList);
        } else {
            for (AddressAreaEntity.InfoListBean infoListBean : mCopyAddressAreaList) {
                if (infoListBean.getAddress().contains(filterStr)) {
                    mAddressAreaList.add(infoListBean);
                }
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void finish() {
        if (mListBean != null) {
            Intent intent = new Intent(); //返回上一个页面 可以不用写跳转
            intent.putExtra("ListBean", mListBean);
            intent.putExtra("AddressAreaList", mCopyAddressAreaList);
            setResult(1, intent);
        }
        super.finish();
    }

}
